Bare palindrome Automatic Machine
3676: [Apio2014] palindrome string Time limit:20 Sec Memory limit:128 MB
submit:504 solved:152
[Submit] [Status] [Discuss] Description
Consider a string s that contains only the lowercase Latin alphabet. We define the "out" of a substring t of s
The present value is the number of occurrences of T in s multiplied by the length of T. Please find out the most in all palindrome strings of S
Large occurrence value.
Input
Enter only one row for a non-empty string s that contains only a lowercase letter (A-Z).
Output
Outputs an integer that is the largest occurrence of a palindrome substring.
Sample Input "example inputs L"
Abacaba
"Sample input 2]
Www
Sample Output "example outputs L"
7
Sample Output 2]
4
HINT
A string is a palindrome when and only if it reads from left to right and reads from right to left exactly the same.
In the first example, there are 7 palindrome substrings: A,b,c,aba,aca,bacab,abacaba, wherein:
A appears 4 times with a value of 4:1:1=4
B appears 2 times with a value of 2:1:1=2
C appears 1 times with a value of l:1:l=l
ABA appears 2 times with a value of 2:1:3=6
ACA appears 1 times with a value of 1=1:3=3
Bacab appears 1 times with a value of 1:1:5=5
Abacaba appears 1 times with a value of 1:1:7=7
Therefore, the maximum palindrome string occurrence value is 7.
"Data size and scoring"
The data satisfies the 1≤ string length ≤300000.
Source
[Submit] [Status] [Discuss]
HOME back
/* *********************************************** Author:ckboss Created time:2015 year April 17 Friday 22:29 19 sec File Nam
E:bzoj3676.cpp ************************************************ * * #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib
> #include <vector> #include <queue> #include <set> #include <map> using namespace std;
typedef long long int LL;
const int maxn=330000;
const int c=30;
int next[maxn][c];
int FAIL[MAXN]; int CNT[MAXN]; The number of occurrences of a palindrome string in essence (count after) int NUM[MAXN]; Indicates the most right end of the longest palindrome string represented by node I is the number of palindrome strings at the end of the palindrome string int LEN[MAXN]; Node I represents the length of the palindrome string int S[MAXN]; The character of the node I exists int last; A new letter is formed after the longest palindrome string represented by the node int p; The number of added nodes P-2 is the number of different palindrome string int n;
Add the number of characters int newnode (int x) {for (int i=0;i<c;i++) next[p][i]=0; cnt[p]=0; num[p]=0;
Len[p]=x;
return p++;
} void Init () {p=0; NewNode (0);
NewNode (-1); last=0;
n=0; S[0]=-1; fail[0]=1;
} int Get_fail (int x) {while (S[n-len[x]-1]!=s[n]) x=fail[x];
return x;
} void Add (int c) {c-= ' a ';
S[++n]=c;
int Cur=get_fail (last);
if (!next[cur][c]) {int now=newnode (len[cur]+2);
Fail[now]=next[get_fail (Fail[cur])][c];
Next[cur][c]=now;
num[now]=num[fail[now]]+1;
} Last=next[cur][c];
cnt[last]++;
} void Count () {for (int i=p-1;i>=0;i--) cnt[fail[i]]+=cnt[i];}
Char STR[MAXN];
int main () {//freopen ("In.txt", "R", stdin);
Freopen ("OUT.txt", "w", stdout);
while (scanf ("%s", str)!=eof) {init ();
for (int i=0,sz=strlen (str); i<sz;i++) Add (Str[i]);
Count ();
LL Ans=1;
for (int i=2;i<p;i++) Ans=max (Ans,1ll*len[i]*cnt[i]);
printf ("%lld\n", ans);
} return 0; }