思路:考的是字符串的编辑距离。在蓝桥杯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 动态规划的更多相关文章

  1. hdu 1087 动态规划之最长上升子序列

    http://acm.hdu.edu.cn/showproblem.php?pid=1087 Online Judge Online Exercise Online Teaching Online C ...

  2. HDU 1003 动态规划

    http://acm.hdu.edu.cn/showproblem.php?pid=1003 这几天开始刷动归题目,先来一道签到题 然而做的并不轻松, 没有注意到边界问题, WA了几发才发现 #inc ...

  3. hdu 4055 && hdu 4489 动态规划

    hdu 4055: 一开始我想的递推方向想得很复杂,看了别人的博客后才醍醐灌顶: 参照他的思路和代码: #include<cstdio> #include<cstring> # ...

  4. hdu 4745 动态规划

    思路:特水的一个最长回文子序列动态规划.比赛时硬卡第一题,49WA后终于AC,可惜没时间做这题,结果成绩也就可想而知了.兔子跳一样权值的石头,并且一个正跳,一个反跳,这不就是个回文子序列吗?????! ...

  5. hdu 4711 动态规划

    思路:其实这题是个挺水的动态规划,一开始就能AC,可是不知道错哪了,瞎改瞎交,WA了数十次.AC之后怎么改都是AC,也不知道改了什么地方,郁闷死了~~~难道开始时的测试数据有问题??? dp[i][j ...

  6. HDU 6076 (动态规划)

    HDU 6076 Security Check Problem : 有两个长度为n的队列过安检,每个人有一个特征值.如果两个队列中的第一个人的特征值之差小于等于k,那么一次只能检查其中一个人,否则一次 ...

  7. 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 ...

  8. 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]为新 ...

  9. hdu 3280 动态规划

    思路:dp[i][j]表示区间i,j变为回文串最少的代价. #include<map> #include<set> #include<cmath> #include ...

随机推荐

  1. Web CORS 跨域方式使用方式

    CORS 参考 http://enable-cors.org/index.html https://help.aliyun.com/document_detail/oss/practice/cors_ ...

  2. 导入导出Excel

    最近需要频繁的使用导入导出,各么,又不想使用ms的PIA,在4.0以下,存在版本兼容的问题. 于是网上查找了很久,找到两款开源的excel组件. 1.CSharpJExcel,这是JExcel的.ne ...

  3. 11.聚合(Aggregation)

    聚合关系是关联关系的一种特例,它体现的是整体与部分的关系,即has-a的关系,此时整体与部分之间是可分离的,它们可以具有各自的生命周期.比如计算机与CPU.公司与员工的关系等.表现在代码层面,和关联关 ...

  4. Ehcache(04)——设置缓存的大小

    http://haohaoxuexi.iteye.com/blog/2116749 设置缓存的大小 目录 1     CacheManager级别 2     Cache级别 3     大小衡量 4 ...

  5. MON166 User's Guide

    MON166 is a debug monitor for C16x and ST10 user programs. It consists of: A configurable monitor pr ...

  6. PhoneGap+jQuery Mobile+Rest 访问远程数据

    最近研究Mobile Web技术.发现了一个好东西-PhoneGap! 发现用PhoneGap+jQuery Mobile是一个很完美的组合! 本实例通俗易懂.适合广大开发人群:高富帅.白富美.矮穷戳 ...

  7. [Node.js] Broswerify -- 2

    Browserify allows you to leverage 10s of thousands of javascript modules available in the Node Packa ...

  8. 清除SQL Server 2008中登陆时的历史记录

    win7 在地址栏直接输入下面路径,删除SqlStudio.bin文件%AppData%\Microsoft\Microsoft SQL Server\100\Tools\Shell

  9. RMI、RPC、SOAP通讯技术介绍及比对 - XML/SOAP

    RMI.RPC.SOAP通信技术介绍及比对 1.RMI 使用java的程序员,对于RMI(RemoteMethod Invoke,远程方法调用)一定不陌生,在java中,为了在分布式应用开发时,能够方 ...

  10. Ubuntu下安装可视化SVN客户端Rabbitvcs

    如果你用过Windows下的tortoisesvn,肯定会感叹,同样是开源程序,为什么这些开源的东西不在开源的系统上先跑呢? 不用着急,那边有个乌龟,这篇有只兔子,只是看了太多的龟兔赛跑的故事,不知到 ...