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

HDU 3336 Count the string 查找匹配字符串的更多相关文章

  1. HDU 3336 Count the string(KMP的Next数组应用+DP)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. hdu 3336 Count the string KMP+DP优化

    Count the string Problem Description It is well known that AekdyCoin is good at string problems as w ...

  3. HDU 3336 Count the string(next数组运用)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. hdu 3336:Count the string(数据结构,串,KMP算法)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. hdu 3336 Count the string -KMP&dp

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  6. HDU 3336 Count the string KMP

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...

  7. 【HDU 3336 Count the string】

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  8. HDU 3336——Count the string

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  9. HDU 3336 - Count the string(KMP+递推)

    题意:给一个字符串,问该字符串的所有前缀与该字符串的匹配数目总和是多少. 此题要用KMP的next和DP来做. next[i]的含义是当第i个字符失配时,匹配指针应该回溯到的字符位置. 下标从0开始. ...

随机推荐

  1. Spring核心之IoC——依赖注入

    在J2EE开发平台中,Spring是一种优秀的轻量级企业应用解决方案.Spring倡导一切从实际出发,它的核心技术就是IOC(控制反转)和AOP(面向切面编程)技术.本文用的Spring版本为spri ...

  2. [NPM] Avoid Duplicate Commands by Calling one NPM Script from Another

    We can get a lot of utility through CLI tools invoked via npm scripts. Many of these tools have APIs ...

  3. 给你的webstorm添加快速生成注释得快捷键

    打开File----setting-map-搜搜"fix doc"

  4. angularjs中使用$q.defer

    方法method1和方法method2的区别,我还正在研究中...待添加 代码如下: <html ng-app="myApp"> <head> <ti ...

  5. python3 http.client 网络请求

    python3 http.client 网络请求 一:get 请求 ''' Created on 2014年4月21日 @author: dev.keke@gmail.com ''' import h ...

  6. 〖Linux〗不知谁写的,很实用的Linux命令

    第一部分 . sudo 运行上一条命令 sudo !! . HTTP方式共享当前目录 python -m SimpleHTTPServer . vim保存一个root用户文件 :w !sudo tee ...

  7. java线程同步: synchronized详解(转)

    Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码. 一.当两个并发线程访问同一个对象object中的这个synchronized(this ...

  8. 【HTML入门】Html中块状元素和内联元素解析

    [HTML入门]Html中块状元素和内联元素解析 块元素(block element)一般是其他元素的容器元素,块元素一般都从新行开始,它可以容纳内联元素和其他块元素,内联元素(inline elem ...

  9. [精]Oracle APEX 5.0 入门教程(一) Form表单

    Oracle APEX Tutorial for Beginners (APEX 5.0) 1- Introduction 2- Create Workspace 3- Work with Works ...

  10. Linux-正则表达式的POSIX规范及流派

    Linux/Unix工具与正则表达式的POSIX规范 对正则表达式有基本了解的读者,一定不会陌生『\d』.『[a-z]+』之类的表达式,前者匹配一个数字字符,后者匹配一个以上的小写英文字母.但是如果你 ...