https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1239
這題就是要找出最短子字串且算出有幾個重複 程式架構是子字串1~n一個一個去跟原本字串做比對 if(str.length()%sub.length()!=0) continue; 這個是判斷子字串是否為原本字串的倍數否就繼續執行下一個迴圈
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
while(scn.hasNext()){
String str=scn.nextLine();
StringBuilder sub = new StringBuilder();
if(str.equals("."))
break;
for(int i=0;i<str.length();i++){
sub.append(str.charAt(i));
if(str.length()%sub.length()!=0)
continue;
boolean b=true;
for(int j=0;j<str.length();j+=sub.length()){
if(!str.substring(j,j+sub.length()).equals(sub.toString())){
b=false;
break;
}
}
if(b)
break;
}
System.out.println(str.length()/sub.length());
}
}
/*
題目:Q10298 - Power Strings
作者:1010
時間:西元 2016 年 8 月 */
}