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 ...
随机推荐
- CF876 D 树状数组
大意就是放n个硬币,每次放一个计算下这种情况下的操作次数,一个操作为从左到右扫描,如果一个硬币右边是空的,就将硬币后移,否则该次操作停止. 显然发现对于一个情况,我们只要考虑最右边的空位的左侧有几个硬 ...
- 《Two Dozen Short Lessons in Haskell》(二十四)代数类型
这是<Two Dozen Short Lessons in Haskell>这本书的最后一章,第23章没有习题. 这一章里介绍了Haskell如果自定义一种类型,并且用一个双人博弈游戏为例 ...
- Spring 学习01
一.Spring概念 1 spring是开源的轻量级框架 2 spring核心主要两部分: (1)aop:面向切面编程,扩展功能不是修改源代码实现 (2)ioc:控制反转, - 比如有一个类,在类里面 ...
- JavaScript继承详解(一)
面向对象与基于对象 几乎每个开发人员都有面向对象语言(比如C++.C#.Java)的开发经验. 在传统面向对象的语言中,有两个非常重要的概念 - 类和实例. 类定义了一类事物公共的行为和方法:而实例则 ...
- es6笔记(4) Set数据结构
概要 介绍: 集合是由一组无序且唯一的项组成的,这个数据结构使用了与有限集合相同的数学概念,应用在计算机的数据结构中. ES6提供了数据结构Set.它类似于数组,但是没有重复的值. 特点: key与v ...
- Python人工智能之路 - 第一篇 : 你得会点儿Python基础
Python 号称是最接近人工智能的语言,因为它的动态便捷性和灵活的三方扩展,成就了它在人工智能领域的丰碑 走进Python,靠近人工智能 一.编程语言Python的基础 之 "浅入浅出&q ...
- [Alg::DP] Square Subsequence
题目如下: #include <iostream> #include <string> #include <vector> using namespace std; ...
- hdu GuGuFishtion 6390 数论 欧拉函数
题目:http://acm.hdu.edu.cn/showproblem.php?pid=6390 直接开始证明: 我们设…………………………………….....…...............………… ...
- MySQL 5.6.10 跨平台GTID复制实践
根据业务需要,建立MySQL复制来实现数据冗余. 1:binlog_format 默认值是:statement 有效值: ROW,基于行的复制 STATEMENT 基于语句级别的复制 MASTER ...
- 解决阿里云安骑士漏洞警告:wordpress WPImageEditorImagick 指令注入漏洞
解决:wordpress WPImageEditorImagick 指令注入漏洞 前些天在阿里云服务器上安装了wordpress,阿里云提示有wordpress WP_Image_Editor_Ima ...