Count the string

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4105    Accepted Submission(s): 1904

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
 讲解算法:kmp算法,节省的时间源于没有结果的循环和比较;ababcababb
 例如字符串 a  b  a  b  c  a  b  a b b
                                           //首先记录的是  第一个字符"a"出现的位置:其他的就不用再进行循环比较了;此时count  就为 4 了;保存位置0,2,5,7
                0    2      5    7      //然后在第一个已经匹配的位置(0,2,5,7)进行下一个"b"的(第二个)匹配:匹配后;保存位置1, 3, 6, 8;cout+=4;
                0  1           5  6         //然后在第二次保存的位置上(1,3,6,8) 进行下一个字符"a"的(第三个)匹配:匹配后保存位置2,7          ;cout+=2;
                0  1  2      5  6  7     // 然后在第三次保存的位置上(2,7)      进行下一个字符"b"的(第四个)匹配:保存位置3,8;                cout+=2;
                0  1  2    5  6  7 8     //然后寻找在第四次的位置上(3,8)       进行下一个字符"c"的(第五个)匹配:保存位置4;                    cout+=1;
                .........         5  6  7 8 9  //然后就只有一轮能匹配了                                           cout+=5;
                                                   //共 18 个匹配;  明白否,呵呵
 #include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define MOD 10007
char s[];
int a[];
int main()
{
int i, j, n, t; cin>>t;
while(t--)
{
cin>>n;
scanf("%s", s);
int L = ;
for(i = ; s[i]; i++)
{
if(s[i] == s[]) a[L++] = i;//首先记录第一个字符出现的位置
}
int count = L, X = ;
cout<<count<<endl;
for(i = ; s[i]; i++)
{
X = ;
for(j = ; j < L; j++)//直接比较已经匹配的上一个位置,是否匹配下一个字符
{
if(s[a[j]+] == s[i])
{
count += ;
count %= MOD;
a[X++] = a[j] + ;
}
}
L = X;
}
printf("%d\n", count);
}
return ;
}

最新文章

  1. curl命令
  2. CSS3详解:transform
  3. SQL行转列汇总
  4. angularjs2 学习笔记(四) 路由
  5. django: db howto - 1
  6. Java I/O theory in system level
  7. MVC+Bootstrap设计
  8. Angular2开发拙见——组件规划篇
  9. php中的多条件查询
  10. MacOS下Rails+Nginx+SSL环境的搭建(中)
  11. Appium环境搭建——安卓真机调试注意点
  12. vw, vh视区覆盖和自适应图片
  13. 2017.4.9 函数式编程FP
  14. jenkins权限配置不对导致jenkins无法登陆
  15. 压力测试工具JMeter入门教程&lt;转&gt;
  16. printf(&quot;%f\n&quot;,5);
  17. java 编程技巧
  18. C# CRC16 查表法
  19. 4星|《财经》2018年第15期:电动飞机、无人小飞机、AI无人机
  20. Linux 下的jdk安装

热门文章

  1. Fragment 简介 基础知识 总结 MD
  2. 项目笔记:创建XML文件和导出功能
  3. Eclipse 构建Maven项目--普通web项目 复制另外一个项目的配置文件导致的问题
  4. [Python爬虫] 之七:selenium webdriver定位不到元素的五种原因及解决办法(转载)
  5. C# 将Dictionary,StringDictionary等集合数据绑定到如comboBox等控件数据源中将获取健值
  6. Cocos2d-x -- 图片菜单按钮
  7. TP框架中session操作
  8. 关于索引的sql语句优化之降龙十八掌
  9. Python 命令行输出的颜色设置
  10. Patterns-Proxy