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
  • 相关阅读:
    leetcode刷题16
    leetcode刷题15
    leetcode刷题14
    leetcode刷题13
    UnityWebReqest和WWW,请求web数据打包到Android手机上,报错 Unknown error记录
    Unable to instantiate prefab. Prefab may be broken.(Unity2018.2.2报错)
    Unity 2018.4.0 回退到 2018.2.2 出现错误日志修复
    Windows系统中,使用Protobuf,编译出C#文件
    Unity长按Button,显示消息盒子
    Mac 端 查找UnityEngine.dll和UnityEngine.UI.dll
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/6809425.html
Copyright © 2011-2022 走看看