UVa 10192 - Vacation & UVa 10066 The Twin Towers ( LCS 最长公共子串)
题意:给定两个字符串。求最长公共子串的长度
思路:这个是最长公共子串的直接应用
#include<stdio.h>
#include<string.h>
int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
char s[105],t[105];
int i,j,k=0,m,n,dp[105][105];
while(gets(s)!=NULL){
if(strcmp(s,"#")==0)
break;
k++;
gets(t);
m=strlen(s);
n=strlen(t);
for(i=0;i<m;i++)
dp[i][0]=0;
for(i=0;i<n;i++)
dp[0][i]=0;
for(i=0;i<m;i++)
for(j=0;j<n;j++){
if(s[i]==t[j])
dp[i+1][j+1]=dp[i][j]+1;
else
dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]);
}
printf("Case #%d: you can visit at most %d cities.\n",k,dp[m][n]);
}
return 0;
}
option=com_onlinejudge&Itemid=8&category=12&page=show_problem&problem=1007">链接:UVa 10066
题意:求给定两个字符串的最长公共子串
#include<stdio.h>
int m,n,a[105],b[105],dp[105][105];
int max(int a,int b)
{
return a>b? a:b;
}
void LCS()
{
int i,j;
for(i=1;i<=m;i++)
dp[i][1]=0;
for(j=1;j<=n;j++)
dp[1][j]=0;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++){
if(a[i]==b[j])
dp[i+1][j+1]=dp[i][j]+1;
else
dp[i+1][j+1]=max(dp[i][j+1],dp[i+1][j]);
}
}
int main()
{
int i,k=0;
while(scanf("%d%d",&m,&n)!=EOF){
if(m==0&&n==0)
break;
k++;
for(i=1;i<=m;i++)
scanf("%d",&a[i]);
for(i=1;i<=n;i++)
scanf("%d",&b[i]);
LCS();
printf("Twin Towers #%d\n",k);
printf("Number of Tiles : %d\n\n",dp[m+1][n+1]);
}
return 0;
}
UVa 10192 - Vacation & UVa 10066 The Twin Towers ( LCS 最长公共子串)的更多相关文章
- uva 10066 The Twin Towers (最长公共子)
uva 10066 The Twin Towers 标题效果:最长公共子. 解题思路:最长公共子. #include<stdio.h> #include<string.h> # ...
- uva 10192 Vacation(最长公共子)
uva 10192 Vacation The Problem You are planning to take some rest and to go out on vacation, but you ...
- UVA.10192 Vacation (DP LCS)
UVA.10192 Vacation (DP LCS) 题意分析 某人要指定旅游路线,父母分别给出了一系列城市的旅游顺序,求满足父母建议的最大的城市数量是多少. 对于父母的建议分别作为2个子串,对其做 ...
- UVA.10066 The Twin Towers (DP LCS)
UVA.10066 The Twin Towers (DP LCS) 题意分析 有2座塔,分别由不同长度的石块组成.现在要求移走一些石块,使得这2座塔的高度相同,求高度最大是多少. 问题的实质可以转化 ...
- UVA 10066 The Twin Towers
裸最长公共子序列 #include<time.h> #include <cstdio> #include <iostream> #include<algori ...
- UVA 10066 The Twin Towers(LCS)
Problem B The Twin Towers Input: standard input Output: standard output Once upon a time, in an anci ...
- UVA 10192 Vacation
裸最长公共子序列 #include<time.h> #include <cstdio> #include <iostream> #include<algori ...
- LightOJ1126 Building Twin Towers(DP)
题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1126 Description Professor Sofdor Al ...
- The Twin Towers zoj2059 DP
The Twin Towers Time Limit: 2 Seconds Memory Limit: 65536 KB Twin towers we see you standing ta ...
随机推荐
- ACM-The Coco-Cola Store
题目: Once upon a time, there is a special coco-cola store. If you return three empty bottles to the s ...
- bootstrap3基本了解
使用 BootCDN 提供的免费 CDN 加速服务(同时支持 http 和 https 协议) Bootstrap 中文网 为 Bootstrap 专门构建了免费的 CDN 加速服务,访问速度更快.加 ...
- PHP持久进程
在有些业务需求中,一个业务逻辑会涉及很多其他模块,这时可以把不需要返回的数据,扔到后台异步处理(比如注册时邮件验证,发邮件这个过程就可以扔到后台处理). 这个时候可以在后台起一个PHP进程,轮循处理业 ...
- 使用vim进行java编程
首先:编写源代码Test.java 1class Test{ ...
- (十一)Ubuntu下面怎么找到一个软件安装的目录,卸载软件
aptitude show packagename 实例: aptitude show sublime-text-installer 可以看到这个软件一系列信息 dpkg命令 dpkg -l //列车 ...
- Xamarin for Visual Studio 3.11.590 稳定版 破解补丁 Version 3
前提概要 全新安装请参考 安装 Xamarin for Visual Studio. Release Log 3.11.590 此版本是紧急修复(HotFix)版,重点改善了 build-tool 及 ...
- codeforces-103B
题目连接:http://codeforces.com/contest/103/problem/B B. Cthulhu time limit per test 2 seconds memory lim ...
- 给出一个string字符串,统计里面出现的字符个数
给出一个string字符串,统计里面出现的字符个数 解决方案: 使用algorithm里面的count函数,使用方法是count(begin,end,'c'),其中begin指的是起始地址,end指的 ...
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] A. Raising Bacteria【位运算/二进制拆分/细胞繁殖,每天倍增】
A. Raising Bacteria time limit per test 1 second memory limit per test 256 megabytes input standard ...
- UVA 1395 Slim Span 最小生成树
题意: 给你一个图,让你求这个图中所有生成树中满足题目条件的,这个条件是生成树中最长边与最短边的差值最小. 思路: 根据最小瓶颈生成树的定义:在一个有权值的无向图中,求一个生成树最大边的权值尽量小.首 ...