HDU 3746 Cyclic Nacklace (kmp cycle section)


       The meaning of the question is to give you a string to see if it is a string composed of substring loops, such as ababab is composed of ab loops, and then ask if the string is not a looped string, then you need to add How many characters can be turned into a looping string, if yes, it is 0.

       Use kmp to find the number of repetitions Next[] before and after the string

       According to this question, it can be extended to determine whether a string is composed of substring loops, the condition is that when L==n&&n%L==0, the string is a substring loop composed of strings.


AC code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 1000005;
int Next[MAXN];
char p[MAXN];
int n,T;

void Com(){
  Next[0] = 0;
  int k = 0;
  for(int q=1;q<n;q++){
    while(k>0 && p[k]!=p[q])k = Next[k - 1];
    if(p[k] == p[q])k++;
    Next[q] = k;
  }
  return ;
}

intmain()
{
  scanf("%d",&T);
  while(T--){
  memset(Next,0,sizeof(Next));
  scanf("%s",p);
  // n = strlen(t);
  n = strlen(p);
  With();
  int l = n - Next[n - 1];
  if(l != n && n % l == 0){
    printf("0\n");
  }
  else printf("%d\n",l - n % l);
  // printf("%d\n",sum);
}
  return 0;
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324811356&siteId=291194637
Recommended