标签:前缀 ase nbsp sample efi bin https letter int
题目链接:https://vjudge.net/contest/70325#problem/K
题目:
1 4 ababSample Output
6
题意:给你一个字符串,求出所有前缀的个数和,可以重叠
思路:首先前缀个数就是字符串长度len,然后利用kmp求next数组,如果next[n]为4,对应前后缀最长相等的部分为abcd,那么a,ab,abc,abcd,都要加一次就是加next【n】,所以这题就是相当于
求next数组,只要满足next[i]+1!=next[i+1]就行,就可以找到所有前缀的个数和了
// // Created by HJYL on 2019/8/16. // #include <iostream> #include <vector> #include <map> #include <string> #include <queue> #include <stack> #include <set> #include <algorithm> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> using namespace std; typedef long long ll; const int maxn=1e6+10; char str[maxn]; int nextt[maxn]; void getnext() { int i=0,j=-1; nextt[0]=-1; int len=strlen(str); while(i<len) { if(j==-1||str[i]==str[j]) { i++,j++; //if(str[i]!=str[j]) nextt[i]=j; // else // nextt[i]=nextt[j]; } else j=nextt[j]; } } int main() { //freopen("C:\\Users\\asus567767\\CLionProjects\\untitled\\text","r",stdin); int T; scanf("%d",&T); int n; while(T--) { scanf("%d",&n); getchar(); scanf("%s",str); int len=strlen(str); //cout<<"len="<<len<<endl; getnext(); int res=len+nextt[len]; for(int i=0;i<len;i++) { if(nextt[i]+1!=nextt[i+1]&&nextt[i]>0) res+=nextt[i]; } printf("%d\n",res%10007); } return 0; }
[kuangbin带你飞]专题十六 KMP & 扩展KMP & ManacherK - Count the string HDU - 3336(前缀数量问题)
标签:前缀 ase nbsp sample efi bin https letter int
原文地址:https://www.cnblogs.com/Vampire6/p/11366768.html