HDU3336Count the string
来源:互联网 发布:jquery 1.10.2.min.js 编辑:程序博客网 时间:2024/06/15 13:54
Count the string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5060 Accepted Submission(s): 2380
s: "abab"
The prefixes are: "a", "ab", "aba", "abab"
For each prefix, we can count the times it matches in s. So we can see that prefix "a" matches twice, "ab" matches twice too, "aba" matches once, and "abab" matches once. Now you are asked to calculate the sum of the match times for all the prefixes. For "abab", it is 2 + 2 + 1 + 1 = 6.
The answer may be very large, so output the answer mod 10007.
For each case, the first line is an integer n (1 <= n <= 200000), which is the length of string s. A line follows giving the string s. The characters in the strings are all lower-case letters.
14abab
6
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 10007
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
bool cmp(int a,int b){
return a>b;
}
char p[200005];
int f[200005];
int num[200005];
void getFail(char* p, int* f,int n)
{
int m = n;
f[0] = 0;
f[1] = 0;
for (int i = 1; i < m; i++)
{
int j = f[i];
while (j && p[i] != p[j])
{
j = f[j];
}
f[i + 1] = p[i] == p[j] ? j + 1 : 0;
}
}
int sum;
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t,m;
cin>>t;
while(t--)
{
cle(num);
cin>>m;
scanf("%s",p);
getFail(p,f,m);
for(int i=1;i<=m;i++)
{
num[f[i]]=(num[f[i]]+1)%mod;///num[f[i]]++;
}
sum=0;
for(int i=1;i<=m;i++)
{
if(num[i]!=0)
sum=(sum+1+num[i])%mod;///加上它本身
else sum=(sum+1)%mod;
}
printf("%d\n",sum);
}
return 0;
}
- hdu3336Count the string
- hdu3336Count the string
- hdu3336Count the string kmp
- HDU3336Count the string
- hdu3336Count the string
- HDU3336Count the string
- HDU3336Count the string(KMP+动态规划)
- hdu3336Count the string(kmp的next的使用
- hdu4552怪盗基德的挑战书&hdu3336Count the string【后缀数组求各前缀重复次数和】
- hdu3336Count the string(KMP的next[]数组的应用+KMP模板)
- The String |
- The String ||
- the length of the string
- The C++ String Class
- the format control string
- hdu3336 Count the string
- hdu3336 Count the string
- HDU3336:Count the string
- ZOJ1091 Knight Moves
- CF 275.5 A. SwapSort
- CF 277.5 B. BerSU Ball
- HDU 2594
- HDU1686Oulipo
- HDU3336Count the string
- HDU 4707Pet
- CF 253A. Anton and Letters
- CF 253B. Kolya and Tandem Repeat
- cust 学校选拔赛
- CF 280A. Vanya and Cubes(div2)
- CF 280B. Vanya and Lanterns(div2)
- CF 280C. Vanya and Exams(div2)
- C语言qsort的三种使用