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) ...
随机推荐
- 12_注解04_注解实现Action调用Service,Service调用Dao的过程
[工程截图] [PersonDao.java] package com.HigginCui.annotation; public interface PersonDao { public void s ...
- (LightOJ 1004) Monkey Banana Problem 简单dp
You are in the world of mathematics to solve the great "Monkey Banana Problem". It states ...
- 使用 Virtual Box 安装 android x86
1.安装 跟随别人的教程:http://www.maketecheasier.com/run-android-4-3-in-virtualbox/ 2.问题 安装过程出现以下问题:Kernel pan ...
- ubuntu server修改时区
公司用的是ubuntu server 服务器在美国亚马逊VPS 现在要修改时区 执行:tzselect 或直接修改 /etc/timezone 文件,我是改成(America/Los_Angeles) ...
- jQuery EasyUI tree中state属性慎用
EasyUI 1.4.4 tree控件中,如果是叶子节点,切忌把state设置为closed,否则该节点会加载整个tree,形成死循环 例如: json入下: [ { "checked&qu ...
- H5小内容(六)
Web Worker 基本内容 单线程与多线程 Worker可以模拟多线程的效果 定义 - 运行在后台的javascript 注意 - 不能使用DOM ...
- yum使用详细
1.使用YUM查找软件包 命令:yum search~ 2.列出所有可安装的软件包 命令:yum list 3.列出所有可更新的软件包 命令:yum list updates 4.列出所有已安装的软件 ...
- DataGridView出现大红叉--在使用多线程访问数据源时
datagridview 的数据源操作在一个方面里面处理 不要多个地方处理 并且处理的时候要加锁 红叉 应该是多线程操作出现的. try catch 只是起到 捕获异常的功能,但是一旦出现了这种错误 ...
- 在后台代码中引入XAML的方法
本文将介绍三种方法用于在后台代码中动态加载XAML,其中有两种方法是加载已存在的XAML文件,一种方法是将包含XAML代码的字符串转换为WPF的对象. 一.在资源字典中载入项目内嵌资源中的XAML文件 ...
- ACM竞赛常用STL(二)之STL--algorithm
<algorithm>无疑是STL 中最大的一个头文件,它是由一大堆模板函数组成的.下面列举出<algorithm>中的模板函数: adjacent_find / binary ...