bzoj 2160: 拉拉队排练 回文自动机

Description

Input

Output

题解:

• 一个长为n的字符串中最多含有n个本质不同的回文串

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
const int maxn = 1000010;
struct Node{
int nx[26];
int siz,fail,len;
}T[maxn];
int last,nodecnt,str[maxn],len;
inline void init(){
last = nodecnt = 0;
T[++nodecnt].len = -1;
T[0].fail = 1;
str[len=0] = -1;
}
inline void insert(char cha){
int c = cha - 'a',cur,p,x;str[++len] = c;
for(p = last;str[len-T[p].len-1] != str[len];p = T[p].fail);
if(T[p].nx[c] == 0){
T[cur = ++ nodecnt].len = T[p].len + 2;
for(x = T[p].fail;str[len-T[x].len-1] != str[len];x = T[x].fail);
T[cur].fail = T[x].nx[c];T[p].nx[c] = cur;
}T[last = T[p].nx[c]].siz ++ ;
}
const int mod = 19930726;
inline ll qpow(ll x,int p){
ll ret = 1;
for(;p;p>>=1,x=x*x%mod) if(p&1) ret=ret*x%mod;
return ret;
}
int q[maxn],siz[maxn],length[maxn];
inline bool cmp(const int &i,const int &j){
return length[i] > length[j];
}
int main(){init();
for(int i=1;i<=n;++i){
while(ch=getchar(),ch<'!');
insert(ch);
}
for(int i = nodecnt;i>=2;--i){
q[i] = i;
if(T[i].fail) T[T[i].fail].siz += T[i].siz;
siz[i] = T[i].siz;length[i] = T[i].len;
}sort(q+2,q+nodecnt+1,cmp);
ll ans = 1;
for(int i=2;i<=nodecnt;++i){
int x = q[i];
if(k > siz[x]){
k -= siz[x];
ans = ans*qpow(length[x],siz[x]) % mod;
}else{
ans = ans*qpow(length[x],k) % mod;
break;
}
}printf("%lld\n",ans);
getchar();getchar();
return 0;
}


posted @ 2017-03-11 07:31  Sky_miner  阅读(256)  评论(0编辑  收藏  举报