It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. For example:
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的更多相关文章

  1. 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) ...

  2. 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 ...

  3. HDU 3336 Count the string(next数组运用)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. HDU 3336 Count the string 查找匹配字符串

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. hdu 3336:Count the string(数据结构,串,KMP算法)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. 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 ...

  7. HDU 3336 Count the string KMP

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...

  8. 【HDU 3336 Count the string】

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  9. hdu 3336 Count the string(思维可水过,KMP)

    题目 以下不是KMP算法—— 以下是kiki告诉我的方法,好厉害的思维—— 就是巧用标记,先标记第一个出现的所有位置,然后一遍遍从标记的位置往下找. #include<stdio.h> # ...

随机推荐

  1. 【Java基础】数组和算法

    数组和算法 查找算法 线性查找 ... 二分查找 二分查找要求数据结构是有序的. package com.parzulpan.java.ch03; /** * @Author : parzulpan ...

  2. 【JDBC核心】JDBC 概述

    JDBC 概述 数据的持久化 持久化(persistence):把数据保存到可掉电式存储设备中以供之后使用.大多数情况下,特别是企业级应用,数据持久化意味着将内存中的数据保存到硬盘上加以"固 ...

  3. 【SpringBoot1.x】 Docker

    SpringBoot1.x Docker 核心概念 Docker 是一个开源的应用容器引擎,是一个轻量级容器技术.Docker 支持将软件编译成一个镜像,然后在镜像中各种软件做好配置,将镜像发布出去, ...

  4. python函数3-函数嵌套/递归/匿名函数

    2 .函数递归: 3.匿名函数

  5. Python使用Protobuf&&如何赋值&&如何正反序列化

    前言 使用protobuf主要是两个步骤,序列化和反序列化. 关于Proto有哪些数据类型,然后如何编写,此处就不赘述了,百度一下有很多. 此文主要是总结,python使用protobuf的过程,如何 ...

  6. html2canvas canvas webgl 截图透明空🤣

    1. React用这个插件html2canvas完成div截图功能,div里面嵌套canvas,返回base64是透明图片. html2canvas(document.getElementById(& ...

  7. php 换行符

    PHP 中换行可以用 PHP_EOL 来替代,以提高代码的源代码级可移植性: unix系列用 \n windows系列用 \r\n mac用 \r 总结:在一些大文本域中换行的文本可以用这个来进行切割 ...

  8. typora+PicGo+gitee搭建免费的的床

    一.gitee 1.第一步拥有自己的gitee账号 没有的可以自己去注册gitee地址 2.使用自己的gitee账号创建仓库 创建好之后注意 记住.com/以后的地址 此处就为y***L/photo- ...

  9. 07. struts2中对Action的管理方式

    web.xml配置文件的常用代码 <filter> <filter-name>struts2</filter-name> <filter-class>o ...

  10. 【转载】HTTP 协议详细介绍

    背景 当你在浏览器地址栏敲入"http://www.cnblogs.com/",然后猛按回车,呈现在你面前的,将是博客园的首页了(这真是废话,你会认为这是理所当然的).作为一个开发 ...