已经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. ADF成长记1--认识ADF

    2014-07-08 近段时间由于公司项目需要,开始接触Oracle ADF.都说有事没事,上百度,但是对于IT技术而言,上百度还真是不一定好使,至于谷歌嘛,很不巧的进不去了.不过网上ADF的资料当真 ...

  2. axis2 webservice 发布、调用与项目集成

    发布 1.在apache官网下载axis2包,下载Binary Distribution和War Distribution两个zip. 2.将war放入tomcat webapps下部署.并输入 ht ...

  3. Nikto是一款Web安全扫描工具,可以扫描指定主机的web类型,主机名,特定目录,cookie,特定CGI漏洞,XSS漏洞,SQL注入漏洞等,非常强大滴说。。。

    Nikto是一款Web安全扫描工具,可以扫描指定主机的web类型,主机名,特定目录,cookie,特定CGI漏洞,XSS漏洞,SQL注入漏洞等,非常强大滴说... root@xi4ojin:~# cd ...

  4. LSB 简介

    前 Linux 的发行版非常繁多,为了促进 Linux 不同发行版间的兼容性,LSB(Linux Standards Base)开发了一系列标准,使各种软件可以很好地在兼容 LSB 标准的系统上运行, ...

  5. Linux awk命令详解??????????(研究)

    http://blog.chinaunix.net/uid-25120309-id-3801250.html 一.  AWK 说明  awk是一种编程语言,用于在linux/unix下对文本和数据进行 ...

  6. chrome断点续传功能

    刚好找到了一个临时的解决方法,chrome其实已经内部实现了断点续传功能,不过应该还没完善,所以要自己打开.方法:用chrome在地址栏输入chrome://flags用搜索找到resumption( ...

  7. MVC 详细说明

    .NET MVC执行过程: 1.网址路由比对 2.执行Controller与Action 3.执行View并返回结果 在使用MVC中是由IgnoreRoute()辅助方法对比成功的,会导致程序直接跳离 ...

  8. Android Intent不可传递大数据

    今天用intent传递一个bitmap,结果一直出错,intent无法执行,原来是intent不能传递大数据导致的,具体是多大,不太清楚,但我传递的bitmap在1m以上.

  9. 改数(洛谷 U5398)

    题目背景 又是一年NOIP,科学馆的五楼:"我们看下这道题,我们来模拟一下-2,3,5,7,12-这其实就是一个a[i+1]-a[i]=i的序列--"那熟悉的凌波教鞭,熟悉的憨厚的 ...

  10. ASP.Net和新对象之context.Server

    描述 Server是一个HttpServerUtility类型的对象,不是一个类名 1.获取服务器上的绝对路径文件名tring ss = context.Server.MapPath("~/ ...