Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
这道题目讨论得好少啊,贴个代码做参考。还是使用DP/* -*- c++ -*- copy[write] by dirlt(dirtysalt1987@gmail.com) */ #include <cstdio> #include <cstdlib> #include <cctype> #include <cmath> #include <cassert> #include <cstring> #include <iostream> #include <algorithm> using namespace std; char dict[610][32]; int W; char mesg[310]; int L; //DP[i]表示前i个字符串如果需要和dict匹配的话需要删除的字符。那么从i+1开始匹配匹配到j位置如果有n个删除的话,那么DP[j]=min(DP[j],DP[i]+n). int DP[310]; int solve() { for(int i=0;i<=L;i++)DP[i]=i; for(int i=1;i<=L;i++){ int j=i-1; for(int k=0;k<W;k++){ int lk=0,lj=j; int cnt=0; //从j开始进行匹配的话最长匹配到lj同时删除cnt个字符 while(dict[k][lk]!=0 && mesg[lj]!=0){ if(dict[k][lk]==mesg[lj]){ lk++; lj++; }else{ cnt++; lj++; } } if(dict[k][lk]==0){ //一旦到某个字符删除字符数减少为n的话,那么后面的串就可能以n+1,n+2个这个字符数删除:-) //这也是为什么初始化DP[i]=i的原因. if((DP[j]+cnt)<DP[lj]){ DP[lj]=DP[j]+cnt; for(int n=lj+1;n<=L;n++) DP[n]=min(DP[n],DP[lj]+n-lj); } } } } return DP[L]; } int main() { scanf("%d %d",&W,&L); scanf("%s",mesg); for(int i=0;i<W;i++){ scanf("%s",dict[i]); } printf("%d\n",solve()); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator