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的更多相关文章

  1. Xtreme9.0 - Communities 强连通

    Xtreme9.0 - Communities 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/c ...

  2. Xtreme9.0 - Light Gremlins 容斥

    Xtreme9.0 - Light Gremlins 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenge ...

  3. IEEEXtreme Practice Community Xtreme9.0 - Digit Fun!

    Xtreme9.0 - Digit Fun! 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/di ...

  4. 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 ' ...

  5. 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: [] ...

  6. 经典递归问题:0,1背包问题 kmp 用遗传算法来解背包问题,hash表,位图法搜索,最长公共子序列

    0,1背包问题:我写笔记风格就是想到哪里写哪里,有很多是旧的也没删除,代码内部可能有很多重复的东西,但是保证能运行出最后效果 '''学点高大上的遗传算法''' '''首先是Np问题的定义: npc:多 ...

  7. 模式串 从 0 开始的KMP算法

    /** * 看了 b站视频 BV1jb411V78H 对KMP有了一点理解,然后我写了这个代码 * 这个代码和视频里面的有一点不同,字符串是从 0 开始的,而不是从1 开始的 * 希望能够帮到学习KM ...

  8. Xtreme9.0 - Block Art 线段树

    Block Art 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/block-art Descr ...

  9. Xtreme9.0 - Taco Stand 数学

    Taco Stand 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/taco-stand Des ...

随机推荐

  1. bzoj千题计划186:bzoj1048: [HAOI2007]分割矩阵

    http://www.lydsy.com/JudgeOnline/problem.php?id=1048 #include<cmath> #include<cstdio> #i ...

  2. html5 canvas多个图像旋转

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. jquery操作select(取值,设置选中)[转]

    每一次操作select的时候,总是要出来翻一下资料,不如自己总结一下,以后就翻这里了. 比如<select class="selector"></select&g ...

  4. 20155318 2016-2017-2 《Java程序设计》第五周学习总结

    20155318 2016-2017-2 <Java程序设计>第五周学习总结 教材学习内容总结 try...catch 键盘输入利用java.util.Scanner,Scanner 名 ...

  5. 洛谷 P1603 斯诺登的密码

    我一开始还没看懂非正规数字的意义,以为那里写的单词不算,蒙了好久,而且这题非常考验仔细程度,一不小心就RE,WA. 嗯,好像讲了些废话,那我们看看思路,我的做法和前面的大佬们有些不同,因为这题只有六个 ...

  6. VirtualBox中CentOS遇到的问题

    centos7 安装步骤 https://www.cnblogs.com/hihtml5/p/8217062.html 静态ip设置 TYPE="Ethernet" PROXY_M ...

  7. 环形缓冲区-模仿linux kfifo【转】

    转自:https://blog.csdn.net/vertor11/article/details/53741681 struct kfifo{ uint8_t *buffer; uint32_t i ...

  8. Shiro:授权控制

    对容易忽略的地方记录如下: 1.需要引入下面2个依赖,具体版本根据自身环境修改: <dependency> <groupId>org.apache.geronimo.bundl ...

  9. 浅谈TCP拥塞控制算法

    TCP通过维护一个拥塞窗口来进行拥塞控制,拥塞控制的原则是,只要网络中没有出现拥塞,拥塞窗口的值就可以再增大一些,以便把更多的数据包发送出去,但只要网络出现拥塞,拥塞窗口的值就应该减小一些,以减少注入 ...

  10. springboot---->集成mybatis开发(一)

    这里面我们介绍一下springboot与mybatis的集成,主要完成了mybatis的真分页.一个成熟的人往往发觉可以责怪的人越来越少,人人都有他的难处. springboot简单集成mytbati ...