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> # ...
随机推荐
- GC算法介绍及工作原理和优缺点
一.GC定义与作用 GC就是垃圾回收机制的简写 GC可以找到内存中的垃圾,并释放和回收空间,GC里的垃圾是什么 如下图所示: GC算法是什么:GC是一种机制,垃圾回收器完成具体的工作 工作的内容就是查 ...
- Nginx 路由转发和反向代理 location 配置
Nginx 配置的三种方式 第一种直接替换 location 匹配部分 第二种 proxy_pass 的目标地址,默认不带 /,表示只代理域名,url 和参数部分不会变(把请求的 path 拼接到 p ...
- 【Linux】E297: Write error in swap file 解决办法
今天登陆到服务器上,发现通过vi 打开文件就会报错: E297: Write error in swap file E303: Unable to open swap file for "c ...
- 【Oracle】regexp_substr()函数详解
环境:Oracle10.2.0.5 在SQL中尝试使用正则 可以试下regexp_substr()来进行分割 首先创建一个实验视图: SQL> create or replace view te ...
- 在recover database时,如何决定该从哪一个SCN开始恢复
使用备份恢复的方法搭建DG库,还原数据文件后,打开数据库时报错 SQL> ALTER DATABASE OPEN READ ONLY; ALTER DATABASE OPEN READ ONLY ...
- 图像分割论文 | DRN膨胀残差网络 | CVPR2017
文章转自:同作者个人微信公众号[机器学习炼丹术].欢迎交流沟通,共同进步,作者微信:cyx645016617 论文名称:'Dilated Residual Networks' 论文链接:https:/ ...
- QT串口助手(二):参数配置
作者:zzssdd2 E-mail:zzssdd2@foxmail.com 一.前言 主要实现功能 串口参数的配置:波特率.数据位.停止位.校验位 本机串口设备的查询与添加显示 串口设备的手动更新与打 ...
- 利用JavaUDPSocket+多线程模拟实现一个简单的聊天室程序
对Socket的一点个人理解:Socket原意是指插座.家家户户都有五花八门的家用电器,但它们共用统一制式的插座.这样做的好处就是将所有家用电器的通电方式统一化,不需要大费周章地在墙壁上凿洞并专门接电 ...
- 2021年【线上】第一性原理vasp技术实战培训班
材料模拟分子动力学课程 3月19号--22号 远程在线课 lammps分子动力学课程 3月12号--15号 远程在线课 第一性原理VASP实战课 3月25号-28号 远程在线课 量子化学Gaussia ...
- Django Signals
信号 Django中提供了"信号调度",用于在框架执行操作时解耦.通俗来讲,就是一些动作发生的时候,信号允许特定的发送者去提醒一些接受者. Django内置的信号 Model si ...