hihocoder 1829 - 压缩字符串 - [状压+暴力枚举][2018ICPC北京网络预赛B题]
题目链接:https://hihocoder.com/problemset/problem/1829
描述
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题]的更多相关文章
- 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 ...
- 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 ...
- 计蒜客 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 样例输入: ...
- 计蒜客 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 ...
- [Luogu P3959] 宝藏 (状压DP+枚举子集)
题面 传送门:https://www.luogu.org/problemnew/show/P3959 Solution 这道题的是一道很巧妙的状压DP题. 首先,看到数据范围,应该状压DP没错了. 根 ...
- hihoCoder #1320 : 压缩字符串 区间dp
/** 题目:hihoCoder #1320 : 压缩字符串 链接:https://hihocoder.com/problemset/problem/1320 描述 小Hi希望压缩一个只包含大写字母' ...
- [NYIST32]组合数(状压,枚举,暴力)
题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=32 求n个数中挑出r个数字的所有情况,最后倒序输出所有情况. 状压枚举所有情况就是了 ...
- 状态压缩动态规划 状压DP
总述 状态压缩动态规划,就是我们俗称的状压DP,是利用计算机二进制的性质来描述状态的一种DP方式 很多棋盘问题都运用到了状压,同时,状压也很经常和BFS及DP连用,例题里会给出介绍 有了状态,DP就比 ...
- [CF1234F] Yet Another Substring Reverse - 字符串,状压DP
CF1234F Yet Another Substring Reverse Description 给定一个字符串,可以任意翻转一个子串,求最终满足所有字符互不相同的子串的最大长度. 数据范围: \( ...
随机推荐
- 【动态规划】数字分组I
[动态规划]数字分组I 时间限制: 1 Sec 内存限制: 64 MB提交: 10 解决: 6[提交][状态][讨论版] 题目描述 给出一堆魔法石的重量,问如何分成两堆,使得它们质量和之差最小,求 ...
- 标准JSON格式定义与解析注意点
标准JSON格式定义与解析注意点 在JS.IOS.Android中都内置了JSON的序列化.反序列化SDK.JEE中也可以使用第三方的JSON解析库,如GSON.虽然在JSON格式被定义出来的时候并没 ...
- 表单验证<AngularJs>
经常使用的表单验证指令 1. 必填项验证 某个表单输入是否已填写,仅仅要在输入字段元素上加入HTML5标记required就可以: <input type="text" re ...
- 7 -- Spring的基本用法 -- 10... 获取其他Bean的属性值;获取Field值;获取任意方法的返回值
7.10 高级依赖关系配置 组件与组件之间的耦合,采用依赖注入管理:但基本类型的成员变量值,应直接在代码中设置. Spring支持将任意方法的返回值.类或对象的Field值.其他Bean的getter ...
- 判断资源贴图是否有alpha
/* modfly selected textures`s maxSize and ImportFormat bool hasAlpha = true; if(hasAlpha)then(textur ...
- Elastic Search 5.4.3 java api 入门
首先介绍一点,es的版本从之前的2.x跳跃到5.x,很多插件要保持一致,不然会产生很多版本不兼容的问题. 首先看一个demo先熟悉一下, 具体代码在git服务器上: https://github.co ...
- 《转载》图解Tomcat类加载机制
本文转载自http://www.cnblogs.com/xing901022/p/4574961.html 说到本篇的tomcat类加载机制,不得不说翻译学习tomcat的初衷. 之前实习的时候学习j ...
- Linux终端多用户通信实用命令
一 命令 1.1 write 该命令将当前终端(源)输入的字符拷贝至目标用户的终端,从而发送消息给系统中某个用户.用法如下: #write <user> <msg> [Ctr ...
- C语言字节对齐问题详解
引言 考虑下面的结构体定义: typedef struct{ char c1; short s; char c2; int i; }T_FOO; 假设这个结构体的成员在内存中是紧凑排列的,且c1的起始 ...
- 二、K3 Cloud 开发插件《K3 Cloud 常用数据表整理》
一.数据库查询常用表 --查询数据表select * from ( ),t1.FKERNELXML.query('//TableName')) as 'Item',t1.FKERNELXML,t2.F ...