HDU4545+LCS
最长公共子序列。
/*
LCS 最长公共子序列
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<math.h>
using namespace std;
typedef long long ll;
//typedef __int64 int64;
const int maxn = ;
const int inf = 0x7fffffff;
const double pi=acos(-1.0);
const double eps = 1e-;
int dp[ maxn ][ maxn ];
char s1[ maxn ],s2[ maxn ];
bool ss[ ][ ];
//map<char,char>mp;
void init(){
memset( dp,,sizeof( dp ) );
//for( int i=0;i<26;i++ ){
//mp[ 'a'+i ] = '@';
//}
memset( ss,false,sizeof( ss ) );
}
bool same( int x,int y ){
if( s1[x]==s2[y]||ss[s2[y]-'a'][s1[x]-'a']==true ) return true;
else return false;
}
int main(){
int T;
while( scanf("%d",&T)!=EOF ){
int Case = ;
while( T-- ){
scanf("%s",s1);
scanf("%s",s2);
init();
int q;
scanf("%d",&q);
char a[],b[];
while( q-- ){
scanf("%s",a);
scanf("%s",b);
ss[a[]-'a'][b[]-'a']=true;
//mp[ a[0] ] = b[0];
}
int len1 = strlen( s1 );
int len2 = strlen( s2 );
for( int i=;i<=len1;i++ )
dp[ i ][ ] = ;
for( int i=;i<=len2;i++ )
dp[ ][ i ] = ;
for( int i=;i<=len1;i++ ){
for( int j=;j<=len2;j++ ){
if( same( i-,j- )==true )
dp[ i ][ j ] = dp[ i- ][ j- ]+;
else
dp[ i ][ j ] = max( dp[i-][j],dp[i][j-] );
//printf("dp[%d][%d]=%d\n",i,j,dp[i][j]);
}
}
if( dp[len1][len2]!=len1 ) printf("Case #%d: unhappy\n",Case++);
else printf("Case #%d: happy\n",Case++);
}
}
return ;
}
HDU4545+LCS的更多相关文章
- hdu----(4545)魔法串(LCS)
魔法串 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submiss ...
- 我的第一篇博客----LCS学习笔记
LCS引论 在这篇博文中,博主要给大家讲一个算法----最长公共子序列(LCS)算法.我最初接触这个算法是在高中学信息学竞赛的时候.那时候花了好长时间理解这个算法.老师经常说,这种算法是母算法,即从这 ...
- 动态规划之最长公共子序列(LCS)
转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- Hackerrank11 LCS Returns 枚举+LCS
Given two strings, a and , b find and print the total number of ways to insert a character at any p ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- 删除部分字符使其变成回文串问题——最长公共子序列(LCS)问题
先要搞明白:最长公共子串和最长公共子序列的区别. 最长公共子串(Longest Common Substirng):连续 最长公共子序列(Longest Common Subsequence,L ...
- 最大公共字串LCS问题(阿里巴巴)
给定两个串,均由最小字母组成.求这两个串的最大公共字串LCS(Longest Common Substring). 使用动态规划解决. #include <iostream> #inclu ...
- LCS
/**LCS问题*/ #include <iostream>#include <string>#include <algorithm> using namespac ...
随机推荐
- Cocos2d-x实例:设置背景音乐与音效-设置场景实现
设置场景(Setting),Setting.h文件代码如下: #ifndef __Setting_SCENE_H__ #define __Setting_SCENE_H__ #include &quo ...
- The influence of informal governance mechanisms on knowledge integration
Title:The influence of informal governance mechanisms on knowledge integration within cross-function ...
- C++运用SDK截屏
引言 最近有一个需要截取当前屏幕,并保存成BMP文件的需求.整个需求,拆分成三步:1.截取屏幕,获得位图数据.2.配合bmp文件结构信息,将数据整合.3.对整合后的数据做操作,如保存在本地.通过网络传 ...
- [UNIX环境高级编程](第三版)中apue.h的问题
编译 gcc -g myls.c 时,报错 ‘找不到头文件 apue.h’ apue.h是作者自己写的一个文件,系统不自带.其中包含了常用的头文件,以及出错处理函数的定义. 需要到 http://ww ...
- linux 截取时间段的日志
sed -n '/14:[0-9][0-9]:[0-9][0-9]/p’ haproxy.log 截取14点的日志 cat haproxy.log-20160302 | grep xslh ...
- 《APUE》第七章笔记
这一章主要是要解决这么几个问题: 当执行程序时,main函数是如何被调用的? main函数的原型是: int main(int argc, char *argv[]); 其中argc是命令个数,arg ...
- 使用visual studio测试功能进行暴力破解
web项目上线前需要做访问压力测试,奈何对这方面不懂,所以自己网上搜索了下相关工具没有找到合适的,就自己研究了下visual studio 2013中的测试项目,发现还挺好使的,结合数据库还能用做暴力 ...
- 【winform】如何在DateTimePicker中显示时分秒
1. 首先属性里面的Format属性value设置为Custom(默认为Long) 2. 对应的Custom属性value设置为yyyy-MM-dd HH:mm:ss
- mvc的删除
有UserInfo一个实体类,db代表请求上下文 属性有ID,UserName,UserPass,Email var users=from c in db.UserInfo select c; var ...
- th:each
<tr th:each="user,userStat:${users}">userStat是状态变量,有 index,count,size,current,even,o ...