已经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的更多相关文章

  1. UVA.10192 Vacation (DP LCS)

    UVA.10192 Vacation (DP LCS) 题意分析 某人要指定旅游路线,父母分别给出了一系列城市的旅游顺序,求满足父母建议的最大的城市数量是多少. 对于父母的建议分别作为2个子串,对其做 ...

  2. UVA.10066 The Twin Towers (DP LCS)

    UVA.10066 The Twin Towers (DP LCS) 题意分析 有2座塔,分别由不同长度的石块组成.现在要求移走一些石块,使得这2座塔的高度相同,求高度最大是多少. 问题的实质可以转化 ...

  3. UVA-1625-Color Length(DP LCS变形)

    Color Length(UVA-1625)(DP LCS变形) 题目大意 输入两个长度分别为n,m(<5000)的颜色序列.要求按顺序合成同一个序列,即每次可以把一个序列开头的颜色放到新序列的 ...

  4. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  5. LightOJ1033 Generating Palindromes(区间DP/LCS)

    题目要计算一个字符串最少添加几个字符使其成为回文串. 一年多前,我LCS这道经典DP例题看得还一知半解时遇到一样的问题,http://acm.fafu.edu.cn/problem.php?id=10 ...

  6. poj 1159 (DP LCS)

    滚动数组 + LCS // File Name: 1159.cpp // Author: Missa_Chen // Created Time: 2013年07月08日 星期一 10时07分13秒 # ...

  7. [DP] LCS小结

    额..失误.. LCS是Longest Common Subsequence的缩写,即最长公共子序列.一个序列,如果是两个或多个已知序列的子序列,且是所有子序列中最长的,则为最长公共子序列. DP.O ...

  8. poj1080--Human Gene Functions(dp:LCS变形)

    Human Gene Functions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17206   Accepted:  ...

  9. UVA 531 - Compromise(dp + LCS打印路径)

      Compromise  In a few months the European Currency Union will become a reality. However, to join th ...

随机推荐

  1. [Effective JavaScript 笔记]第39条:不要重用父类的属性名

    假设想给上节讲的场景图库添加收集诊断信息的功能.这对于调试和性能分析很有用. 38条示例续 给每个Actor实例一个唯一的标识数. 添加标识数 function Actor(scene,x,y){ t ...

  2. webservice远程调试开启

    在.NET 中已经默认将WEBSERVICE的远程调试功能关闭,有的时候我们需要远程调试程序的时候,就需要打开此功能我们只需在WEBSERVICE的项目的中添web.config的<system ...

  3. 什么是mixin

    转自:http://guangboo.org/2013/01/28/python-mixin-programming http://en.wikipedia.org/wiki/Mixin http:/ ...

  4. sharepoint更新多行文本webparth

    前台 <script> function Copy() { var value = document.getElementById("<%=BodyBox.ClientID ...

  5. Apache配置文件中的deny和allow的使用

    Apache配置文件中的deny和allow的使用 由于产品的需要,最近在配置apache的负载均衡功能,但是在配置虚拟主机的访问权限的时候我们遇到了一些问题.主要问题是deny和allow的执行顺序 ...

  6. Android Studio项目整合PullToRefresh的问题记录

    PullToRefresh下拉刷新在App中应用非常频繁,然而PullToRefresh是在ADT下开发完成的.如果要将其整合到Android Studio目录下的话颇费周折.前面的文章“Androi ...

  7. iOS8 UILocalNotification 和 UIRemoteNotification 使用注意 草稿,正在整理中。。。。

    先说一个关于UILocalNotification的知识点,容易被忘记: Each app on a device is limited to 64 scheduled local notificat ...

  8. iOS 和 Android 中的Alert

    iOS 和 Android中都有alert这种提示框,下面简单介绍下. ios中的alert叫做UIAlertView,共有4种样式,由于在ios7上,自定义alertview不太好用,所以也就这4种 ...

  9. 项目总结(五)--- 界面调试工具Reveal

    在开发中,我们也许会碰到以下需求:对于一些动态复杂的交互界面,手码去制定界面是常有的事情,然而我们在开发中想修改过一些参数后想看下实时效果,只能重新运行项目,进入到对应的页面来进行修改,是不是有点麻烦 ...

  10. 侃侃前端MVC设计模式

    前言 前端的MVC,近几年一直很火,大家也都纷纷讨论着,于是乎,抽空总结一下这个知识点.看了些文章,结合实践略作总结并发表一下自己的看法. 最初接触MVC是后端Java的MVC架构,用一张图来表示之— ...