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> # ...
随机推荐
- 深入理解MySQL索引(下)
先创建一个T表. mysql> create table T ( ID int primary key, k int NOT NULL DEFAULT 0, s varchar(16) NOT ...
- 基于腾讯云存储网关 CSG 实现视频在线转码分发
一.背景 随着越来越多的传统业务云化和云端业务发展,数据上云和云端数据处理领域的需求爆发式增长.腾讯云存储网关CSG提供一键部署开箱即用的便捷模式,深度结合COS对象存储生态,为用户提供方便快捷的数据 ...
- XSS - Labs 靶场笔记(上)
上周在网上看到的一个XSS平台,刷一波<doge Less - 1: 1.进入主界面,由图二可知是GET请求,提交name=test,回显在页面 2.查看源代码可知 没有做任何过滤,显然存在反射 ...
- 前端面试准备笔记之JavaScript(03)
01. 变量声明提升 在预解析的时候,成员变量和函数,被提升到最高的位置,方便其他程序访问. 可以先使用后声明. 只提升变量名,不提升变量值 let const 声明的变量不具有变量声明提升. // ...
- 無法直接連接互聯網,需要使用代理時(Scrapy)
在windows系統中,如果無法直接連接互聯網,需要使用代理時該怎麽做呢? 1. 在powershell中設置proxy 背景:使用公司電腦,無法直接訪問互聯網,想要訪問互聯網就得使用代理,但是在控制 ...
- LVS负载均衡理论以及算法概要
一. LVS简介 LVS是Linux Virtual Server的简称,也就是Linux虚拟服务器, 由章文嵩博士发起的自由软件项目,它的官方站点是www.linuxvirtualserver.or ...
- https://www.cnblogs.com/wclwcw/p/7515515.html
https://www.cnblogs.com/wclwcw/p/7515515.html
- Salt (cryptography)
Salt (cryptography) Here is an incomplete example of a salt value for storing passwords. This first ...
- oracle创建表并加索引
一个语句创建Oracle所有表的序列 -- 动态创建序列 2 declare 3 cursor c_job is 4 select TABLE_NAME from user_tables; 5 6 c ...
- try-catch 异常捕获学习
#include <iostream> #include <string> #include <vector> #include <stdexcept> ...