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. 高斯—若尔当(约当)消元法解异或方程组+bitset优化模板

    高斯消元法是将矩阵化为上三角矩阵 高斯—若尔当消元法是 选定主元后,将主元化为1,枚举除主元之外的所有行进行消元 即将矩阵化为对角矩阵,这样不用回代 bitset<N>a[N]; int ...

  2. python学习笔记6--双色球需求实现

    # 5,随机产生5条双色球号码 # blue 存蓝色的求 01,02 # red 存红色的求 17,16,03 # date存生成的时间,精确达到秒 #处理 import random,datetim ...

  3. Java入门系列(八)多线程

    基本线程类指的是Thread类,Runnable接口,Callable接口 典型多线程问题 生产者-消费者 死锁问题

  4. Django Book 学习笔记(上)

    拜读了网上的Django Book,现在来总结一下吧...... 一.Django的配置 非常的蛋疼,由于Django的块组之间耦合度低,这既是它的优点,也是它的缺点.我在Ubuntu所配置的Djan ...

  5. Guava HashMultiMap(MultiMap)反转映射

    (一)MultiMap 多重map,一个key可以对应多个值(多个值放在一个list中),可用于分组 举例: Multimap<String, Integer> map = HashMul ...

  6. 因子分析(Factor analysis)

    1.引言 在高斯混合和EM算法中,我们运用EM算法拟合混合模型,但是我们得考虑得需要多少的样本数据才能准确识别出数据中的多个高斯模型!看下面两种情况的分析: 第一种情况假如有 m 个样本,每个样本的维 ...

  7. va_start(),va_end()函数应用【转】

    转自:http://www.cnblogs.com/gogly/articles/2416833.html 原理解释: VA_LIST 是在C语言中解决变参问题的一组宏,在<stdarg.h&g ...

  8. 使用Python编写简单的端口扫描器的实例分享【转】

    转自 使用Python编写简单的端口扫描器的实例分享_python_脚本之家 http://www.jb51.net/article/76630.htm -*- coding:utf8 -*- #!/ ...

  9. linux进程的一些日常处理

    linux 下查看一个进程运行路径的方法 在linux下查看进程大家都会想到用 ps -ef|grep XXX 可是看到的不是全路径,怎么看全路径呢? 每个进程启动之后在 /proc下面有一个于pid ...

  10. SqlServer中 SET DATEFIRST更改

    在 SQL Server 中默认情况下,每周的开始都是从周日开始算起的,如果默认星期一呢? 这里有三种方式可以解决这个问题: 一:直接通过 SET DATEFIRST VALUE 来更改重新生成新的 ...