アットウィキロゴ

動的計画法 クラス化

class makeword{

String[] dic=new String[100];
double[] p=new double[100];  
double[] v=new double[100];
String[] word=new String[100];
String[] phx=new String[100];
int[] gototx=new int[100];
int strlong;
String ph;
int dicnumber,wordnumber;
int t,tx,txs,sx;
double p1,v1,maxv;

void makedata(String str){

strlong=str.length();
ph=str.substring(0,1);

p1=p[0];
for(sx=1;sx<dicnumber+1;sx++){
if(ph.equals(dic[sx]))p1=p[sx];
}
v[0]=p1;

for(t=1;t<strlong;t++){
ph=str.substring(0,t+1);
p1=p[0];
for(sx=1;sx<dicnumber+1;sx++){
if(ph.equals(dic[sx]))p1=p[sx];
}
txs=0;
maxv=p1;
for(tx=0;tx<t;tx++){
ph=str.substring(tx+1,t+1);
p1=p[0];
for(sx=1;sx<dicnumber+1;sx++){
if(ph.equals(dic[sx]))p1=p[sx];
}
v1=p1+v[tx];
if(v1>maxv)txs=tx;
if(v1>maxv)maxv=v1;
}
gototx[t]=txs;
v[t]=maxv;
}

sx=0;

t=strlong-1;
while(t>0){
tx=gototx[t];
ph=str.substring(tx+1,t+1);
t=tx;
sx=sx+1;
phx[sx]=ph;
wordnumber=wordnumber+1;
}
sx=sx+1;
phx[sx]=str.substring(0,t+1);

wordnumber=sx;

for(sx=1;sx<wordnumber+1;sx++){
word[sx]=phx[wordnumber+1-sx];
}

}
}
最終更新:2011年05月27日 08:18