Skr(馬拉車+雜湊)
阿新 • • 發佈:2018-12-11
A number is skr, if and only if it's unchanged after being reversed. For example, "12321", "11" and "1" are skr numbers, but "123", "221" are not. FYW has a string of numbers, each substring can present a number, he wants to know the sum of distinct skr number in the string. FYW are not good at math, so he asks you for help.
Input
The only line contains the string of numbers SSS.
It is guaranteed that 1≤S[i]≤91 \le S[i] \le 91≤S[i]≤9, the length of SSS is less than 200000020000002000000.
Output
Print the answer modulo 100000000710000000071000000007.
樣例輸入1
111111
樣例輸出1
123456
樣例輸入2
1121
樣例輸出2
135
#include<bits/stdc++.h> using namespace std; typedef unsigned long long ULL; typedef long long LL; #define rep(i,a,b) for(int i=a;i<b;++i) #define per(i,a,b) for(int i=b-1;i>=a;--i) const int N=2e6+10; const int mod=1e9+7; char s[N]; ULL base[N*2],has[N*2];//auto LL base10[N*2],sum[N*2]; const ULL P=1000007; int fir[N<<1],nxt[N<<1],cnt=0; ULL has_link[N<<1]; int HAS(ULL x){ ULL t=x%P; for(int i=fir[t];i;i=nxt[i]){ if(has_link[i]==x)return 1; } nxt[++cnt]=fir[t]; fir[t]=cnt; has_link[cnt]=x; return 0; } LL solve(int l,int r){ ULL v=has[r]-has[l-1]*base[r-l+1]; if(HAS(v))return 0; LL res=(sum[r]-sum[l-1]*base10[(r-l+2)/2]%mod+mod)%mod; return res; } char Ma[N*2]; int Mp[N*2];//record radius void Manacher(char s[],int len){ int l=0; Ma[l++]='$';//head Ma[l++]='#'; rep(i,0,len){ Ma[l++]=s[i]; Ma[l++]='#'; } Ma[l]=0;//tail base[0]=base10[0]=1; sum[0]=has[0]=0; rep(i,1,l+1){ base[i]=base[i-1]*P,has[i]=has[i-1]*P+Ma[i]; base10[i]=base10[i-1]*10%mod; if(Ma[i]>='0'&&Ma[i]<='9') sum[i]=(sum[i-1]*10+Ma[i]-'0')%mod; else sum[i]=sum[i-1]; } int mx=0,id=0;//record max_pos LL ans=0; rep(i,1,l){ if(Ma[i]!='#')ans=(ans+solve(i,i))%mod; Mp[i]=mx>i?min(Mp[2*id-i],mx-i):1; while(Ma[i+Mp[i]]==Ma[i-Mp[i]]){ if(Ma[i+Mp[i]]!='#')ans=(ans+solve(i-Mp[i],i+Mp[i]))%mod; ++Mp[i];//exceed mx } if(i+Mp[i]>mx){ mx=i+Mp[i]; id=i; } } printf("%lld\n",ans); } int main(){ scanf("%s",s); int len=strlen(s); Manacher(s,len); return 0; }