HDU 3336——Count the string
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.
InputThe 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.
OutputFor 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
题意:找出来给出的字符串里面所有前缀在字符串中出现的个数
解法一:
这道题仔细一想和POJ-2752 Seek the Name, Seek the Fame很相似,2752这一道题目是求前缀和后缀的所有相同的长度
比如:
ababab
前缀和后缀相同的长度有4、2
这一道题就是让我们求前缀的在这个串中个数,那我们可以像2752这一道题一样,先找出来整个串相同前后缀的所有类型,在把串的长度依次递减,在对他求出来前后缀所有类型,很nice!
代码:
1 #include<stdio.h>
2 #include<string.h>
3 #include<iostream>
4 #include<algorithm>
5 using namespace std;
6 const int maxn=200005;
7 const int INF=0x3f3f3f3f;
8 const int mod=10007;
9 char str[maxn];
10 void get_next(int len,int *next)
11 {
12 next[0]=-1;
13 int k=-1;
14 for(int i=1;i<len;++i)
15 {
16 while(k>-1 && str[k+1]!=str[i])
17 k=next[k];
18 if(str[k+1]==str[i]) k+=1;
19 next[i]=k;
20 }
21 }
22 int main()
23 {
24 int t;
25 scanf("%d",&t);
26 while(t--)
27 {
28 int len;
29 scanf("%d",&len);
30 scanf("%s",str);
31 int next[len];
32 get_next(len,next);
33 int ans=0;
34 for(int i=len;i>0;--i)
35 {
36 int k=next[i-1];
37 while(k>=0)
38 {
39 k=next[k];
40 ans++;
41 }
42 ans%=mod;
43 }
44 ans+=len;
45 printf("%d\n",ans%mod);
46 }
47 return 0;
48 }
解法二:
例如:
ababab
我们知道他的相同前后缀的长度为4、2
当为4的时候分别为(1-4)==(3-6)那么此时的(1-3)==(3-5)是不是也是一种前缀在字符串中重复了一次(注意:这里的数字代表字符串下标,从1开始)
同理(1-2)==(3-4)且(1-1)==(3-3)
那么可以说加上了4,即next[6]
此时我们再看相同前后缀为2的时候,这个时候这个2都已经包含在了4里面,所以我们要注意只有next[i]!=next[i-1]+1的时候才可以直接加上next[i]
还不要忘记了最后加上一个字符串长度,因为前缀本身还没有计算在内
代码:
1 #include<stdio.h>
2 #include<string.h>
3 #include<iostream>
4 #include<algorithm>
5 using namespace std;
6 const int maxn=200005;
7 const int INF=0x3f3f3f3f;
8 const int mod=10007;
9 char str[maxn];
10 void get_next(int len,int *next)
11 {
12 next[0]=-1;
13 int k=-1;
14 for(int i=1;i<len;++i)
15 {
16 while(k>-1 && str[k+1]!=str[i])
17 k=next[k];
18 if(str[k+1]==str[i]) k+=1;
19 next[i]=k;
20 }
21 }
22 int main()
23 {
24 int t;
25 scanf("%d",&t);
26 while(t--)
27 {
28 int len;
29 scanf("%d",&len);
30 scanf("%s",str);
31 int next[len];
32 get_next(len,next);
33 int ans=next[len-1]+len+1;
34 for(int i=0;i<len-1;++i)
35 {
36 if(next[i]>=0 && next[i+1]!=next[i]+1)
37 ans=ans+next[i]+1;
38 ans%=mod;
39 }
40 printf("%d\n",ans%mod);
41 }
42 return 0;
43 }
HDU 3336——Count the string的更多相关文章
- 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+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) ...
- 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
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- HDU 3336 Count the string KMP
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...
- 【HDU 3336 Count the string】
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- hdu 3336 Count the string(思维可水过,KMP)
题目 以下不是KMP算法—— 以下是kiki告诉我的方法,好厉害的思维—— 就是巧用标记,先标记第一个出现的所有位置,然后一遍遍从标记的位置往下找. #include<stdio.h> # ...
随机推荐
- 【Markdown】使用方法与技巧
Markdown使用方法与技巧 前言 注意到Github上经常含有.md格式的文件,之后了解到这个是用Markdown编辑后生成的文件.Markdown语言用途广泛,故学之. 简介 Markdow ...
- Invalid bound statement (not found): com.xxx.xxx.dao.ShopMapper.insertShop
mybatis在编写完SQL,进行测试的时候出现了错误,显示 org.apache.ibatis.binding.BindingException: Invalid bound statement ( ...
- 在阿里云托管的k8s上使用nas做动态存储
前言 关于aliyun托管k8s的存储插件主要有两种: CSI # kubectl get pod -n kube-system | grep csi-plugin csi-plugin-8bbnw ...
- XSS-labs通关挑战(xss challenge)
XSS-labs通关挑战(xss challenge) 0x00 xss-labs 最近在看xss,今天也就来做一下xss-labs通过挑战.找了好久的源码,终于被我给找到了,因为在GitHub上 ...
- Java 在pom.xml中配置build resources, 来防止我们资源导出失败问题(Maven项目)
在pom.xml中配置build, 来防止我们资源导出失败问题 <!--在build中配置resources, 来防止我们资源导出失败问题--> <build> <res ...
- linux之平均负载(学习笔记非原创)
什么是平均负载 [root@111 ~]# uptime 11:03:33 up 149 days, 17:34, 1 user, load average: 0.08, 0.05, 0.01 最后三 ...
- 【Linux】如何查看命令来源于哪个包
Debian:(Ubuntu等) 先安装apt-file sudo apt-get install -y apt-file apt-file update 查询命令:(已查询ifconfig为例) r ...
- Upload - Labs (上)
Pass - 01: 1.尝试上传一个php文件:aaa.php,发现只允许上传某些图片类型,用bp抓包,发现http请求都没通过burp就弹出了不允许上传的提示框,这表明验证点在前端,而不在服务端 ...
- pandas DataFrame的新增行列,修改、删除、筛选、判断元素以及转置操作
1)指定行索引和列索引标签 index 属性可以指定 DataFrame 结构中的索引数组, columns 属性可以指定包含列名称的行, 而使用 name 属性,通过对一个 DataFrame 实 ...
- 基于FPGA的光口通信开发案例|基于Kintex-7 FPGA SFP+光口的10G UDP网络通信开发案例
前言 自著名华人物理学家高锟先生提出"光传输理论",实用化的光纤传输产品始于1976年,经历了PDH→SDH→DWDM→ASON→MSTP的发展历程.本世纪初期,ASON/OADM ...