ACM-ICPC 2018 南京赛区网络预赛Skr

题意:求本质不同的回文子串的和

题解:先构造pam,然后根据pam的原理(ch表示在该节点表示的回文串两侧加上该字符)对于每个节点维护一个表示该节点字符串的值,加起来即可

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define LL long long
//#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
template<typename T>
inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>
inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;} using namespace std; const double eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=2000000+10,maxn=1000000+10,inf=0x3f3f3f3f; struct PAM{
int ch[N][10],fail[N],cnt[N],num[N],len[N],s[N],le[N];
ll val[N],ans[N];
int last,n,p;
int newnode(int w,int c)
{
for(int i=0;i<10;i++)ch[p][i] = 0;
cnt[p] = num[p] = 0;
len[p] = w;val[p]=c;
return p++;
}
void init()
{
p = last = n = 0;
newnode(0,0);
newnode(-1,0);
s[n] = -1;
fail[0] = 1;
}
int getfail(int x)
{
while(s[n-len[x]-1] != s[n]) x = fail[x];
return x;
}
void add(int c)
{
s[++n] = c;
int cur = getfail(last);
if(!ch[cur][c]){
int now = newnode(len[cur]+2,c);
fail[now] = ch[getfail(fail[cur])][c];
ch[cur][c] = now;
num[now] = num[fail[now]] + 1;
}
last = ch[cur][c];
cnt[last]++;
}
void cal()
{
ll pp=0;
for(int i=0;i<p;i++)
{
for(int j=0;j<10;j++)if(ch[i][j])
{
if(ans[i])ans[ch[i][j]]=(ans[i]*10%mod+val[ch[i][j]]+qp(10,1+le[i])*val[ch[i][j]]%mod)%mod,
le[ch[i][j]]=le[i]+2;
else
{
if(len[i]&1)ans[ch[i][j]]=val[ch[i][j]],le[ch[i][j]]=1;
else ans[ch[i][j]]=(val[ch[i][j]]+val[ch[i][j]]*10%mod)%mod,le[ch[i][j]]=2;
}
}
pp+=ans[i];if(pp>=mod)pp-=mod;
}
printf("%lld\n",pp);
}
}pam;
char s[N];
int main()
{
pam.init();
scanf("%s",s);
int n=strlen(s);
for(int i=0;i<n;i++)pam.add(s[i]-'0');
pam.cal();
return 0;
}
/******************** ********************/
上一篇:PHP 5.4.17 发布!


下一篇:比较两个List列表,取得List中不同项返回