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
- HDU - 4271(经典字符串dp改)
- hdu 1421 经典DP
- hdu 1227(经典dp)
- hdu 1421经典dp
- HDU 1069 DP 经典
- HDU 1003 经典DP
- HDU 6170 字符串Dp
- hdu 4035 经典期望dp
- hdu 2196(经典树形dp)
- dp经典题目 hdu 2084
- hdu 4433 字符串变化 DP
- HDU 2196 Computer 经典树形DP
- HDU——1227(经典DP)
- HDU 2196 Computer(树形dp经典)
- HDU 2196 Computer(经典树形DP)
- hdu 4616 Game ( 经典树形dp )
- hdu 4455 Substrings (经典DP)
- HDU - 1421 搬寝室(经典DP)
- 二进制文件与文本文件区别
- hdu3622 2-SAT+二分
- Gym 100641 H Time Warp || 追击问题
- JavaScript语法
- POJ-3250-Bad Hair Day- 又是一颗单调栈
- HDU - 4271(经典字符串dp改)
- 自定义 CheckBox样式
- 窗口中的对象和元素
- shwoModalDialog弹出窗的属性
- Spring+Axis1.4开发WebService实例
- java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- 内置对象 DOM BOM
- CvMat、Mat、IplImage之间的转换
- Android性能优化典范(一)