import java.io.*;
class pro{
String[] data=new String[50000];
int datanumber;
int strlong,dicnumber;
String str;
int s,sx,sxs,m,ms,slong;
String[][] ph=new String[500][11];
int[][] nph=new int[500][11];
String[] dic=new String[5000];
double[] p=new double[5000];
double[] pos=new double[101];
double[] v=new double[500];
int[] gotosx=new int[500];
int[] op=new int[500];
int j;
double v1,maxv;
int wordnumber;
String[] word=new String[500];
int sx1,sx2;
int[] num=new int[5000];
int tr;
public static void main(String args[]){
pro test=new pro();
}
pro(){
readfile("ban.txt");
makepos();
dicnumber=0;
p[0]=1;
for(tr=1;tr<5000;tr++){
str=data[tr];
strlong=str.length();
for(s=1;s<strlong+1;s++){
slong=strlong-s+1;
if(slong>10)slong=10;
for(sx=1;sx<slong+1;sx++){
ph[s][sx]=str.substring(s-1,s+sx-1);
}
}
for(s=1;s<strlong+1;s++){
slong=strlong-s+1;
if(slong>10)slong=10;
for(sx=1;sx<slong+1;sx++){
ms=0;
for(m=1;m<dicnumber+1;m++){
if(ph[s][sx].equals(dic[m]))ms=m;
}
nph[s][sx]=ms;
}
}
sx=nph[strlong][1];
v[strlong]=pos[1]*p[sx];
for(j=1;j<strlong;j++){
s=strlong-j;
slong=strlong-s+1;
if(slong>10)slong=10;
maxv=-999;
sxs=0;
for(sx=1;sx<slong+1;sx++){
m=nph[s][sx];
if(s+sx<strlong+1)v1=sx*pos[sx]*p[m]+v[s+sx];
if(s+sx==strlong+1)v1=sx*pos[sx]*p[m];
if(v1>maxv)sxs=s+sx;
if(v1>maxv)maxv=v1;
}
gotosx[s]=sxs;
v[s]=maxv;
}
sx=1;
op[sx]=1;
s=gotosx[1];
while(s<strlong){
sx=sx+1;
op[sx]=s;
s=gotosx[s];
}
wordnumber=sx;
for(s=1;s<wordnumber;s++){
sx1=op[s];
sx2=op[s+1];
word[s]=ph[sx1][sx2-sx1];
}
s=wordnumber;
sx1=op[s];
word[s]=ph[sx1][strlong+1-sx1];
for(s=1;s<wordnumber+1;s++){
sxs=0;
for(sx=1;sx<dicnumber+1;sx++){
if(word[s].equals(dic[sx]))sxs=sx;
}
if(sxs==0)dicnumber=dicnumber+1;
if(sxs==0)dic[dicnumber]=word[s];
}
for(s=0;s<dicnumber+1;s++){
num[s]=0;
}
for(s=1;s<wordnumber+1;s++){
sxs=0;
for(sx=1;sx<dicnumber+1;sx++){
if(word[s].equals(dic[sx]))sxs=sx;
}
num[sxs]=num[sxs]+1;
}
for(s=0;s<dicnumber+1;s++){
p[s]=0.99*p[s]+0.01*(double)num[s]/wordnumber;
}
if(dicnumber>2000)diet();
System.out.println(dicnumber);
}
for(s=1;s<wordnumber+1;s++){
System.out.println(word[s]);
}
}
void diet(){
int s,sx;
int[] ch=new int[5000];
String[] newdic=new String[5000];
double[] newp=new double[5000];
int newdicnumber;
double p1,minp;
minp=999;
for(s=1;s<dicnumber+1;s++){
if (p[s]<minp)minp=p[s];
}
sx=0;
for(s=1;s<dicnumber+1;s++){
ch[s]=0;
if (p[s]>2*minp)sx=sx+1;
if (p[s]>2*minp)ch[s]=sx;
}
newdicnumber=sx;
for(s=1;s<dicnumber+1;s++){
if (ch[s]>0)sx=ch[s];
if (ch[s]>0)newdic[sx]=dic[s];
if (ch[s]>0)newp[sx]=p[s];
}
p1=0;
for(s=1;s<newdicnumber+1;s++){
p1=p1+newp[s];
}
newp[0]=1-p1;
if(newp[0]<0)newp[0]=0;
dicnumber=newdicnumber;
dic=newdic;
p=newp;
}
void makepos(){
double lam,x1,x2;
double[] cx=new double[101];
int k;
lam=4.8;
x1=1-lam;
cx[1]=1;
cx[2]=1;
for(k=3;k<101;k++){
cx[k]=(k-1)*cx[k-1];
}
for(k=1;k<101;k++){
x2=Math.pow(lam-1,k);
pos[k]=x2*Math.exp(x1)/cx[k];
}
}
void readfile(String file){
String st;
BufferedReader br;
s=0;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"SJIS"));
while((st = br.readLine()) != null) {
s=s+1;
data[s]=st;
}
br.close();
} catch (IOException e) {System.out.println(e);}
datanumber=s;
}
}
最終更新:2011年06月02日 10:30