题目链接:https://hihocoder.com/problemset/problem/1829

时间限制:1000ms
单点时限:1000ms
内存限制:256MB

描述

Lara Croft, the fiercely independent daughter of a missing adventurer, must push herself beyond her limits when she discovers the island where her father disappeared. In this mysterious island, Lara finds a tomb with a very heavy door. To open the door, Lara must input the password at the stone keyboard on the door. But what is the password? After reading the research notes written in her father's notebook, Lara finds out that the key is on the statue beside the door.

The statue is wearing many arm rings on which some letters are carved. So there is a string on each ring. Because the letters are carved on a circle and the spaces between any adjacent letters are all equal, any letter can be the starting letter of the string. The longest common subsequence (let's call it "LCS") of the strings on all rings is the password. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.

For example, there are two strings on two arm rings: s1 = "abcdefg" and s2 = "zaxcdkgb". Then "acdg" is a LCS if you consider 'a' as the starting letter of s1, and consider 'z' or 'a' as the starting letter of s2. But if you consider 'd' as the starting letter of s1 and s2, you can get "dgac" as a LCS. If there are more than one LCS, the password is the one which is the smallest in lexicographical order.

Please find the password for Lara.

输入

There are no more than 10 test cases.

In each case:

The first line is an integer n, meaning there are n (0 < n ≤ 10) arm rings.

Then n lines follow. Each line is a string on an arm ring consisting of only lowercase letters. The length of the string is no more than 8.

输出

For each case, print the password. If there is no LCS, print 0 instead.

样例输入
2
abcdefg
zaxcdkgb
5
abcdef
kedajceu
adbac
abcdef
abcdafc
2
abc
def
样例输出
acdg
acd
0

题意:

给出 $n$ 个环形的字符串,求出它们共同的最长子序列。

不分顺时针逆时针。

题解:

由于数据量较小,考虑选择 $n$ 个串中长度最短的那个串 $s[idx]$,若存在LCS,必然是 $s[idx]$ 的一个子序列(子序列可以不连续),所以按状压的方式枚举 $s[idx]$ 的所有子序列,

对于每个子序列,去其他剩下的 $n-1$ 个串里看是否都能找得到,若是都能找到,说明就是一个公共子序列。

最后,在所有的公共子序列里找到最大的那个,就是LCS,输出时对其字典序排序一下即可。

#include<bits/stdc++.h>
using namespace std; int n;
string s[]; bool Find(const string &big,const string &sma)
{
int len1=big.size(),len2=sma.size();
for(int st=;st<len1;st++)
{
int pos=;
for(int dx=;dx<len1;dx++)
{
if(big[(st+dx)%len1]==sma[pos]) pos++;
}
if(pos>=len2) return ;
pos=;
for(int dx=;dx<big.size();dx++)
{
if(big[(st-dx+len1)%len1]==sma[pos]) pos++;
}
if(pos>=len2) return ;
}
return ;
} int main()
{
while(cin>>n)
{
int m=,idx;
for(int i=;i<=n;i++)
{
cin>>s[i];
if(m>s[i].size()) m=s[i].size(),idx=i;
} int maxi=;
string ans;
for(int sta=;sta<=(<<s[idx].size())-;sta++)
{
string tmp;
int tot=;
for(int i=;i<s[idx].size();i++)
{
if(sta&(<<i))
{
tmp.insert(tmp.end(),,s[idx][i]);
tot++;
}
} bool ok=;
for(int i=;i<=n;i++)
{
if(!Find(s[i],tmp)) ok=;
} if(ok && maxi<tot)
{
maxi=tot;
ans=tmp;
}
} if(maxi>)
{
sort(ans.begin(),ans.end());
cout<<ans<<endl;
}
else cout<<<<endl;
}
}

时间复杂度:设字符串长度为 $L$,则 $O\left( {2^L nL^3 } \right)$。

hihocoder 1829 - 压缩字符串 - [状压+暴力枚举][2018ICPC北京网络预赛B题]的更多相关文章

  1. POJ 3279 - Fliptile - [状压+暴力枚举]

    题目链接:http://poj.org/problem?id=3279 Sample Input 4 4 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 Sample Output 0 ...

  2. Gym 101194L / UVALive 7908 - World Cup - [三进制状压暴力枚举][2016 EC-Final Problem L]

    题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...

  3. 计蒜客 30994 - AC Challenge - [状压DP][2018ICPC南京网络预赛E题]

    题目链接:https://nanti.jisuanke.com/t/30994 样例输入: 5 5 6 0 4 5 1 1 3 4 1 2 2 3 1 3 1 2 1 4 样例输出: 55 样例输入: ...

  4. 计蒜客 31451 - Ka Chang - [DFS序+树状数组][2018ICPC沈阳网络预赛J题]

    题目链接:https://nanti.jisuanke.com/t/31451 Given a rooted tree ( the root is node $1$ ) of $N$ nodes. I ...

  5. [Luogu P3959] 宝藏 (状压DP+枚举子集)

    题面 传送门:https://www.luogu.org/problemnew/show/P3959 Solution 这道题的是一道很巧妙的状压DP题. 首先,看到数据范围,应该状压DP没错了. 根 ...

  6. hihoCoder #1320 : 压缩字符串 区间dp

    /** 题目:hihoCoder #1320 : 压缩字符串 链接:https://hihocoder.com/problemset/problem/1320 描述 小Hi希望压缩一个只包含大写字母' ...

  7. [NYIST32]组合数(状压,枚举,暴力)

    题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=32 求n个数中挑出r个数字的所有情况,最后倒序输出所有情况. 状压枚举所有情况就是了 ...

  8. 状态压缩动态规划 状压DP

    总述 状态压缩动态规划,就是我们俗称的状压DP,是利用计算机二进制的性质来描述状态的一种DP方式 很多棋盘问题都运用到了状压,同时,状压也很经常和BFS及DP连用,例题里会给出介绍 有了状态,DP就比 ...

  9. [CF1234F] Yet Another Substring Reverse - 字符串,状压DP

    CF1234F Yet Another Substring Reverse Description 给定一个字符串,可以任意翻转一个子串,求最终满足所有字符互不相同的子串的最大长度. 数据范围: \( ...

随机推荐

  1. Python基础教程学习笔记:第一章 基础知识

    Python基础教程 第二版 学习笔记 1.python的每一个语句的后面可以添加分号也可以不添加分号:在一行有多条语句的时候,必须使用分号加以区分 2.查看Python版本号,在Dos窗口中输入“p ...

  2. UpLoader------实现上传大文件

    代码: <div id="selectFile">选择文件1</div> <script> var da = newGuid(); var kk ...

  3. 5 -- Hibernate的基本用法 --4 4 数据库方言

    Hibernate底层依然使用SQL语句来执行数据库操作,虽然所有关系数据库都支持使用标准SQL语句,但所有数据库都对标准SQL进行了一些扩展,所以在语法细节上存在一些差异.因此,Hibernate需 ...

  4. SVN的Hooks功能--强制添加注释

    所谓hooks,可以类似 理解Linux内核Netfilter框架的hook点和hook函数的概念.当用户在维护代码的过程中,其执行的相关动作正好触发了相关hook点,就 会去执行对应hook点的脚本 ...

  5. PHP计算两个绝对路径的相对路径

    用PHP计算两个绝对路径的相对路径,该如何求呢? 先根据分隔符切割,然后查找相同 异同点,然后开始有相同点,从相同点结束为止开始拼接剩余部分,没有的话,到达根路径拼接整体. 截图如下: 代码如下: & ...

  6. Mac 使用 launchctl 定时运行程序(转载)

    摘要: 在linux下可以用crontab来定时执行任务,在mac下可以用launchctl来定时执行任务. 在linux下可以用crontab来定时执行任务,在MAC下可以用launchctl来定时 ...

  7. 【代码审计】eduaskcms_v1.0.7前台存储型XSS漏洞分析

      0x00 环境准备 eduaskcms官网:https://www.eduaskcms.xin 网站源码版本:eduaskcms-1.0.7 程序源码下载:https://www.eduaskcm ...

  8. SpringBoot(六)-- 静态资源处理

    1.Spring Boot 的默认资源映射 其中默认配置的 /** 映射到 /static (或/public./resources./META-INF/resources), 其中默认配置的 /we ...

  9. 3dmax osg格式导出插件 osgExp OpenSceneGraph Max Exporter

    https://sourceforge.net/projects/osgmaxexp/files/OpenSceneGraph%20Max%20Exporter/

  10. Push rejected: Push master to origin/master was rejected /failed to push some refs to /git did not exit cleanly

    用studio提交代码报 Push rejected: Push master to origin/master was rejected 用TortiuseGit提交代码报下面错,(我是用这种方法解 ...