Isomorphic Inversion

时间限制: 1 Sec  内存限制: 128 MB
题目描述
Let s be a given string of up to 106 digits. Find the maximal k for which it is possible to partition s into k consecutive contiguous substrings, such that the k parts form a palindrome.
More precisely, we say that strings s0, s1, . . . , sk−1 form a palindrome if si = sk−1−i for all 0 ≤ i < k.
In the first sample case, we can split the string 652526 into 4 parts as 6|52|52|6, and these parts together form a palindrome. It turns out that it is impossible to split this input into more than 4 parts while still making sure the parts form a palindrome.
输入
A nonempty string of up to 106 digits.
输出
Print the maximal value of k on a single line.

样例输入

652526

样例输出
4


题意:给一串数字串,求问最多能分成多少个区域使得区域回文。
 1 #include<bits/stdc++.h>
 2 #pragma GCC optimize(3)
 3 using namespace std;
 4 typedef long long ll;
 5 const int maxn=1e6+7;
 6 const ll prime=97;
 7 ll Ha[maxn];
 8 char str[maxn];
 9 void init()
10 {
11     Ha[0]=1;
12     for(int i=1; i<=maxn-5; ++i)
13     {
14         Ha[i]=Ha[i-1]*prime;
15     }
16 }
17 int main()
18 {
19     init();
20     int ans=0;
21     scanf("%s",str+1);
22     int len=strlen(str+1);
23     int l=1,r=len;
24     while(l<=r)
25     {
26         int start=r;
27         ll ldata=ll(str[l]-'0'),rdata=ll(str[r]-'0');
28         while(ldata!=rdata&&(l<r))
29         {
30             ++l;
31             --r;
32             ldata=ll(str[l]-'0')+ldata*prime;
33             rdata+=ll(str[r]-'0')*Ha[start-r];
34         }
35         if(ldata!=rdata)
36         {
37             ++ans;
38             break;
39         }
40         if(l!=r)ans+=2;
41         else ++ans;
42         ++l;
43         --r;
44     }
45     printf("%d\n",ans);
46     return 0;
47 }
 
Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐