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. 一致性hash在分布式系统中的应用

    场景 如果要设计一套KV存储的系统,用户PUT一个key和value,存储到系统中,并且提供用户根据key来GET对应的value.要求随着用户规模变大,系统是可以水平扩展的,主要要解决以下几个问题. ...

  2. OpenCV学习(13) 细化算法(1)

    程序编码参考经典的细化或者骨架算法文章: T. Y. Zhang and C. Y. Suen, "A fast parallel algorithm for thinning digita ...

  3. TensorFlowIO操作(三)------图像操作

    图像操作 图像基本概念 在图像数字化表示当中,分为黑白和彩色两种.在数字化表示图片的时候,有三个因素.分别是图片的长.图片的宽.图片的颜色通道数.那么黑白图片的颜色通道数为1,它只需要一个数字就可以表 ...

  4. MySQL服务器安装完之后如何调节性能

    原文作者: Peter Zaitsev原文来源: http://www.mysqlperformanceblog.com/2006/09/29/what-to-tune-in-mysql-server ...

  5. Drupal、IoT 和开源硬件之间的交集

    导读 来认识一下Amber Matz,她是来自 Lullabot Education 旗下的Drupalize.Me的产品经理以及培训师.当她没有倒腾 Arduino.Raspberry Pi 以及电 ...

  6. Jenkins 安装与使用--实例

    參考了博客Jenkins master在windows上安装 Jenkins的主要功能是监视反复工作的运行,比如软件project的构建详细地: *软件的持续构建和測试 本质上提供了一个易于使用的持续 ...

  7. 主从复制时报:ERROR 1794 (HY000): Slave is not configured or failed to initialize properly. You must at least set --server-id to enable either a master or a slave. Additional error messages can be found in t

    centos 6.5 mysql5.7 在从库作stop slave时报: error:ERROR 1794 (HY000): Slave is not configured or failed to ...

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

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

  9. Ruby 和 Python 分析器是如何工作的?

    你好! 我作为一名编写Ruby profiler的先驱,我想对现有的Ruby和Python profiler如何工作进行一次调查. 这也有助于回答很多人的问题:“你怎么写一个profiler?” 在这 ...

  10. CI:关于计算智能

    该书认为,当前的计算智能起源于人工智能和生物智能.人工智能的起源大约可以追溯到50年以前,而计算智能这个术语则仅仅出现于10年以前.计算智能由几个部分组成,即人工智能.模糊集和模糊逻辑.神经网络(有时 ...