POJ----(3974 )Palindrome [最长回文串]

    

Time Limit: 15000MS   Memory Limit: 65536K
Total Submissions: 5121   Accepted: 1834

Description

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

Source

Seventh ACM Egyptian National Programming Contest

关于manacher算法..........()

代码:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #include<algorithm>
 5 #define maxn 1000010
 6 char ss[maxn];
 7 char str[maxn << 1L];
 8 int var[maxn << 1L];
 9
10 int manacher() {
11
12
13     int len = 2, mx_l = 0, pos = 0;
14     str[0] = ‘$‘;
15     str[1] = ‘#‘;
16     for (int i = 0; ss[i] != ‘\0‘; i++) {
17         str[len++] = ss[i];
18         str[len++] = ‘#‘;
19     }
20     str[len] = ‘\0‘;
21     for (int i = 0; i<len; i++) {
22         var[i] = mx_l>i ? std::min(var[2 * pos - i], mx_l - i) : 1;
23         while (str[i + var[i]] == str[i - var[i]])++var[i];
24         if (mx_l<i + var[i]) {
25             mx_l = i + var[i];
26             pos = i;
27         }
28     }
29     return len;
30 }
31
32 int main() {
33
34     int cnt = 1, len = 0, res = 0;
35     while (scanf("%s", ss) != EOF) {
36
37         if (ss[0] == ‘E‘) break;
38         len = manacher();
39         res = 0;
40         for (int i = 0; i<len; i++)
41             res =std::max(res, var[i]);
42         memset(str, 0, sizeof(char)*len);
43         printf("Case %d: %d\n", cnt++, res-1);
44     }
45     return 0;
46 }
时间: 2024-10-02 19:54:22

POJ----(3974 )Palindrome [最长回文串]的相关文章

[LeetCode] Longest Palindrome 最长回文串

Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example "Aa" is not considered a palindrome here. Note: Assume the leng

1112个人赛,最长回文串常见算法讨论

ps.此贴大部分文字与代码来自网上,我只是取长补短整理了下 S=“c a b a”  那么  S' = “a b a c”, 这样的情况下 S和 S‘的最长公共子串是aba.没有错误. 但是当 S=“abacdfgdcaba”, 那么S’ = “abacdgfdcaba”. 这样S和S‘的最长公共子串是abacd.很明显abacd并不是S的最长回文子串,它甚至连回文都不是. 现在是不是都明白为什么最长回文子串不能转化成为最长公共子串问题了.当原串S中含有一个非回文的串的反序串的时候,最长公共子串

poj3974 最长回文串 exkmp

Palindrome Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 3695   Accepted: 1338 Description Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, "Can you pro

409.求最长回文串的长度 LongestPalindrome

题目要求求出长度即可,并不需要求出最长回文串. 思路:用字典统计每一个字符的出现次数,出现次数大于1的字符必定出现在回文串中,另外还再加上一个中心点. public static int LongestPalindrome(string s) { int length = 0; Dictionary<char, int> dictionary = new Dictionary<char, int>(); int value = 0; foreach (char c in s) {

hdu--3068 最长回文串(manachar模板)

Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 回文就是正反读都是一样的字符串,如aba, abba等 Input 输入有多组case,不超过120组,每组输入为一行小写英文字符a,b,c...y,z组成的字符串S 两组case之间由空行隔开(该空行不用处理) 字符串长度len <= 110000 Output 每一行一个整数x,对应一组case,表示该组case的字符串中所包含的最长回文长度. Sample Input aaaa

(最长回文串 模板) 最长回文 -- hdu -- 3068

http://acm.hdu.edu.cn/showproblem.php?pid=3068 最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12079    Accepted Submission(s): 4430 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长

hdu 3068 最长回文串 o(n) Manacher 算法

最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10596    Accepted Submission(s): 3759 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 回文就是正反读都是一样的字符串,如aba, abba等 Input 输入有多

HDU ACM 4513 吉哥系列故事——完美队形II-&gt;求最长回文串(manacher算法)

分析:该題可以通过求最长回文串的方法来解决:求最长回文串使用manacher算法,O(n)时间复杂度. 注意:while(a[i-len[i]]==a[i+len[i]] && a[i-len[i]]<=a[i-len[i]+2])这里多出的判断a[i-len[i]]<=a[i-len[i]+2]即为该題的限制从左到中保证身高不降,因在回文串的计算过程中添加了额外的字符,所以这里是i-len[i]+2而不是i-len[i]+1,以避开添加的字符. #include<ios

Manacher算法 O(n)求最长回文串

最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5158    Accepted Submission(s): 1755 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 回文就是正反读都是一样的字符串,如aba, abba等 Input 输入有多组