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 ...
随机推荐
- MySQL之aborted connections和aborted clients
影响Aborted_clients 值的可能是客户端连接异常关闭,或wait_timeout值过小. 最近线上遇到一个问题,接口日志发现有很多超时报错,根据日志定位到数据库实例之后发现一切正常,一般来 ...
- HNU 12812 Broken Audio Signal
题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12812 一直弄错了一个题意,教训啊,比赛最怕的就是弄错题意了 ...
- [Effective JavaScript 笔记]第60条:支持方法链
无状态的API的部分能力是将复杂操作分解为更小的操作的灵活性.一个很好的例子是字符串的replace方法.由于结果本身也是字符串,可以对前一个replace操作重复执行替换.这种模式的一个常见用例是在 ...
- ■Ascii逐字解码法注入,mysql5.0一下版本手工注入
/*By:珍惜少年时*/ 逐字解码法,不一定非要猜字段内容.库名,表名,字段,data,都能猜. 环境过滤了select.union(mysql5.0以下的版本就不支持union所以也可以用此方法), ...
- nginx: [emerg] getpwnam(“www”) failed
在配置nginx 时提示如下错误时:nginx: [emerg] getpwnam(“www”) failed 解决方案一 在nginx.conf中 把user nobody的注释去掉既可 解决方案二 ...
- luarocks install with lua5.1 and luajit to install lapis
# in luarocks source directory...git clone https://github.com/archoncap/luarockscd luarocks ./config ...
- Install ssdb-rocks on CentOS 6
Install ssdb-rocks on CentOS 6 C.C. 发表于 2014年08月10日 20:14 | Hits: 649 为了优化节操精选的弹幕系统,打算更换到Facebook的R ...
- PHP中冒号、endif、endwhile、endfor这些都是什么
我们经常在wordpress一类博客程序的模板里面看到很多奇怪的PHP语法,比如:<?php if(empty($GET_['a'])): ?><font color="r ...
- Power of Two & Power of Three & Power of Four
Check Power of 2 Using O(1) time to check whether an integer n is a power of 2. Example For n=4, re ...
- vs2013秘钥
Ultimate:BWG7X-J98B3-W34RT-33B3R-JVYW9 Premium:FBJVC-3CMTX-D8DVP-RTQCT-92494 YKCW6-BPFPF-BT8C9-7DCT ...