Write a program to read a character string and determine its smallest period.
Input
The first line oif the input file will contain a single integer N indicating how many test case that your program will test followed by a blank line. Each test case will contain a single character string of up to 80 non-blank characters. Two consecutive input will separated by a blank line.Output
An integer denoting the smallest period of the input string for each input. Two consecutive output are separated by a blank line.Sample Input
1 HoHoHo
Sample Output
2
CPE的題目,
但是找不到他的測是檔案提目在哪裡,
所以不知道到底對不對.
題目上像是在講說輸入一個字串, 最多80的非零字串,
判斷字串中是不是有重複的,
有的話把重複字串的長度最為輸出.
import java.io.*;
import java.util.*;
public class Periodic_String{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
char s_array[] = new char[81];
s_array = s.toCharArray();
System.out.println("s.length(): "+s.length());
char check_char[] = new char[s.length()+10];
check_char[0] = s_array[0];
char beda[] = new char[s.length()+10];
int beda_num = 0;
int check_num = 1;
int cn = 0;
int first = 0;
for(int i = 1 , j = 0; i < s_array.length ; i++){
if(s_array[i] != s_array[i-1]&& first==0){
if(s_array[i]!= s_array[0]){
check_char[i] = s_array[i];
cn++;
//System.out.println(i+" 1");
}else{
first++;
beda[beda_num++] = s_array[i];
//System.out.println(i+" 2");
}
}else if(first==0){
check_char[i] = s_array[i];
cn++;
//System.out.println(i+" 3");
}else if(first!=0){
if(s_array[i]==check_char[check_num++]){
beda[beda_num++] = s_array[i];
//System.out.println(i+" 4 "+check_num+" ("+beda[beda_num-1]+")("+s_array[i]+")");
}else{
System.out.println(i+" 5 ");
for(int k = 0; k<beda_num ; k++){
check_char[cn++] = beda[k];
}
}
}
}
cn = cn+1;
if(cn!=beda_num){
//System.out.println(" 6 "+cn+" "+beda_num);
for(int k = 0; k<beda_num ; k++){
check_char[cn++] = beda[k];
}
System.out.println("TTQQ: "+cn);
}else{
System.out.println("SSQQ: "+cn);
}
/*System.out.println("-----------------");
for(int i = 0; i < s_array.length ; i++){
System.out.println(i + " (" + check_char[i] + ") (" + beda[i] + ") ("+s_array[i]+")");
}
System.out.println("-------------------++++--------");*/
}
}
abcabc
s.length(): 6
SSQQ: 3
abcac
s.length(): 5
4 5
TTQQ: 5
aacaac
s.length(): 6
SSQQ: 3
分三種, 一個是簡單的abcabc,
就是標準的重複字串.
第二種是字串中出現重複過的字元,
第三種出現大量首字元.
使用方法是把輸入的字串放到s_array裡面,
在一開始的時候把S_array的字串放到check_char裡面,
在依序往下比對,
比對到跟字首相同的時候停止.
停止的時候把正在比對的字元丟到另一個陣列beda裡面,
就是用s_array[i+1..]跟check[0..]比對, 成功揪丟到beba裡面.
而第二種就是直接把字串丟到到check裡面, 直到碰到不同字元, 就換到第一種判斷式上,
而第三種方法就是把字串丟到check, 接下來第二部分的丟到bade裡面,
但是發現bade跟check或是長度不同的時候,
就把bade放在check後面接上去,
沒有留言:
張貼留言