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 ...
随机推荐
- Caesar's Legions(三维dp)
Caesar's Legions Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u S ...
- git寻根——^和~的区别
一. 引子 在git操作中,我们可以使用checkout命令检出某个状态下文件,也可以使用reset命令重置到某个状态,这里所说的“某个状态”其实对应的就是一个提交(commit). 我们可以把一个g ...
- CF#310 d2
A:|c[1]-c[0]| B:A+-(oc)A[0]==0..n-1 C: #include <cstdio> int n,m,i,j,k,p; int ll,ca,cb,cc; int ...
- k Sum | & ||
k Sum Given n distinct positive integers, integer k (k <= n) and a number target. Find k numbers ...
- maven项目 Java compiler level does not match the version of the installed Java project facet
因工作的关系,Eclipse开发的Java项目拷来拷去,有时候会报一个很奇怪的错误.明明源码一模一样,为什么项目复制到另一台机器上,就会报“java compiler level does not m ...
- 【转】基于注解的SpirngMVC简单介绍
转载地址:http://haohaoxuexi.iteye.com/blog/1343761 SpringMVC是一个基于DispatcherServlet的MVC框架,每一个请求最先访问的都是 Di ...
- iOS7上在xib中使用UITableViewController设置背景色bug
今天用xcode5.1设置xib中,用静态的方式设置UITableViewController中的tableview,把tableview中的backgroundColor改变后,xib上有效果,但是 ...
- Java for LeetCode 155 Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Java for LeetCode 026 Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- AJAX 异步交互基本总结
AJAX (Asynchronous JavaScript and Xml) 直译中文 - javascript和XML的异步 同步与异步的区别: 同步交互 执行速度相对比较慢 响应的是完整的HTML ...