import java.io.*;
class ren{
String[] data=new String[50000];
int datanumber;
String str;
int s,sx,sxs,t,tx,txs,slong,tlong,strlong;
int dicnumber,mpnumber,wordnumber;
String[] word=new String[500];
int[] nword=new int[500];
String[][] ph=new String[500][11];
int[][] nph=new int[500][11];
String[] dic=new String[1000];
double[] fp=new double[1000];
double[][] v=new double[500][11];
int[][] gototx=new int[500][11];
int[] mp1=new int[10000];
int[] mp2=new int[10000];
double[] count=new double[10000];
double[] fcount=new double[1000];
double[] mp=new double[10000];
int m,ms;
double v1,maxv;
double c1,c2;
int tr;
public static void main(String args[]){
ren test=new ren();
}
ren(){
readfile("ban.txt");
dicnumber=0;
mpnumber=0;
mp[0]=1;
fp[0]=1;
for(tr=1;tr<100;tr++){
str=data[tr];
strlong=str.length();
for(s=1;s<strlong+1;s++){
slong=s;
if(slong>10)slong=10;
for(sx=1;sx<slong+1;sx++){
ph[s][sx]=str.substring(s-sx,s);
}
}
for(s=1;s<strlong+1;s++){
slong=s;
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;
}
}
slong=strlong;
if(slong>10)slong=10;
for(s=1;s<slong+1;s++){
sx=nph[s][s];
v[s][s]=fp[sx];
}
m=mk(nph[1][1],nph[2][1]);
v[2][1]=mp[m]+v[1][1];
gototx[2][1]=1;
for(s=3;s<strlong+1;s++){
slong=s-1;
if(slong>10)slong=10;
for(sx=1;sx<slong+1;sx++){
t=s-sx;
tlong=t;
if(tlong>10)tlong=10;
maxv=-999;
txs=0;
for (tx=1;tx<tlong+1;tx++){
m=mk(nph[t][tx],nph[s][sx]);
v1=mp[m]+v[t][tx];
if(v1>maxv)txs=tx;
if(v1>maxv)maxv=v1;
}
gototx[s][sx]=txs;
v[s][sx]=maxv;
}
}
s=strlong;
maxv=-999;
sxs=0;
slong=strlong;
if(slong>10)slong=10;
for(sx=1;sx<slong+1;sx++){
if(v[s][sx]>maxv)sxs=sx;
if(v[s][sx]>maxv)maxv=v[s][sx];
}
makeword(sxs);
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];
if(sxs==0)nword[s]=dicnumber;
if(sxs>0)nword[s]=sxs;
}
for(s=1;s<wordnumber;s++){
m=mk(nword[s],nword[s+1]);
if(m==0)mpnumber=mpnumber+1;
if(m==0)mp1[mpnumber]=nword[s];
if(m==0)mp2[mpnumber]=nword[s+1];
}
for(s=1;s<mpnumber+1;s++){
count[s]=0.95*count[s];
}
for(s=1;s<wordnumber;s++){
m=mk(nword[s],nword[s+1]);
count[m]=count[m]+1;
}
for(s=1;s<mpnumber+1;s++){
c1=count[s];
c2=0;
for(sx=1;sx<mpnumber+1;sx++){
if(mp1[s]==mp1[sx])c2=c2+count[s];
}
mp[s]=0;
if(c2>0)mp[s]=c1/c2;
}
for(s=0;s<dicnumber+1;s++){
fcount[s]=0.95*fcount[s];
}
m=nword[1];
fcount[m]=fcount[m]+1;
c1=0;
for(s=1;s<dicnumber+1;s++){
c1=c1+fcount[s];
}
for(s=1;s<dicnumber+1;s++){
fp[s]=fcount[s]/c1;
}
System.out.println(dicnumber);
}
}
void makeword(int sxs){
int s,sx,t,tx;
int[] op1=new int[500];
int[] op2=new int[500];
int z,c,h;
sx=sxs;
s=strlong;
z=s-sx;
op1[1]=s;
op2[1]=sx;
c=1;
while(z>1){
t=s-sx;
tx=gototx[s][sx];
s=t;
sx=tx;
c=c+1;
op1[c]=s;
op2[c]=sx;
z=s-sx;
}
h=0;
if(op1[c]>op2[c])h=100;
if(h>50)op1[c+1]=op1[c]-op2[c];
if(h>50)op2[c+1]=op1[c+1];
if(h>50)c=c+1;
wordnumber=c;
for(c=1;c<wordnumber+1;c++){
s=op1[c];
sx=op2[c];
word[wordnumber+1-c]=ph[s][sx];
}
}
int mk(int sx1,int sx2){
int i,sxs;
int h;
sxs=0;
for(i=1;i<mpnumber+1;i++){
h=0;
if(mp1[i]==sx1)h=h+1;
if(mp2[i]==sx2)h=h+1;
if(h==2)sxs=i;
}
return sxs;
}
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年05月31日 19:18