登 录
这个题的最大特点是待匹配的串中有杂音。如果把字典做成hash存储,采用枚举杂音点的方法来匹配,效率是O(L*2^L),这是没办法接受的。这样的问题我们一般要逆着来,把字典往待匹配的字符串里面代,效率可以做到O(L*W*25)。
总之,很经典很经典。
#include "string.h" #include "math.h" #include <queue> #include <algorithm> #include <iostream> #define _clr(a,b) memset(a,b,sizeof(a)) template<class T> T _abs(T a) {if(a<0) a=-a;return a;} template<class T> void Get_Min(T& a,T b) { if(a>b) a=b;} template<class T> void Get_Max(T& a,T b) { if(a<b) a=b;} using namespace std; int W,L; char str[305]; char dictionary[601][26]; int DP[305]; int main() { scanf("%d%d",&W,&L); scanf("%s",str); for(int i=0;i<W;i++) { scanf("%s",dictionary[i]); } for(int i=0;i<=L;i++) DP[i]=i; for(int i=1;i<=L;i++) { for(int j=0;j<W;j++) { int index1=0,index2=i-1; int cnt=0; while(dictionary[j][index1]!='/0'&&str[index2]!='/0') { if(dictionary[j][index1]==str[index2]) { index1++; index2++; } else { index2++; cnt++; } } if(dictionary[j][index1]=='/0') { if(DP[index2]>DP[i-1]+cnt) { DP[index2]=DP[i-1]+cnt; for(int k=index2+1;k<=L;k++) { DP[k]=min(DP[k],DP[index2]+k-index2); } } } } } printf("%d/n",DP[L]); return 0; }
抱歉!评论已关闭.