Count the string -- HDOJ 3336
Count the string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3637 Accepted Submission(s): 1689
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.
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.
4
abab
思路:KMP,但要对其进行变形,当找到失败位置时,要继续考察该位置,一直向前找到字符串首不能再向前找,因为我们不只要计算该子串本身,我们还要计算该子串包含的其他子串,因为这些子串都是原串的子串,这是显然的。个人认为属于KMP的进阶应用,不只是模板题,KMP有待更深入的理解。
AC代码:
#include<stdio.h>
#include<string.h>
int fail[];
int sum[];
char str[];
int T, m, max;
void getfail()
{
fail[] = -;
int i, j, temp;
for(i = , j = -; i < m; i ++)
{
while(j >= && str[j + ] != str[i])
{
j = fail[j];
}
if(str[j + ] == str[i])
j ++;
fail[i] = j;
if(j >= )
{
sum[j] ++;
if(max < j)
max = j;
temp = j;
while(temp >= )
{
temp = fail[temp];
sum[temp] ++;
}
}
}
}
int main(int argc, char const *argv[])
{
int i, cnt;
scanf("%d", &T);
while(T--)
{
memset(str, , sizeof(str));
memset(sum, , sizeof(sum));
scanf("%d", &m);
cnt = m % ;
max = ;
getchar();
fgets(str, m+,stdin);
getfail();
for(i = ; i <= max; i ++)
{
/* printf("OK\n"); */
sum[i] %= ;
cnt += sum[i];
cnt %= ;
}
printf("%d\n", cnt);
}
return ;
}
Count the string -- HDOJ 3336的更多相关文章
- (KMP)Count the string -- hdu -- 3336
http://acm.hdu.edu.cn/showproblem.php?pid=3336 Count the string Time Limit: 2000/1000 MS (Java/Other ...
- Count the string - HDU 3336(next+dp)
题目大意:给你一个串求出来这个串所有的前缀串并且与前缀串相等的数量,比如: ababa 前缀串{"a", "ab", "aba", &quo ...
- Count the string HDU - 3336
题意: 求一个字符串的每个前缀在这个字符串中出现次数的加和 解析: 默默的骂一句...傻xkmp..博主心里气愤... 拓展kmp就好多了... 因为拓展kmp每匹配一次 就相当于这些前缀出现了一 ...
- hdoj 3336 Count the string【kmp算法求前缀在原字符串中出现总次数】
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 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) ...
- hdu 3336:Count the string(数据结构,串,KMP算法)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 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 ...
- HDU 3336 Count the string(next数组运用)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3336 Count the string 查找匹配字符串
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- OC的内存管理机制
总的来说OC有三种内存管理机制,下面将分别对这三种机制做简要的概述. 1.手动引用计数(Mannul Reference Counting-MRC) mannul:用手的,手工的. 引用计数:reta ...
- 使用CLK.AspNet.Identity提供以角色为基础的访问控制(RBAC)
使用CLK.AspNet.Identity提供以角色为基础的访问控制(RBAC) 程序代码下载 程序代码下载:点此下载 前言 ASP.NET Identity是微软所贡献的开源项目,用来提供ASP.N ...
- [002] The Perks of Being a Wallflower - 读后记
The Perks of Being a Wallflower 今天(2015年10月30日 18:26:17)读完"The Perks of Being a Wallflower" ...
- PAT_1046 划拳
啦啦啦.今天晚上火车回学校了.= =还是挺舍不得家里的. 题目描述: 划拳是古老中国酒文化的一个有趣的组成部分.酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一个数字.如果谁比划出的数字 ...
- Headfirst设计模式的C++实现——装饰者模式(Decorater)
Beverage.h #ifndef BEVERAGE_H_INCLUDED #define BEVERAGE_H_INCLUDED #include <string> class Bev ...
- linux进程间通信--无名管道
管道 只能用于具有亲缘关系的进程之间通信是一个半双工的通信模式, 具有固定的写读端和写端,管道可以看成一种特殊的文件,对它可以使用普通的read.write等操作 管道的创建: #include &l ...
- canvas径向渐变详解
创建径向渐变步骤如下: 1,创建径向渐变对象 createRadialGradient(x0,y0,r0,x1,y1,r1),其中x0,y0,r0分别为起始圆的位置坐标和半径,x1,y1,r1为终止圆 ...
- shell脚本修复MySQL主从同步
发布:thebaby 来源:net [大 中 小] 分享一例shell脚本,用于修改mysql的主从同步问题,有需要的朋友参考下吧. 一个可以修改mysql主从同步的shell脚本. 例子 ...
- PHP 冒泡排序法
<?php // 冒泡排序法:将一个数组中的值按照从小到大的顺 序排序 $arr = array(33, 1, 4, 5, 2, 3, 7, 9, 8, 99); $len = count($a ...
- shell编程的一些例子5
1.here文档 here文档允许我们调用一个交互式程序:可以从脚本程序中输出大量的文本,从而不必echo每行 例子1: #!/bin/bash cat<<!DATA! This is a ...