class pro{
String str;
int dicnumber,pnumber;
int s,sx,sxs,t,tx,txs,mx,mxs,slong,tlong,strlong;
double[] p=new double[500];
double[] q=new double[500];
String[][] ph=new String[500][11];
int[][] nph=new int[500][11];
String[] dic=new String[500];
double[][] v=new double[500][11];
int[] p1=new int[500];
int[] p2=new int[500];
int[][] gototx=new int[500][11];
int[] nword=new int[500];
double v1,maxv;
String[] word=new String[500];
int wordnumber;
int h,tr;
double[] px=new double[500];
int[] num=new int[500];
public static void main(String args[]){
pro test=new pro();
}
pro(){
dicnumber=0;
pnumber=0;
p[0]=1;
q[0]=1;
for(tr=1;tr<100;tr++){
str="東日本大震災による地盤沈下で道路など";
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++){
mxs=0;
for(mx=1;mx<dicnumber+1;mx++){
if(ph[s][sx].equals(dic[mx]))mxs=mx;
}
nph[s][sx]=mxs;
}
}
slong=strlong;
if(slong>10)slong=10;
for(s=1;s<slong+1;s++){
sx=nph[s][s];
v[s][s]=q[sx];
}
sx=mk(nph[1][1],nph[2][1]);
v[2][1]=q[sx]+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++){
mx=mk(nph[t][tx],nph[s][sx]);
v1=p[mx]+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<pnumber+1;s++){
num[s]=0;
}
for(s=1;s<wordnumber;s++){
sx=mk(nword[s],nword[s+1]);
if(sx==0)pnumber=pnumber+1;
if(sx==0)p1[pnumber]=nword[s];
if(sx==0)p2[pnumber]=nword[s+1];
if(sx==0)num[pnumber]=1;
if(sx>0)num[sx]=num[sx]+1;
}
for(s=1;s<pnumber+1;s++){
px[s]=(double)num[s]/(wordnumber-1);
}
for(s=0;s<pnumber+1;s++){
p[s]=0.95*p[s]+0.05*px[s];
}
for(s=0;s<dicnumber+1;s++){
h=0;
if(nword[1]==s)h=100;
if(h>50)q[s]=0.95*q[s]+0.05;
if(h<50)q[s]=0.95*q[s];
}
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 h,sx,sxs;
sxs=0;
for(sx=1;sx<pnumber+1;sx++){
h=0;
if(p1[sx]==sx1)h=h+1;
if(p2[sx]==sx2)h=h+1;
if(h==2)sxs=sx;
}
return sxs;
}
}
最終更新:2011年05月30日 15:29