import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
static int[] next;
static char[] c;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.next();//输入字符串
int len = s.length();
c = s.toCharArray();
next = new int[len];//创建next数组
getnext(len);//得到next数组
int temp = len - next[len-1];//字符串长度减去字符串的next值,得到最短循环节
if(len%temp==0)//如果是完整的循环节组成
{
System.out.println(len/temp);
}
else
{
System.out.println(1);
}
}
static void getnext(int len) {
int j = 0;
next[0] = 0;
for (int i = 1; i < len; i++) {//从后一位开始遍历
while (j != 0 && c[i] != c[j])//不匹配 往前推
{
j = next[j - 1];
}
if (c[i] == c[j]) j++;
next[i] = j;
}
}
}
boarder-KMP
最新推荐文章于 2024-09-10 10:21:38 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)