题目链接:https://nanti.jisuanke.com/t/30998
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
题目来源
题意:求所有的数字回文子串和。
解题思路:回文树裸题,先把回文树建出来,我们知道,回文树每个节点即代表一个回文子串。树上边转移的时候,可以很好地这算这个子串所代表的数字,不用每次都把子串的每一位给枚举一遍。
当前节点的所代表的数字=当前添加的数字*pow(10,当前回文串长度-1) +他父亲的数字*10+当前添加的数字
如
22 -----> 1221
可以很好地计算。
因此,直接深搜一遍回文树就好了。注意分奇数偶数节点。
#include<iostream>
#include<string.h>
using namespace std;
typedef long long ll;
const int MAXN=2005005;
const ll MOD=1000000007ll;
ll pow(ll a,ll b){
ll t,y;
t=1; y=a;
while (b!=0){
if (b&1==1) t=t*y%MOD;
y=y*y%MOD; b=b>>1;
}
return t;
}
int len[MAXN];
int nxt[MAXN][15];
int fail[MAXN];
int num[MAXN];
int cnt[MAXN];
int last;
int S[MAXN];
int tot;
int N;
int new_node(int l){
cnt[tot]=0;
num[tot]=0;
len[tot]=l;
return tot++;
}
void init_tree(){
tot=0;
new_node(0);
new_node(-1);
last=0;
N=0;
S[N]=-1;
fail[0]=1;
}
int get_fail(int x){
while(S[N-len[x]-1]!=S[N])
x=fail[x];
return x;
}
void add_char(int c){
c-='0';
S[++N]=c;
int cur=get_fail(last);
if(!nxt[cur][c]){
int now=new_node(len[cur]+2);
fail[now]=nxt[get_fail(fail[cur])][c];
nxt[cur][c]=now;
num[now]=num[fail[now]]+1;
}
last=nxt[cur][c];
cnt[last]++;
}
ll jans=0;
ll oans=0;
void dfs1(int x,ll fa){
for(int i=1;i<=9;i++){
if(nxt[x][i]){
ll cur;
if(len[nxt[x][i]]==1){
jans+=i;
cur=i;
jans%=MOD;
}
else{
cur=i*pow(10,(len[nxt[x][i]]-1))%MOD+i+fa*10%MOD;
jans=(jans+cur%MOD)%MOD;
jans%=MOD;
}
dfs1(nxt[x][i],cur%MOD);
}
}
}
void dfs2(int x,ll fa){
for(int i=1;i<=9;i++){
if(nxt[x][i]){
ll cur;
cur=i*pow(10,(len[nxt[x][i]]-1))%MOD+i+fa*10%MOD;
oans=(oans+cur%MOD)%MOD;
dfs2(nxt[x][i],cur%MOD);
}
}
}
char str[MAXN];
int main(){
scanf("%s",str);
int N1 = strlen(str);
init_tree();
for(int i=0;i<N1;i++)
add_char(str[i]);
dfs1(1,0);
dfs2(0,0);
printf("%lld\n",(jans%MOD+oans%MOD)%MOD);
return 0;
}