Count the string

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4105    Accepted Submission(s): 1904

Problem Description
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.
 
Input
The 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.
 
Output
For 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
6
 讲解算法:kmp算法,节省的时间源于没有结果的循环和比较;ababcababb
 例如字符串 a  b  a  b  c  a  b  a b b
                                           //首先记录的是  第一个字符"a"出现的位置:其他的就不用再进行循环比较了;此时count  就为 4 了;保存位置0,2,5,7
                0    2      5    7      //然后在第一个已经匹配的位置(0,2,5,7)进行下一个"b"的(第二个)匹配:匹配后;保存位置1, 3, 6, 8;cout+=4;
                0  1           5  6         //然后在第二次保存的位置上(1,3,6,8) 进行下一个字符"a"的(第三个)匹配:匹配后保存位置2,7          ;cout+=2;
                0  1  2      5  6  7     // 然后在第三次保存的位置上(2,7)      进行下一个字符"b"的(第四个)匹配:保存位置3,8;                cout+=2;
                0  1  2    5  6  7 8     //然后寻找在第四次的位置上(3,8)       进行下一个字符"c"的(第五个)匹配:保存位置4;                    cout+=1;
                .........         5  6  7 8 9  //然后就只有一轮能匹配了                                           cout+=5;
                                                   //共 18 个匹配;  明白否,呵呵
 #include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define MOD 10007
char s[];
int a[];
int main()
{
int i, j, n, t; cin>>t;
while(t--)
{
cin>>n;
scanf("%s", s);
int L = ;
for(i = ; s[i]; i++)
{
if(s[i] == s[]) a[L++] = i;//首先记录第一个字符出现的位置
}
int count = L, X = ;
cout<<count<<endl;
for(i = ; s[i]; i++)
{
X = ;
for(j = ; j < L; j++)//直接比较已经匹配的上一个位置,是否匹配下一个字符
{
if(s[a[j]+] == s[i])
{
count += ;
count %= MOD;
a[X++] = a[j] + ;
}
}
L = X;
}
printf("%d\n", count);
}
return ;
}

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(数据结构,串,KMP算法)

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

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

  6. HDU 3336 Count the string KMP

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

  7. 【HDU 3336 Count the string】

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

  8. HDU 3336——Count the string

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  9. HDU 3336 - Count the string(KMP+递推)

    题意:给一个字符串,问该字符串的所有前缀与该字符串的匹配数目总和是多少. 此题要用KMP的next和DP来做. next[i]的含义是当第i个字符失配时,匹配指针应该回溯到的字符位置. 下标从0开始. ...

随机推荐

  1. [Android Pro] Android 必知必会-使用 supportV4 的 RoundedBitmapDrawable 实现圆角

    RoundedBitmapDrawable 是 supportV4 下的一个类,有了它,显示圆角和圆形图片的情况下就不需要额外的第三方类库了,还能和各种图片加载库配合使用. 背景 今天无意间看到一段实 ...

  2. django的过滤和搜索排序功能django-filter

    参考: 1.https://django-filter.readthedocs.io/en/master/guide/usage.html#the-filter 2.https://www.cnblo ...

  3. DICOMDIR结构

    DICOMDIR文件结构: 格式: DICOMDIR文件遵照DICOM文件的存储格式,需要有128个无意义字节的前言(假如没有特殊要求,这些字节都为00H),紧接着是DICOM前缀(长度为4字节的“D ...

  4. Orchard运用 - 学习资源分享

    这段时间研究Orchard,看到一些资源十分有用. 比如国内还是有些牛人在分享心得,在此也广播一下给新手: JustRun对Orchard的一些代码分析 Orchard源码分析 Orchard中文社区 ...

  5. metal2的四个新features

    https://developer.apple.com/documentation/metal/mtldevice/ios_and_tvos_devices/about_gpu_family_4 Im ...

  6. Tensorflow 之模型内容可视化

    TensorFlow模型保存和提取方法 1. tensorflow实现 卷积神经网络CNN:Tensorflow实现(以及对卷积特征的可视化) # 卷积网络的训练数据为MNIST(28*28灰度单色图 ...

  7. javascript刷新父页面方法总结

    用iframe.弹出子页面刷新父页面iframe <script language=JavaScript> parent.location.reload(); </script> ...

  8. IOS NSNotification Center 通知中心的使用

    通知中心,它是IOS程序内部的一种消息广播机制,通过它,可以实现无引用关系的对象之间的通信.通知中心他是基于观察者模式,它只能进行程序内部通信,不能跨应用程序进程通信.当通知中心接受到消息后会根据设置 ...

  9. Google帮助IE浏览器实现对SVG支持

    可缩放矢量图形(SVG)的意识就是一个用于描述二维矢量图形的一种开放图形格式. SVG现在已经能够广泛得应用到许多的项目当中,包括KDE和维基百科等.但是 Internet Explorer浏览器的内 ...

  10. C++ 踩内存

    1.从上往下,栈在堆上面(记忆方法:站在堆上面),二者向里压缩,也就是说,栈地址减少,堆地址增加.栈顶是小地址. 2.模拟踩内存,让程序崩溃.代码如下: int main(int argc, char ...