問題描述
原諒我不放文字了。(我估計沒人看這個博客吧,當自己筆記用了,有價值的話後期再改)
代碼
註釋我也沒加,有時間的話以後再說吧
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Random;
public class SeussAndSaki {
final static String alphabet = "abcdefghijklmnopqrstuvwxyz";
public static void main(String[] args) throws Exception {
String fileA = "spamiam.txt";
String fileB = "saki_story.txt";
randomA();
System.out.println("\nspamiam.txt in strategy B");randomB(fileA);
System.out.println("\nspamiam.txt in strategy C");randomC(fileA);
System.out.println("\nspamiam.txt in strategy D");randomD(fileA);
System.out.println("\nsaki_story.txt in strategy B");randomB(fileB);
System.out.println("\nsaki_story.txt in strategy C");randomC(fileB);
System.out.println("\nsaki_story.txt in strategy D");randomD(fileB);
}
public static void randomA() {
Random random = new Random();
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
for (int k = 1; k <= 4; k++) {
System.out.print(alphabet.charAt(random.nextInt(26)));
}
System.out.print(" ");
}
System.out.println();
}
}
public static void randomB(String fileName) throws Exception {
int[] frequency = new int[26];
String[] splitedTxt = readToString(fileName);
for(int i=0; i<26; i++)
frequency[i] = 0;
for(String s : splitedTxt)
for(int i=0; i<s.length(); i++)
frequency[s.charAt(i)-97]++;
for(int i=1; i<=25; i++)
frequency[i] += frequency[i-1];
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
for (int k = 1; k <= 4; k++) {
System.out.print(strategyB(frequency));
}
System.out.print(" ");
}
System.out.println();
}
}
public static void randomC(String fileName) throws Exception {
Random random = new Random();
int[][] frequency = new int[26][27];
int[] total = new int[26];
String[] splitedTxt = readToString(fileName);
for(int i=0; i<26; i++)
for(int j=0; j<27; j++)
frequency[i][j] = 0;
initTotal(total);
for(String s : splitedTxt) {
if(s.length() == 0)
continue;
total[s.charAt(0)-97]++;
for(int i=1; i<s.length(); i++) {
char cn1 = s.charAt(i-1);
char cn2 = s.charAt(i);
frequency[cn1-97][cn2-97]++;
total[cn2-97]++;
}
}
sumTotal(total);
for(int i=0; i<26; i++)
for(int j=1; j<=26; j++)
frequency[i][j] += frequency[i][j-1];
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
char pre = strategyB(total);
System.out.print(pre);
for(int k=1; k<=3; k++) {
if(frequency[pre-97][26] <= 0) {
pre = strategyB(total);
System.out.print(pre);
}else {
int r = random.nextInt(frequency[pre-97][26]);
for(int m=0; m<26; m++) {
if(r <= frequency[pre-97][m]) {
pre = alphabet.charAt(m);
System.out.print(pre);
break;
}
}
}
}
System.out.print(" ");
}
System.out.println();
}
}
public static void randomD(String fileName) throws Exception {
Random random = new Random();
int frequency[][][] = new int[26][26][27];
int[] total = new int[26];
String[] splitedTxt = readToString(fileName);
for(int i=0; i<26; i++)
for(int j=0; j<26; j++)
for(int k=0; k<27; k++)
frequency[i][j][k] = 0;
initTotal(total);
for(String s : splitedTxt) {
if(s.length() == 0)
continue;
else if(s.length() == 1) {
total[s.charAt(0)-97]++;
continue;
}
total[s.charAt(0)-97]++;
total[s.charAt(1)-97]++;
for(int i=2; i<s.length(); i++) {
char cn1 = s.charAt(i-2);
char cn2 = s.charAt(i-1);
char cn3 = s.charAt(i);
frequency[cn1-97][cn2-97][cn3-97]++;
total[cn3-97]++;
}
}
sumTotal(total);
for(int i=0; i<26; i++) {
for(int j=0; j<26; j++) {
for(int k=1; k<27; k++) {
frequency[i][j][k] += frequency[i][j][k-1];
}
}
}
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
char cn1 = strategyB(total);
char cn2 = strategyB(total);
System.out.print(cn1);
System.out.print(cn2);
for(int k=1; k<=2; k++) {
if(frequency[cn1-97][cn2-97][26] <= 0) {
cn1 = cn2;
cn2 = strategyB(total);
System.out.print(cn2);
}else {
int r = random.nextInt(frequency[cn1-97][cn2-97][26]);
for(int m=0; m<26; m++) {
if(r <= frequency[cn1-97][cn2-97][m]) {
cn1 = cn2;
cn2 = alphabet.charAt(m);
System.out.print(cn2);
break;
}
}
}
}
System.out.print(" ");
}
System.out.println();
}
}
public static String[] readToString(String filename) throws Exception {
File file = new File(filename);
FileReader reader = new FileReader(file);
BufferedReader bf = new BufferedReader(reader);
String txt = "";
String line = null;
while((line = bf.readLine()) != null) {
txt += (line + " ");
}
txt = txt.toLowerCase();
String[] splitedTxt = txt.split(" ");
for(int i=0; i<splitedTxt.length; i++) {
splitedTxt[i] = splitedTxt[i].replaceAll("[^a-zA-Z]", "");
}
bf.close();
return splitedTxt;
}
public static char strategyB(int[] frequency) {
Random random = new Random();
int r = random.nextInt(frequency[25]);
for(int m=0; m<26; m++) {
if(r <= frequency[m]) {
return alphabet.charAt(m);
}
}
return alphabet.charAt(25);
}
public static void initTotal(int[] total) {
for(int i=0; i<26; i++)
total[i] = 0;
}
public static void sumTotal(int[] total) {
for(int i=1; i<=25; i++)
total[i] += total[i-1];
}
}