题目链接: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. Java虚拟机(一):JVM内存结构

    所有的Java开发人员可能会遇到这样的困惑?我该为堆内存设置多大空间呢?OutOfMemoryError的异常到底涉及到运行时数据的哪块区域?该怎么解决呢?其实如果你经常解决服务器性能问题,那么这些问 ...

  2. vue实现百度搜索下拉提示功能

    这段代码用到vuejs和vue-resouece.实现对接智能提示接口,并通过上下键选择提示项,按enter进行搜索 <!DOCTYPE html> <html lang=" ...

  3. Extended VM Disk In VirtualBox or VMware (虚拟机磁盘扩容)

    First, Clean VM all snapshot, and poweroff your VM. vmdk: vmware-vdiskmanager -x 16GB myDisk.vmdk vd ...

  4. 入门常用SQL及官方文档的使用

    SQL语句基础理论 SQL是操作和检索关系型数据库的标准语言,标准SQL语句可用于操作关系型数据库. 5大主要类型: ①DQL(Data Query Language,数据查询语言)语句,主要由于se ...

  5. Netty权威指南之NIO通信模型

    NIO简介:与Socket和ServerSocket类相对应,NIO提供了SocketChannel和ServerSocketChannel两种不同的套接字通道实现,这两种新通道都支持阻塞和非阻塞两种 ...

  6. Steam安装Google Earth VR

    打开Steam 打开火狐浏览器 输入steam://install/348250

  7. Python迭代器笔记

    python中的三大器有迭代器,生成器,装饰器,本文重点讲解下迭代器的概念,使用,自定义迭代器等的介绍. 1.概念: 迭代器是一个对象,一个可以记住遍历位置的对象,迭代器对象从集合的第一个元素开始访问 ...

  8. jq判断滚动条向上还是向下

    $(document).ready(function(){ ,t=; $(window).scroll(function(e){ p = $(this).scrollTop(); if(t<=p ...

  9. codeforces水题100道 第一题 Codeforces Beta Round #1 A. Theatre Square (math)

    题目链接:http://www.codeforces.com/problemset/problem/1/A题意:至少用多少块边长为a的方块铺满NxM的矩形区域.C++代码: #include < ...

  10. STL源码剖析—顺序容器

    一.vector 1.vector简介: vector的数据安排及其操作方式与数组非常相似,微小的差别在于空间的使用,数组是静态空间,一旦配置了就不能改变.vector是动态空间,随着元素的加入,它的 ...