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): 17068    Accepted Submission(s): 7721

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
1
4
abab
 
Sample Output
6
 
Author
foreverlin@HNU
 
Source
 
Recommend
lcy
题意:求字符串所有前缀出现的次数(包括本身)
思路:求所有next的值的个数加上前缀本身
 
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <stdio.h>
#include <string.h>
using namespace std;
char a[]; int num; void getnext(char* a, int len , int *next)
{
next[] = - ;
int k = - , j = ;
while(j < len)
{
if(k == - || a[j] == a[k])
{
k++;
j++;
// if(a[j] != a[k])
next[j] = k ;
// else
// {
// next[j] = next[k];
// } }
else
{
k = next[k];
}
}
} int main()
{int n ;
scanf("%d" , &n);
while(n--)
{
int next[];
int l ;
scanf("%d" , &l);
scanf("%s" , a);
getnext(a , l , next);
int j = ;
for(int i = ; i <= l ; i++)
{
// cout << next[i] << " " ;
j = i ;
while(next[j] > )
{
num = (num + ) % ;
j = next[j];
}
}
// cout << endl ; printf("%d\n" , (num + l)%);
num = ;
} return ;
}

最新文章

  1. marquee滚动语法
  2. 【转】SVN管理多个项目版本库
  3. django配置fcgi参数解释
  4. Javascript-回调函数浅谈
  5. 浅谈Objective—C中的面向对象特性
  6. Windows下使用NIF扩展Erlang方法
  7. hadoop1中mapreduce原理详解
  8. OpenCV SIFT原理与源码分析
  9. 甲骨文公司 Oracle
  10. xcode UIView常用方法属性动画
  11. [leetcode-357-Count Numbers with Unique Digits]
  12. [读书笔记] 三、搭建基于Spring boot的JavaWeb项目
  13. 你应该学会的Postman用法
  14. C#服务器端生成报告文档:使用帆软报表生成Word、Pdf报告
  15. React Native Android启动白屏的一种解决方案下
  16. ubuntu16安装squid代理服务器
  17. php将汉字转换为拼音和得到词语首字母(三)
  18. Spring学习笔记--声明一个简单的Bean
  19. 定期删除Azure存储账号下N天之前的数据文件-ARM
  20. c#中的classes和objects一些知识【1】

热门文章

  1. HTML创建链接框
  2. centos7.4安装mysql
  3. EL表达式多条件判断
  4. vue的请求数据方式
  5. 线程池-连接池-JDBC实例-JDBC连接池技术
  6. python基础--匿名函数
  7. python常用函数 V
  8. 【串线篇】spring boot日志使用
  9. 项目中dubbo的标准配置
  10. uiautomator2 使用注意的地方