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 SS.
It is guaranteed that 1 \le S[i] \le 91≤S[i]≤9, the length of SS is less than 20000002000000.
Output
Print the answer modulo 10000000071000000007.
样例输入1 复制
111111
样例输出1 复制
123456
样例输入2 复制
1121
样例输出2 复制
135
题目来源
ACM-ICPC 2018 南京赛区网络预赛
题意:找出所有的回文串,输出所有独特的回文串的和,
做法:回文树,
#include<bits/stdc++.h>
using namespace std;
const int N = 2e6+100;
const int mod = 1e9+7;
int qp[N];
struct Palindromic_Tree{
int s[N],nex[N][10],fail[N],last,n,cnt[N],len[N],tp;
int ret[N];
int newnode(int x){
for(int i = 0;i< 10;i ++) nex[tp][i] =0;
cnt[tp] = 0;
len[tp] = x;
return tp++;
}
void init(){
tp = n = last = 0;
newnode(0);
newnode(-1);
fail[0] = 1;
s[n] = -1;
}
int get_fail(int x){
while(s[n-len[x]-1] != s[n]) x= fail[x];
return x;
}
void add(int x){
s[++n] = x;
int cur = get_fail(last);
if(!nex[cur][x]){
int np = newnode(len[cur]+2);
fail[np] = nex[get_fail(fail[cur])][x];
nex[cur][x] = np;
}
last = nex[cur][x];
cnt[last]++;
}
int query(){
ret[0] =0,ret[1] = 0;
int ans = 0;
for(int i = 0;i < tp;i ++){
//cout << i <<' '<< ret[i] << endl;
ans += ret[i];
ans %= mod;
for(int j = 0;j < 10;j ++){
if(nex[i][j] == 0) continue;
//cout <<i << ' ' <<j << ' '<<nex[i][j] << endl;
if(i == 1){
ret[nex[i][j]] = j;
}
else{
ret[nex[i][j]] = (1LL*10*ret[i]+1ll*(qp[len[nex[i][j]]-1]+1)*j)%mod;
}
}
}
return ans;
}
}pat;
char st[N];
int main(){
qp[0] = 1;
for(int i = 1;i < N;i ++) qp[i] = 1LL*qp[i-1]*10%mod;
scanf("%s",st);
pat.init();
int len = strlen(st);
for(int i = 0;i < len;i ++){
pat.add(st[i]-'0');
}
printf("%d\n",pat.query());
return 0;
}