zoukankan      html  css  js  c++  java
  • hust1010 kmp

    There is a string A. The length of A is less than 1,000,000. I rewrite it again and again. Then I got a new string: AAAAAA...... Now I cut it from two different position and get a new string B. Then, give you the string B, can you tell me the length of the shortest possible string A. For example, A="abcdefg". I got abcdefgabcdefgabcdefgabcdefg.... Then I cut the red part: efgabcdefgabcde as string B. From B, you should find out the shortest A.InputMultiply Test Cases. For each line there is a string B which contains only lowercase and uppercase charactors. The length of B is no more than 1,000,000.OutputFor each line, output an integer, as described above.Sample Input

    bcabcab
    efgabcdefgabcde
    

    Sample Output

    3
    7
    题意:字符串找最小环
    题解:kmp的Next数组求,还以为会有什么技巧,结果就是一道裸的求环。。。。(还是用slen-Next[slen-1]-1求)
    #include<map>
    #include<set>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<cstdio>
    #include<iomanip>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define pi acos(-1)
    #define ll long long
    #define mod 1000000007
    #define ls l,m,rt<<1
    #define rs m+1,r,rt<<1|1
    
    using namespace std;
    
    const double g=10.0,eps=1e-9;
    const int N=1000000+5,maxn=1000000+5,inf=0x3f3f3f3f;
    
    int Next[N],slen;
    string str;
    
    void getnext()
    {
        int k=-1;
        Next[0]=-1;
        for(int i=1;i<slen;i++)
        {
            while(k>-1&&str[k+1]!=str[i])k=Next[k];
            if(str[k+1]==str[i])k++;
            Next[i]=k;
        }
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
     //   cout<<setiosflags(ios::fixed)<<setprecision(2);
        while(cin>>str){
            slen=str.size();
            getnext();
            cout<<slen-Next[slen-1]-1<<endl;
        }
        return 0;
    }
    View Code
  • 相关阅读:
    【2021-01-07】叫我“何工”就好
    2021年的 目标计划
    转:我在拼多多的三年 https://www.leadroyal.cn/?p=1228#more-1228
    匀强电场等效重力场
    开源图片素材管理软件-civet
    心学#传习录学习
    工作感受月记202101月
    统计字典序元音字符的数量
    可被5整除的二进制
    假期刷题--JAVA
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/6809425.html
Copyright © 2011-2022 走看看