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

本题目就是给定最多是个小串,和一母串,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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值