Periodic string (JAVA language)

Package Chapter 3 Exercises;
/*
 * If a character can be repeated many times from a string of length k, the string is said to have a period of k.
 * For example: abcabcabcabc has a period of 3 (note: it also has a period of 6 and 12)
 * Enter a string with a length not exceeding 80, and output its minimum period.
 */
import java.util.*;
public class periodic string {


public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
String s=in.next ();
int max_T=s.length();
   int T=1;    while(T<max_T){        if(max_T/T*T!=max_T) {T++;continue;} //Filter all possible values ​​of T        int fg=1;        for(int i=1;i*T<max_T;i++){            String c1=s.substring(0,T);            String c2=s.substring(i*T,(i+1)* T);
   






           if(!c1.equals(c2)) fg=0; //Continue the loop if one segment is not satisfied
       }
       if(fg==1)break;     
       T++;
   }
   System.out.println(T); } }




Guess you like

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