ZOJ 3603 DP LCS
已经5年没有做OJ了,
曾经沧海难为水,除去巫山不是云“
准备每周刷1-2题!
题目大意:给出N个字符串,且各个字符串都包含唯一的字母,即不存在“ABCA”(A重复了),而“AFDSG”是正确的。
求出N个字符串的公共字母。 最后,按照字典序输出。
分 析:首先对各个字符串进行字典序排序,然后求所有的LCS,做法是两两相求即可。N个字符串,总共求N-1次LCS,就得到最后的结果了。
代 码:
//http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3603
#include<iostream>
#include<stack>
#include<cstdio>
#include<algorithm>
#include<string>
#include<fstream> using namespace std; int matrix[14][14];
int f[14][14]; string result = "";
void subSequence(int i, int j, string str)
{
if (i == 0 || j == 0) return;
if (f[i][j] == 1)
{
result = str[i - 1] + result;
subSequence(i - 1, j - 1, str);
}
else
{
if (f[i][j] == 2)
{
subSequence(i - 1, j, str);
}
else
{
subSequence(i, j - 1, str);
}
}
} string LCS(string str1, string str2)
{
//初始化边界,过滤掉0的情况
for (int i = 0; i <= str1.size(); i++)
matrix[i][0] = 0; for (int j = 0; j <= str2.size(); j++)
matrix[0][j] = 0; for (int i = 0; i < 22; i++)
{
for (int j = 0; j < 22; j++)
{
f[i][j] = 0;
}
} //填充矩阵
for (int i = 1; i <= str1.size(); i++)
{
for (int j = 1; j <= str2.size(); j++)
{
if (str1[i - 1] == str2[j - 1])
{
matrix[i][j] = matrix[i - 1][j - 1] + 1;
f[i][j] = 1;
}
else
{
if (matrix[i - 1][j] >= matrix[i][j - 1])
{
matrix[i][j] = matrix[i - 1][j];
f[i][j] = 2;
}
else
{
matrix[i][j] = matrix[i][j - 1];
f[i][j] = 3;
}
}
}
}
result = "";
subSequence(str1.size(), str2.size(), str1);
return result;
} int main()
{
//fstream cin("3603.txt");
int T, n;
cin >> T;
while (T--)
{
cin >> n;
stack<string> st = stack<string>();
while (!st.empty())
{
st.pop();
} string tmp;
for (int i = 0; i < n; i++)
{
cin >> tmp;
sort(tmp.begin(), tmp.end()); if (!st.empty())
{
string inner = st.top();
st.pop();
st.push(LCS(inner, tmp));
}
else
{
st.push(tmp);
}
}
cout << st.top() << endl;
}
return 0;
}
ZOJ 3603 DP LCS的更多相关文章
- 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-1625-Color Length(DP LCS变形)
Color Length(UVA-1625)(DP LCS变形) 题目大意 输入两个长度分别为n,m(<5000)的颜色序列.要求按顺序合成同一个序列,即每次可以把一个序列开头的颜色放到新序列的 ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- LightOJ1033 Generating Palindromes(区间DP/LCS)
题目要计算一个字符串最少添加几个字符使其成为回文串. 一年多前,我LCS这道经典DP例题看得还一知半解时遇到一样的问题,http://acm.fafu.edu.cn/problem.php?id=10 ...
- poj 1159 (DP LCS)
滚动数组 + LCS // File Name: 1159.cpp // Author: Missa_Chen // Created Time: 2013年07月08日 星期一 10时07分13秒 # ...
- [DP] LCS小结
额..失误.. LCS是Longest Common Subsequence的缩写,即最长公共子序列.一个序列,如果是两个或多个已知序列的子序列,且是所有子序列中最长的,则为最长公共子序列. DP.O ...
- poj1080--Human Gene Functions(dp:LCS变形)
Human Gene Functions Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17206 Accepted: ...
- UVA 531 - Compromise(dp + LCS打印路径)
Compromise In a few months the European Currency Union will become a reality. However, to join th ...
随机推荐
- ruby实时查看日志
(文章是从我的个人主页上粘贴过来的, 大家也可以访问我的主页 www.iwangzheng.com) 在调试代码的时候,把日志文件打开,边操作边调试能很快帮助我们发现系统中存在的问题. $tail r ...
- PHP学习之一晚撸下W3chscool
PHP 多维数组 其实简单的而言,多维数组就是由单个的数组组成的,两个数组嵌套组成一个二维数组,三个顾名思义就是三维数组. 先来一个简单的数组. 数字是key,引号里的是value <?php ...
- apache2:Invalid option to WSGI daemon process definition
版本说明: ubuntu 12.04 server /apache 2.2 / mod_wsgi 3.3 / python 2.7.3 /django 1.7 在ubuntu12的服务器上配置djan ...
- jQuery mobile 开发问题记录
一.动态加载页面问题 1.存在这样一个页面布局: main.html 为主界面A,B为该页面中的三个page,其中A为splitview左部分页面,B为右半部页面 a1.html 为一个独立的页面 a ...
- 使用pymongo需要手动关闭MongoDB Connection吗?
答:Disconnecting will close all underlying sockets in the connection pool. If this instance is used a ...
- 《ASP.NET1200例》C# WINFORM程序的三层架构如何建立的。
先添加-新建项目-windows应用程序,然后在右边的解决方案资源管理器上面,在当前的解决方案上面右击,点,添加-新建项目-类库,分别建立.DAL,BLL,Model三个项目,然后,在DAL项目上右击 ...
- Counting Bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...
- Android runProguard配置 导致module lib 中的包编译时无法识别
今天写代码时用到了另一个lib型的工程,把它添加到dependencies后,在原工程中可以引用lib中的文件了,但是编译时就会报错,提示包不存在,后来在build.gradle中设置runProgu ...
- iOS7 status bar 样式问题
在ios7中,有如下status bar 样式 typedef NS_ENUM(NSInteger, UIStatusBarStyle) { UIStatusBarStyleDefault = , / ...
- 利用FFmpeg生成视频缩略图 2.3.1
1.下载FFmpeg文件包,解压包里的\bin\下的文件解压到 D:\ffmpeg\ 目录下. 下载地址 http://ffmpeg.zeranoe.com/builds/win32/static/ ...