• HUST 1010 The Minimum Length (字符串最小循环节)


    题意

    有一个字符串A,一次次的重写A,会得到一个新的字符串AAAAAAAA.....,现在将这个字符串从中切去一部分得到一个字符串B。例如有一个字符串A="abcdefg".,复制几次之后得到abcdefgabcdefgabcdefgabcdefg....,现在切去中间红色的部分,得到字符串B,现在只给出字符串B,求出字符串A的最小长度。

    思路

    容易发现这个“循环节”是可以“旋转”的。即上述的"abcdefg"也可以是"bcdefga","cdefgab",…… 。所以我们完全可以规定这个循环节是以字符串B的后缀为后缀的字符串。next[]数组表示满足S[1..i’] == S[i-i’+1..i]的最大的i’。则S[next[n]+1..n]就是答案,长度就为n-next[n]

    代码

      [cpp] #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <string> #include <cstring> #include <vector> #include <set> #include <stack> #include <queue> #define MID(x,y) ((x+y)/2) #define MEM(a,b) memset(a,b,sizeof(a)) #define REP(i, begin, end) for (int i = begin; i <= end; i ++) using namespace std; const int maxn = 1000005; int next[maxn]; char s[maxn]; void get_next(){ int len = strlen(s), j = -1; next[0] = -1; for (int i = 1; i < len; i ++){ while(j > -1 && s[i] != s[j+1]) j = next[j]; if (s[i] == s[j+1]) j ++; next[i] = j; } } int main(){ while(scanf("%s", s) != EOF){ get_next(); printf("%d ", strlen(s) - (next[strlen(s)-1] + 1)); } return 0; } [/cpp]

  • 相关阅读:
    python3 入门
    Python2 的列表排序
    数据库阻塞SQL的隔离级别
    数据库阻塞讲解设计应用程序时避免阻塞的八个准则
    DELPHI学习简单类型
    DELPHI学习结构类型
    InsideVCL第3章面向对象程序语言和Framework
    数据库阻塞分析死锁并处理
    面向对象开发实践之路
    DELPHI hint 的应用
  • 原文地址:https://www.cnblogs.com/AbandonZHANG/p/4114114.html
Copyright © 2020-2023  润新知