Advanced Fruits(HDU 1503 LCS变形)
Advanced Fruits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2358 Accepted Submission(s): 1201
Special Judge
A big topic of discussion inside the company is "How should the new creations be called?" A mixture between an apple and a pear could be called an apple-pear, of course, but this doesn't sound very interesting. The boss finally decides to use the shortest string that contains both names of the original fruits as sub-strings as the new name. For instance, "applear" contains "apple" and "pear" (APPLEar and apPlEAR), and there is no shorter string that has the same property.
A combination of a cranberry and a boysenberry would therefore be called a "boysecranberry" or a "craboysenberry", for example.
Your job is to write a program that computes such a shortest name for a combination of two given fruits. Your algorithm should be efficient, otherwise it is unlikely that it will execute in the alloted time for long fruit names.
Input is terminated by end of file.
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define Max 102
int dp[Max][Max];
int mark[Max][Max];
char s[Max],t[Max];
int len1,len2;
void LCS() //计算LCS,并用mark标记数组记录dp数组的传递过程
{
int i,j;
memset(mark,,sizeof(mark));
memset(dp,,sizeof(dp));
for(i=;i<=len1;i++)
{
for(j=;j<=len2;j++)
{
if(s[i-]==t[j-])
{
dp[i][j]=dp[i-][j-]+;
// cout<<s[i-1]<<" ";
mark[i][j]=;
}
else if(dp[i-][j]>=dp[i][j-])
{
dp[i][j]=dp[i-][j]; //从上面传递下来
mark[i][j]=;
}
else
{
dp[i][j]=dp[i][j-]; //从左边传递下来
mark[i][j]=;
}
}
} return;
}
void output(int i,int j) //回溯输出
{
/*if(i==0&&j!=0)
{
output(i,j-1);
//printf("%c",t[j-1]);
}
else if(i!=0&&j==0)
{
output(i-1,j);
//printf("%c",s[i-1]);
}
else if(i==0&&j==0)
return;*/
if(i==||j==)
return;
if(mark[i][j]==)
{
output(i-,j-);
printf("%c",s[i-]);
}
else if(mark[i][j]==)
{
output(i-,j);
//printf("%c",s[i-1]);
}
else
{
output(i,j-);
//printf("%c",t[j-1]);
}
return;
}
int main()
{
int i,j;
freopen("in.txt","r",stdin);
while(scanf("%s%s",s,t)!=EOF)
{
len1=strlen(s),len2=strlen(t);
LCS();
output(len1,len2);
printf("\n");
}
}
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define Max 102
int dp[Max][Max];
int mark[Max][Max];
char s[Max],t[Max];
int len1,len2;
void LCS() //计算LCS,并用mark标记数组记录dp数组的传递过程
{
int i,j;
memset(mark,,sizeof(mark));
memset(dp,,sizeof(dp));
for(i=;i<=len1;i++)
{
for(j=;j<=len2;j++)
{
if(s[i-]==t[j-])
{
dp[i][j]=dp[i-][j-]+;
// cout<<s[i-1]<<" ";
mark[i][j]=;
}
else if(dp[i-][j]>=dp[i][j-])
{
dp[i][j]=dp[i-][j]; //从上面传递下来
mark[i][j]=;
}
else
{
dp[i][j]=dp[i][j-]; //从左边传递下来
mark[i][j]=;
}
}
} return;
}
void output(int i,int j) //回溯输出
{
if(i==&&j!=)
{
output(i,j-);
printf("%c",t[j-]);
}
else if(i!=&&j==)
{
output(i-,j);
printf("%c",s[i-]);
}
else if(i==&&j==)
return;
else if(mark[i][j]==)
{
output(i-,j-);
printf("%c",s[i-]);
}
else if(mark[i][j]==)
{
output(i-,j);
printf("%c",s[i-]);
}
else
{
output(i,j-);
printf("%c",t[j-]);
}
return;
}
int main()
{
int i,j;
freopen("in.txt","r",stdin);
while(scanf("%s%s",s,t)!=EOF)
{
len1=strlen(s),len2=strlen(t);
LCS();
output(len1,len2);
printf("\n");
}
}
Advanced Fruits(HDU 1503 LCS变形)的更多相关文章
- hdu 1503 LCS输出路径【dp】
hdu 1503 不知道最后怎么输出,因为公共部分只输出一次.有人说回溯输出,感觉好巧妙!其实就是下图,输出的就是那条灰色的路径,但是初始时边界一定要初始化一下,因为最第一列只能向上走,第一行只能向左 ...
- Advanced Fruits HDU杭电1503【LCS的保存】
Problem Description The company "21st Century Fruits" has specialized in creating new sort ...
- hdu 1503, LCS variants, find a LCS, not just the length, backtrack to find LCS, no extra markup 分类: hdoj 2015-07-18 16:24 139人阅读 评论(0) 收藏
a typical variant of LCS algo. the key point here is, the dp[][] array contains enough message to de ...
- hdu 1080(LCS变形)
Human Gene Functions Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- hdu 1243(LCS变形)
反恐训练营 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- hdu 1503:Advanced Fruits(动态规划 DP & 最长公共子序列(LCS)问题升级版)
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu 1503 Advanced Fruits(最长公共子序列)
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 最长公共子序列(加强版) Hdu 1503 Advanced Fruits
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu 1503 Advanced Fruits 最长公共子序列 *
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- Linux 结束占用端口的程序
[root@ucit ~]# lsof -i:80COMMAND PID USER FD TYPE DEVICE SIZE NODE NAMEjava 1839 root ...
- 开心菜鸟学习系列学习笔记------------nodejs util公共函数
global 在最外层定义的变量: 全局对象的属性: 隐式定义的变量(未定义直接赋值的变量). 一.process process 是一个全局变量,即 global 对象的属性 ...
- 转:MVC分页
原文地址:http://www.cnblogs.com/iamlilinfeng/p/4075292.html 分页总是搞得我很烦,也是因为刚接触,貌似有好多插件,之前在用一个,可是后来发现一翻页原来 ...
- mysqli connect database and print
<?php $connect = mysqli_connect('localhost','root','','intertrading') or die('Unale to connect'); ...
- QQ聊天界面的布局和设计(IOS篇)-第二季
QQChat Layout - 第二季 本来第二季是快写好了, 也花了点功夫, 结果gitbook出了点问题, 给没掉了.有些细节可能会一带而过, 如有疑问, 相互交流进步~. 在第一季中我们完成了Q ...
- 线程篇-01-NSThread
一.概述 1.使用NSThread创建线程的三种方式和区别. 二.核心 2.1 NSThread创建线程的三种方式和区别. 主要有NSThread对象的创建线程的类方法detachNewThreadS ...
- 关于Redis
在同步dump.rdb文件时要执行service redis stop后,再拷贝目标rdb文件过去,然后再start 而不是拷贝目标rdb文件过去后直接执行restart 因为redis在执行sto ...
- Hive 10、Hive的UDF、UDAF、UDTF
Hive自定义函数包括三种UDF.UDAF.UDTF UDF(User-Defined-Function) 一进一出 UDAF(User- Defined Aggregation Funcation) ...
- WEB应用知识一二三
1.HTTP协议 |--基于请求(Request)和响应(Response)的无状态通讯协议 浏览器和WEB应用程序通过HTTP进行通信.客户端通过URL对指定服务器要求特定位置的数据 |--POST ...
- 炉石传说__multiset
炉石传说 Problem Description GG学长虽然并不打炉石传说,但是由于题面需要他便学会了打炉石传说.但是传统的炉石传说对于刚入门的GG学长来说有点复杂,所以他决定自己开发一个简化版 ...