zoukankan      html  css  js  c++  java
  • Xtreme9.0

    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
    ",len-p[len]);
        }
    }
  • 相关阅读:
    java程序员面试笔记宝典 note
    JVM常见面试题
    面试&笔试总结 数据库、网络协议、常识
    面试&笔试总结 Java框架篇
    面试准备&总结-Java基础篇
    JDBC事务管理及SavePoint示例
    JSP的内置对象
    Java中的集合框架
    .NET WEBAPI 添加中英文切换,国际化项目
    搜索引擎入门 (建立一个简单的java Lucene实例)
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5958336.html
Copyright © 2011-2022 走看看