HDU - 4271(经典字符串dp改)

来源:互联网 发布:linux 改变文件夹权限 编辑:程序博客网 时间:2024/05/20 11:51

本题目就是给定最多是个小串,和一母串,f(i)为将母串做至少f(i)次修改产生子串si,求min(f(i))

但是母串是可以首位相连的。

那么如果母串长l1 >= 小串长l2 , 那么只需在母串后将首部十个拷贝到尾部,然后正常找f(i), l1<l2 暴力美剧所有l1串的起始位置 ,把长度为n的环拆成 n个等价串再正常找。

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>#include <vector>#define to first#define val second#define rep(i,n) for(int i=0;i<(int)n;i++)#define rep1(i,x,y) for(int i=x;i<=(int)y;i++)using namespace std;typedef long long ll;typedef pair<int,int> pii;typedef long long LL;const int N = 1e5+100;const int inf = 0x3f3f3f3f;int d[12][N]={0};int dp(char *s1,char *s2,int l1,int l2){     int  tmp=inf;     for(int i=1;i<=l1;i++){         d[i][0]=i;         for(int j=1;j<=l2;j++){             d[i][j]=min(d[i-1][j-1]+(s1[i-1]!=s2[j-1]),min(d[i-1][j],d[i][j-1])+1);             if(i==l1)  tmp=min(tmp,d[i][j]);         }     }     return tmp; }char src[15][15],str[N*2];int main(){   while(scanf("%s",str)==1){       int Q; scanf("%d",&Q);       int ans = inf,po = 1;       int l1 = strlen(str);       for(int i=l1,j=0;j<15;i++,j++) str[i]=str[j];       for(int i=1;i<=Q;i++){           scanf("%s",src[i]);           int l2 = strlen(src[i]);           int te = inf;           if(l1 < l2){               for(int j=0;j<l1;j++){                   te = min(te,dp(src[i],str+j,l2,l1));               }           }           else {                te = min(te, dp(src[i],str,l2,l1+min(20,l1)));           }           if(te < ans || (te==ans && strcmp(src[po],src[i])>0)){                ans = te; po = i;           }       }       printf("%s %d\n",src[po],ans);   }   return 0;}



0 0
原创粉丝点击