Wordpress主题 仿魅族,seo技术分享免费咨询,甘肃 网站建设,工程建筑网Description 求一个字符串的所有前缀在串中出现的次数之和 Input 多组用例#xff0c;每组用例占一行为一个长度不超过100000的字符串#xff0c;以文件尾结束输入 Output 对于每组用例#xff0c;输出该字符串的所有前缀在串中出现的次数之和#xff0c;结果模256 Samp…Description 求一个字符串的所有前缀在串中出现的次数之和 Input 多组用例每组用例占一行为一个长度不超过100000的字符串以文件尾结束输入 Output 对于每组用例输出该字符串的所有前缀在串中出现的次数之和结果模256 Sample Input aaa abab Sample Output 6 6 Solution 首先我们知道next数组中next[i]表示的是以第i个字符结尾的前缀中最长公共前后缀的长度即从s[0]到s[Next[i]-1]与s[i-Next[i]]到s[i-1]这一点的字符串是完全重合的。dp[i]表示表示以i结尾的字符串的所有前缀出现次数之和。那么显然有dp[i]dp[next[i]]1,求出dp数组后累加即为答案 Code
#include stdio.h
#include string.h
const int N200010;
const int mod10007;
char s[N];
int next[N],len;
void getNext(){int i0,j-1;next[0]-1;while(ilen){if(j-1||s[i]s[j]){i;j;next[i]j;}else jnext[j];}
}
int main(){int t,i;scanf(%d,t);while(t--){scanf(%d,len);scanf(%s,s);getNext();int res0,pos;for(i1;ilen;i){posi;while(pos){res(res1)%mod;posnext[pos];}}printf(%d\n,res);}return 0;
}