题目传送:HDU - 3068 - 最长回文
AC代码:
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <utility>
#include <iostream>
#include <algorithm>
#include <functional>
#define LL long long
#define INF 0x7fffffff
using namespace std;
char s[250000];
char str[250000];
int p[250000];
int len_s;
int len_str;
void get_str() {
str[0] = ‘$‘;
str[1] = ‘#‘;
for(int i = 1; i <= len_s; i ++) {
str[i * 2] = s[i];
str[i * 2 + 1] = ‘#‘;
}
str[len_s * 2 + 2] = ‘\0‘;
len_str = len_s * 2 + 1;
}
void solve() {
int id;
int mx = 0;
for(int i = 1; i <= len_str; i ++) {
if(mx > i) {
p[i] = min(p[id * 2 - i], mx - i);
}
else p[i] = 1;
while(str[i + p[i]] == str[i - p[i]]) p[i] ++;
if(p[i] + i > mx) {
mx = p[i] + i;
id = i;
}
}
}
int main() {
while(scanf("%s", s + 1) != EOF) {
len_s = strlen(s + 1);
get_str();
solve();
int ans = 0;
for(int i = 1; i <= len_str; i ++) {
ans = ans > p[i] ? ans : p[i];
}
printf("%d\n", ans - 1);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u014355480/article/details/48006031