Xtreme9.0 - Pattern 3 KMP
Pattern 3
题目连接:
https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/car-spark
Description
Vangelis the bear received a digital signal pattern generator that his brother Mitsos built. The generator produces a signal that is encoded using the Latin alphabet. Vangelis starts the generator for some time and records the signal generated. He wants to study the sample he received and try to identify the smallest pattern that the generator could be using to generate the sample.
Your task is to help Vangelis by writing a program that will calculate the length of the smallest valid pattern.
Input
The input is made up of multiple test cases.
The first line contains an integer T (1 <= T <= 10), the number of test cases in this file.
Each line contains an encoded signal. The signal is encoded using the small letters of the Latin alphabet. The length of a signal is between 1 and 106 characters, inclusive.
Vangelis has started the recording at the beginning of a pattern, so each line begins with the first character in the pattern. His recording lasts for at least one pattern length, but the length of the recording may not be an exact multiple of the pattern length.
Output
There must be T lines of output and each line will contain a single non-negative integer number, the length of the minimum valid pattern.
Sample Input
6
abab
abababababababababab
abababababab
abc
aaaaaa
aabaabbaabaabbaabaabbaabaab
Sample Output
2
2
2
3
1
7
Hint
题意
求最小循环节的大小
题解
KMP裸题
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
char s[maxn];
int p[maxn];
int main()
{
int t;scanf("%d",&t);
while(t--){
scanf("%s",s+1);
int len=strlen(s+1);
int j=0;
for(int i=2;i<=len;i++)
{
while(j>0&&s[j+1]!=s[i])
j=p[j];
if(s[j+1]==s[i])
j++;
p[i]=j;
}
int tmp=0;
int first=1;
printf("%d\n",len-p[len]);
}
}
Xtreme9.0 - Pattern 3 KMP的更多相关文章
- Xtreme9.0 - Communities 强连通
Xtreme9.0 - Communities 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/c ...
- Xtreme9.0 - Light Gremlins 容斥
Xtreme9.0 - Light Gremlins 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenge ...
- IEEEXtreme Practice Community Xtreme9.0 - Digit Fun!
Xtreme9.0 - Digit Fun! 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/di ...
- NoReverseMatch at /salesman/zhuce/ Reverse for '/zhuce/' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
NoReverseMatch at /salesman/zhuce/ Reverse for '/zhuce/' with arguments '()' and keyword arguments ' ...
- Django Reverse for 'artic_post' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Reverse for 'home' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: [] ...
- 经典递归问题:0,1背包问题 kmp 用遗传算法来解背包问题,hash表,位图法搜索,最长公共子序列
0,1背包问题:我写笔记风格就是想到哪里写哪里,有很多是旧的也没删除,代码内部可能有很多重复的东西,但是保证能运行出最后效果 '''学点高大上的遗传算法''' '''首先是Np问题的定义: npc:多 ...
- 模式串 从 0 开始的KMP算法
/** * 看了 b站视频 BV1jb411V78H 对KMP有了一点理解,然后我写了这个代码 * 这个代码和视频里面的有一点不同,字符串是从 0 开始的,而不是从1 开始的 * 希望能够帮到学习KM ...
- Xtreme9.0 - Block Art 线段树
Block Art 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/block-art Descr ...
- Xtreme9.0 - Taco Stand 数学
Taco Stand 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/taco-stand Des ...
随机推荐
- bzoj千题计划268:bzoj3131: [Sdoi2013]淘金
http://www.lydsy.com/JudgeOnline/problem.php?id=3131 如果已知 s[i]=j 表示有j个<=n数的数码乘积=i 那么就会有 s[a1]*s[a ...
- JSBinding+Bridge.NET:Inspector拖变量支持
之前的文档说了,JSB的设计是不允许gameObject上挂逻辑脚本的.原因很简单,在Js工程中根本就不存在C#形式的逻辑脚本,如果在Cs工程中挂上了,到了Js工程这边,直接Missing. 实际在使 ...
- 20155207王雪纯 2016-2017-2 《Java程序设计》第六周学习总结
20155207 2016-2017-2 <Java程序设计>第六周学习总结 教材学习内容总结 第10章 输入/输出 10.1 InputStream与OutputStream 10.1. ...
- 【转】UICollectionView使用介绍
CHENYILONG Blog UICollectionView 使用介绍 技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/lu ...
- [转]LaTex常用数学符号整理
转载自 http://blog.csdn.net/ying_xu/article/details/51240291 (自己保存方便查阅,侵删) 另一个网站 Markdown 添加 Latex 数学公式 ...
- Linux释放内存小脚本
最近发现渣渣ECS内存总是不够用,内存太小一不小心就用完了,用完就用完吧,内存用来做cache是可以快一些,但是内存用完了老是一顿一顿的卡,实在有点受不了,于是就写了释放内存的小脚本,觉得卡了就释放下 ...
- CTEX windedt 打开中文tex乱码问题
% !TEX encoding = System % !TEX program = pdflatex % !TEX encoding = System% !TEX program = pdflatex ...
- Mysql备份文件
- WPF的EventAggregator的发布和订阅
EventAggregator是Prism中专门处理ViewModel与ViewModel之间事件传递的类对象,它提供了针对事件的发布方法和订阅方法,所以可以非常方便的来管理事件.下面分几步来实现相关 ...
- js中apply的用法(转)
之一------(函数的劫持与对象的复制)关于对象的继承,一般的做法是用复制法: Object.extend见protpotype.js 的实现方法: Object.extend = function ...