hdu 4271 动态规划
思路:考的是字符串的编辑距离。在蓝桥杯2012年决赛上出现过。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int dp[][];
char targ[],ans;
int Min(int a,int b,int c)
{
int temp;
temp=a<b?a:b;
return temp<c?temp:c;
}
int getMin(char *str1,char *str2)
{
int l1,l2,i,j;
l1=strlen(str1);
l2=strlen(str2);
for(i=;i<=l2;i++)
dp[][i]=i;
int ans=l2;
for(i=;i<l1;i++)
{
for(j=;j<l2;j++)
{
if(str1[i]==str2[j])
dp[i+][j+]=Min(dp[i][j],dp[i][j+]+,dp[i+][j]+);
else
dp[i+][j+]=Min(dp[i][j+],dp[i+][j],dp[i][j])+;
}
ans=min(ans,dp[i+][l2]);
}
return ans;
}
void solve(char *str1,char *str2)
{
int i,j,l1,l2,t;
l1=strlen(str1);
l2=strlen(str2);
if(l2*<l1)
{
for(i=;i<l1;i++)
str1[i+l1]=str1[i];
str1[i+l1]='\0';
t=getMin(str1,str2);
if(t<=ans)
{
if(t==ans)
{
if(strcmp(targ,str2)>)
strcpy(targ,str2);
}
else
{
strcpy(targ,str2);
ans=t;
}
}
str1[l1]='\0';
}
else
{
char str3[];
for(i=;i<l1;i++)
{
for(j=;j<l1;j++)
{
str3[j]=str1[(i+j)%l1];
}
str3[j]='\0';
t=getMin(str3,str2);
if(t<=ans)
{
if(t==ans)
{
if(strcmp(targ,str2)>)
strcpy(targ,str2);
}
else
{
strcpy(targ,str2);
ans=t;
}
}
}
}
}
int main()
{
char str1[],str2[];
int i,j,n;
while(scanf("%s",&str1)!=EOF)
{
memset(dp,,sizeof(dp));
scanf("%d",&n);
ans=;
int t;
for(i=;i<=n;i++)
{
scanf("%s",str2);
solve(str1,str2);
}
printf("%s %d\n",targ,ans);
}
return ;
}
hdu 4271 动态规划的更多相关文章
- hdu 1087 动态规划之最长上升子序列
http://acm.hdu.edu.cn/showproblem.php?pid=1087 Online Judge Online Exercise Online Teaching Online C ...
- HDU 1003 动态规划
http://acm.hdu.edu.cn/showproblem.php?pid=1003 这几天开始刷动归题目,先来一道签到题 然而做的并不轻松, 没有注意到边界问题, WA了几发才发现 #inc ...
- hdu 4055 && hdu 4489 动态规划
hdu 4055: 一开始我想的递推方向想得很复杂,看了别人的博客后才醍醐灌顶: 参照他的思路和代码: #include<cstdio> #include<cstring> # ...
- hdu 4745 动态规划
思路:特水的一个最长回文子序列动态规划.比赛时硬卡第一题,49WA后终于AC,可惜没时间做这题,结果成绩也就可想而知了.兔子跳一样权值的石头,并且一个正跳,一个反跳,这不就是个回文子序列吗?????! ...
- hdu 4711 动态规划
思路:其实这题是个挺水的动态规划,一开始就能AC,可是不知道错哪了,瞎改瞎交,WA了数十次.AC之后怎么改都是AC,也不知道改了什么地方,郁闷死了~~~难道开始时的测试数据有问题??? dp[i][j ...
- HDU 6076 (动态规划)
HDU 6076 Security Check Problem : 有两个长度为n的队列过安检,每个人有一个特征值.如果两个队列中的第一个人的特征值之差小于等于k,那么一次只能检查其中一个人,否则一次 ...
- HDU 1171 Big Event in HDU (动态规划、01背包)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu 4719 动态规划
思路:dp[i]表示到第i个点为结尾能获得的最大值,那么dp[i]=h[i]*h[i]+dp[i-x]-h[i-x];(i-l<=x<=i);那么我们可以转换下,以dp[i]-h[i]为新 ...
- hdu 3280 动态规划
思路:dp[i][j]表示区间i,j变为回文串最少的代价. #include<map> #include<set> #include<cmath> #include ...
随机推荐
- PL/pgSQL学习笔记之五
http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html 39.3. 声明 块中使用的所有的变量必须在块的声明节中进行声明 ...
- 关闭IE窗口
$a=(New-Object -comObject Shell.Application).Windows() ($a|?{$_.locationname -eq "人力与人才信息管理系统&q ...
- iOS 7 Pushing the Limits Notes - create a layer like notification center's or control center's background
Problem: How to create a layer that looks like your notification center's or control center's backgr ...
- accept函数
accept()函数 系统调用 accept() 会有点古怪的地方的! 你能够想象发生 这种事情:有人从非常远的地方通过一个你在侦听 (listen()) 的port连接 (connect()) 到你 ...
- 翻译学python---《Learn Python the hard Way》---第一章 绪论
打算学习python,但是又不想单纯地看书或是写个小项目,干脆引入很流行的翻译学习法来学习吧- 在论坛上看到了国外的一本<Learn Python the hard Way> ...
- [Node.js] Level 7. Persisting Data
Simple Redis Commands Let's start practicing using the redis key-value store from our node applicati ...
- [Express] Level 5: Route Instance -- refactor the code
Route Instance Let's rewrite our cities routes using a Route Instance. Create a new Route Instance f ...
- 信号之sigprocmask函数
一个进程的信号屏蔽字规定了当前阻塞而不能递送给该进程的信号集.调用函数sigprocmask可以检测或更改其信号屏蔽字,或者在一个步骤中同时执行这两个操作. #include <signal.h ...
- 【PHP代码审计】 那些年我们一起挖掘SQL注入 - 4.全局防护Bypass之二次注入
0x01 背景 现在的WEB程序基本都有对SQL注入的全局过滤,像PHP开启了GPC或者在全局文件common.php上使用addslashes()函数对接收的参数进行过滤,尤其是单引号.二次注入也是 ...
- NLP自然语言处理学习笔记二(初试)
前言: 用Python对自然语言处理有很好的库.它叫NLTK.下面就是对NLTK的第一尝试. 安装: 1.安装Pip 比较简单,得益于CentOS7自带的easy_install.执行一行命令就可以搞 ...