牛客网多校第三场 E (KMP求最小循环节)

本来的思路是用字符串哈希做的,搞来搞去搞不动。。。题意理解的还有点偏差ORZ,最后换了KMP,找到了最小循环节就好做的多了,有的题真的是找对了方法难度立即降低不少。

我的丑代码:

#include<stdio.h>
#include <string.h>
using namespace std;
int next[1000005];
char p[1000005];
int pLen;
void getNext()
{
    next[0] = -1;
    int k = -1;
    int j = 0;
    while (j <=pLen-1)
    {
        if (k == -1 || p[j] == p[k])
        {
            ++k;
            ++j;
            next[j] = k;
        }
        else
        {
            k = next[k];
        }
    }
}
int main()
{
  scanf("%s",p);
     
        pLen=strlen(p);
        getNext();
        if(pLen%(pLen-next[pLen])==0){
      
        int x = pLen/(pLen-next[pLen]);
        int y = pLen/x;
        printf("%d\n", y);
        for(int i = 0; i < y; i++){
            printf("%d",x);
            for(int j = 0; j <x; j++){
                printf(" %d", i+j*y);
            }
        printf("\n");
        }
        }   
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41156122/article/details/81229580