1. 程式人生 > >Palindrome POJ - 3974 (字符串hash+二分)

Palindrome POJ - 3974 (字符串hash+二分)

itself bcb length 二分枚舉 std ans pla case har

Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, "Can you propose an efficient algorithm to find the length of the largest palindrome in a string?"

A string is said to be a palindrome if it reads the same both forwards and backwards, for example "madam" is a palindrome while "acm" is not.

The students recognized that this is a classical problem but couldn‘t come up with a solution better than iterating over all substrings and checking whether they are palindrome or not, obviously this algorithm is not efficient at all, after a while Andy raised his hand and said "Okay, I‘ve a better algorithm" and before he starts to explain his idea he stopped for a moment and then said "Well, I‘ve an even better algorithm!".

If you think you know Andy‘s final solution then prove it! Given a string of at most 1000000 characters find and print the length of the largest palindrome inside this string.

Input

Your program will be tested on at most 30 test cases, each test case is given as a string of at most 1000000 lowercase characters on a line by itself. The input is terminated by a line that starts with the string "END" (quotes for clarity).

Output

For each test case in the input print the test case number and the length of the largest palindrome.

Sample Input

abcbabcbabcba
abacacbaaaab
END

Sample Output

Case 1: 13
Case 2: 6

題意:給一個字符串,找出其中最長回文子串長度
思路:二分枚舉其長度,然後枚舉每個位置每個位置,利用字符串hash值比較中間位置前後是否一致,需要分奇偶進行二分,因為奇數或者偶數的時候判斷前後時候一致的區間不同


技術分享圖片
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 const int maxn = 1e6+5;
 8 unsigned long long f1[maxn],f2[maxn],p[maxn];
 9 int even[maxn>>1],odd[maxn>>1];
10 
11 char word[maxn];
12 int n;
13 
14 unsigned long long Find_l(int l,int r)
15 {
16     return f2[n-l+1]-f2[n-r]*p[r-l+1];//逆序hash值
17 }
18  unsigned long long Find_r(int l,int r)
19 {
20     return f1[r]-f1[l-1]*p[r-l+1];//正序hash值
21 }
22 
23 int check(int x)
24 {
25     int len = x;
26     x /= 2;
27     if(len & 1)
28     {
29         for(int i=x; i<=n-x-1; i++)
30             if(Find_l(i-x+1,i)==Find_r(i+2,i+x+1))
31                 return len;
32     }
33     else
34     {
35         for(int i=x; i<=n-x; i++)
36             if(Find_l(i-x+1,i)==Find_r(i+1,x+i))
37                 return len;
38     }
39     return 0;
40 }
41 
42 int serch(int num[],int l,int r)
43 {
44     int maxx = 1;
45     while(l <= r)
46     {
47         int mid = (l+r) >> 1;
48         int val = check(num[mid]);
49         if(val)
50         {
51             l = mid + 1;
52             maxx = max(val,maxx);
53         }
54         else
55             r = mid - 1;
56     }
57     return maxx;
58 }
59 int main()
60 {
61     int tot1 = 0,tot2 = 0;
62     p[0] = 1;
63     for(int i=1; i<=1000000; i++)
64     {
65         p[i] = p[i-1]*131;
66         if(i&1)
67             odd[++tot1] = i;
68         else
69             even[++tot2] = i;
70     }
71     int cas = 0;
72     while(~scanf("%s",word+1) && word[1] != E)
73     {
74         f1[0] = f2[0] = 0;
75         n = strlen(word+1);
76         for(int i=1; i<=n; i++)
77         {
78             f1[i] = f1[i-1]*131 + word[i]-a+1;
79             f2[i] = f2[i-1]*131 + word[n-i+1]-a+1;
80         }
81         int ans1 = serch(odd,1,n/2+1);
82         int ans2 = serch(even,1,n/2+1);
83         printf("Case %d: %d\n",++cas,max(ans1,ans2));
84     }
85 }
View Code






Palindrome POJ - 3974 (字符串hash+二分)