UVa 1625
来源:互联网 发布:北京淘宝网店老师 编辑:程序博客网 时间:2024/04/30 22:51
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=5050;const int INF=0x3f3f3f3f;int t,n,m;char a[maxn],b[maxn];int dp[maxn][maxn];int s1[27],e1[27],s2[27],e2[27];int main(){ scanf("%d",&t); while(t--) { scanf("%s%s",a+1,b+1); int len1=strlen(a+1); int len2=strlen(b+1); for(int i=0;i<26;i++) { s2[i]=s1[i]=INF; e2[i]=e1[i]=0; } int temp; for(int i=1;i<=len1;i++) { temp=a[i]-'A'; if(s1[temp]==INF) s1[temp]=i;//第temp个字母的起始位置 e1[temp]=i;//第temp个字母的末位置 } for(int i=1;i<=len2;i++) { temp=b[i]-'A'; if(s2[temp]==INF) s2[temp]=i; e2[temp]=i; } for(int i=0;i<=len1;i++) { for(int j=0;j<=len2;j++) { int cnt=0,res=INF; for(int k=0;k<26;k++) { if((i>=s1[k]||j>=s2[k])&&(i<e1[k]||j<e2[k]))//和前面的初始化相对应,这就是为什么初始化要e为0,而s为无穷大的原因了。 cnt++; } if(i>0) res=min(res,dp[i-1][j]); if(j>0) res=min(res,dp[i][j-1]); dp[i][j]=cnt+(res==INF ? 0:res); } } printf("%d\n",dp[len1][len2]); } return 0;}这个题真的是非常的难啊,完全做不出来,看了题解 再自己模拟才慢慢明白了什么意思,这个还是要模拟一下才明白所说的新增的元素每次使得原有的幸存的元素+1,也就是位置查加1,这个需要好好理解啊,收获很大,这个算法还是很厉害的,膜拜啊。
0 0
- UVa 1625
- UVa 1625
- UVA 1625
- Uva-1625-Color Length
- UVA 1625 Color Length
- UVA-1625 Color Length
- uva 1625 color length
- UVA 1625 Color Length
- UVA 1625 Color Length
- UVA 1625 Color Length
- UVa 1625 Color Length
- UVA 1625 Color Length
- UVA 1625 Color Length
- uva
- UVA
- UVA
- UVA
- uva
- TextView 设置行高并垂直居中
- 冷却算法综合排名改进模型-R实现代码
- 找出缺失的整数
- web前端css简单
- 关于Android Studio里的Gradle
- UVa 1625
- HDU 5735 Born Slippy(dp+优化)
- 3.2.5 抖动
- 做QA的日子(3)——学会串讲
- java学习之路-基础语法-运算符
- 【转载】信号量
- 智能指针
- 24点计算器Ruby脚本
- ATL书籍