HDU3336Count the string

来源:互联网 发布:jquery 1.10.2.min.js 编辑:程序博客网 时间:2024/06/15 13:54
http://acm.hdu.edu.cn/showproblem.php?pid=3336

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


Problem Description
It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. For example:
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.
 

Input
The first line is a single integer T, indicating the number of test cases.
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.
 

Output
For each case, output only one number: the sum of the match times for all the prefixes of s mod 10007.
 

Sample Input
14abab
 

Sample Output
6
 

Author
foreverlin@HNU
 

Source
HDOJ Monthly Contest – 2010.03.06
貌视这个题有bug
题目大意就是求前i(1<=i<=n)个字母出现的次数的和
还是f数组的应用。样例abab      f[4]=2代表长度为4的子串中重复出现的是ab两个字母
所以做题的时候还要用num数组记录长度为f[i]的有几个

#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;
}



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 被鸡爪子抓伤了怎么办 被鸡抓伤肿了怎么办 护士电子化没有激活码怎么办 窗帘盒螺丝掉了怎么办 窗帘的环扣掉了怎么办 门式起重吊装行车脱轨怎么办 在日本丢了东西怎么办 在日本钱包丢了怎么办 被起诉后没钱还怎么办 分期付款卖车打不起车款怎么办 地铁票买多了怎么办 工伤陪护费没有发票怎么办 工伤医疗费报销单位不盖章怎么办 家里的led灯坏了怎么办 吊顶led灯坏了怎么办 客厅空了一面墙怎么办 轨道灯的轨道不够长怎么办 奔驰大灯不亮了怎么办 led顶灯不亮了怎么办 吸顶灯led灯坏了怎么办 车底盘塑料被刮怎么办 汽车门电机坏了怎么办 宁波北仑普高差三分该怎么办 上班的地方甲醛味很重怎么办 公司不给员工交社保怎么办 户口转到学校毕业了怎么办 外地户口转到北京档案怎么办 隧道防水板过紧怎么办 到国企没报到证怎么办 车子锁了油箱盖能开怎么办 单位不接受档案和户口怎么办 完税凭证弄丢了怎么办 育种玉米公本早了怎么办 网银转账打错了怎么办 转账名字打错了怎么办 普通转账名字打错了怎么办 银行账号转错了怎么办 打款信息错了怎么办 报到证过期了档案怎么办 日本suica卡丢了怎么办 日本地铁卡丢了怎么办