【HDOJ】3068 最长回文

马拉车算法O(n)可解。

 /* 3068 */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxn = ;
char s[maxn*], ss[maxn];
int P[maxn*];
int ans, l; void init() {
int i, len = strlen(ss); l = ;
s[l++] = '@';
s[l++] = '#';
for (i=; i<len; ++i) {
s[l++] = ss[i];
s[l++] = '#';
}
s[l++] = '\0';
} void manacher() {
int i, mx = , id; for (i=; i<l; ++i) {
P[i] = mx>i ? min(P[*id-i], mx-i) : ;
while (s[i+P[i]] == s[i-P[i]])
++P[i];
if (i+P[i] > mx) {
mx = i + P[i];
id = i;
}
} ans = ;
for (i=; i<l; ++i)
ans = max(ans, P[i]);
printf("%d\n", ans-);
} void solve() {
init();
manacher();
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif while (scanf("%s", ss)!=EOF) {
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
上一篇:eclipse导入已有源码


下一篇:.NET Core项目从xproj+project.json向csproj迁移简介