ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 B Tomb Raider 【二进制枚举】
任意门:http://hihocoder.com/problemset/problem/1829
Tomb Raider
描述
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 个字符环的公共子序列(只考虑顺时针方向)
解题思路:
一开始是想两两配对出最长子序列再与下一个配对,用一个队列来实现字符环子序列的操作。
不过这显然是错误的做法,因为两两配对的最长子序列可能存在多种情况。
正确的打开方式:字串全部暴力一遍!!!
具体的暴力方法是二进制枚举,只暴力第一个字符环,把暴力出来的子串与剩下的字符环配对,判断是否每个字符环都存在这个子串, 如果存在保留最长的,如果相同长度保留字典序最小的(直接strcmp()就可以判断哪个子串字典序更小了)。
而处理环的方法是最常见的 double 字符串,两个相同字符串首尾相接就成环了(废话。。。)。
AC code:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std; int len[], len_ans;
char s[+][+], ans[+]; bool judge(char* ss, int k)
{
for(int f = ; f < len[k]; f++){
int pss = ;
for(int i = ; i < len[k]; i++){ //逐位寻找
if(s[k][f+i] == ss[pss]){
pss++;
if(ss[pss] == '\0') return true; //所有都能找到,满足条件
}
}
}
return false;
} void check(int n)
{
char tss[+], css[+];
int u = <<len[]; //二进制每一位代表一个字符
for(int f = ; f < len[]; f++){ //枚举不同起点的子串
strncpy(tss, s[]+f, len[]); // f 为起点
tss[len[]] = '\0';
for(int k = ; k < u; k++){ //二进制枚举当前起点的子串的子串
int p = ;
for(int i = ; i < len[]; i++){
if(!(k&(<<i))) continue;
css[p] = tss[i];
p++;
}
css[p] = '\0';
bool ok = true; //判断其他字符环是否也存在这个子串
for(int i = ; i < n; i++){
if(!judge(css, i)){ //有一个不满足就匹配失败
ok = false;break;
}
}
if(ok){ //如果当前子串满足条件
if(len_ans == -){ //当前子串为找到的第一个满足所有条件的子串
strcpy(ans, css);
len_ans = strlen(ans);
}
else{
int lencss = strlen(css);
if(lencss > len_ans){ //比较子串长度,长的优先
strcpy(ans, css);
len_ans = lencss;
}
else if(len_ans == lencss){ //长度相同,字典序小的优先
if(strcmp(ans, css) > ){
strcpy(ans, css);
}
}
}
}
}
}
} int main()
{
int N;
char tmp[+];
while(~scanf("%d", &N)){
for(int i = ; i < N; i++){
scanf("%s", &s[i]);
len[i] = strlen(s[i]);
strcpy(tmp, s[i]); //字符串double 处理环
strcat(s[i], tmp);
}
len_ans = -; //答案字符长度
check(N); //暴力
if(len_ans == -) puts(""); //没有满足条件的字串
else printf("%s\n", ans);
}
return ;
}
ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 B Tomb Raider 【二进制枚举】的更多相关文章
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛D-80 Days--------树状数组
题意就是说1-N个城市为一个环,最开始你手里有C块钱,问从1->N这些城市中,选择任意一个,然后按照顺序绕环一圈,进入每个城市会有a[i]元钱,出来每个城市会有b[i]个城市,问是否能保证经过每 ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛-B:Tomb Raider(二进制枚举)
时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Lara Croft, the fiercely independent daughter of a missing adv ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 80 Days(尺取)题解
题意:n个城市,初始能量c,进入i城市获得a[i]能量,可能负数,去i+1个城市失去b[i]能量,问你能不能完整走一圈. 思路:也就是走的路上能量不能小于0,尺取维护l,r指针,l代表出发点,r代表当 ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛
题意:到一个城市得钱,离开要花钱.开始时有现金.城市是环形的,问从哪个开始,能在途中任意时刻金钱>=0; 一个开始指针i,一个结尾指针j.指示一个区间.如果符合条件++j,并将收益加入sum中( ...
- hihoCoder #1831 : 80 Days-RMQ (ACM/ICPC 2018亚洲区预选赛北京赛站网络赛)
水道题目,比赛时线段树写挫了,忘了RMQ这个东西了(捞) #1831 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an int ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A、Saving Tang Monk II 【状态搜索】
任意门:http://hihocoder.com/problemset/problem/1828 Saving Tang Monk II 时间限制:1000ms 单点时限:1000ms 内存限制:25 ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A.Saving Tang Monk II(优先队列广搜)
#include<bits/stdc++.h> using namespace std; ; ; char G[maxN][maxN]; ]; int n, m, sx, sy, ex, ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 D 80 Days (线段树查询最小值)
题目4 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an interesting game based on Jules Ve ...
- hihoCoder-1829 2018亚洲区预选赛北京赛站网络赛 B.Tomb Raider 暴力 字符串
题面 题意:给你n个串,每个串都可以选择它的一个长度为n的环形子串(比如abcdf的就有abcdf,bcdfa,cdfab,dfabc,fabcd),求这个n个串的这些子串的最长公共子序列(每个串按顺 ...
随机推荐
- Linux——【rpm、yun、源码包】安装
RPM包或者安装源码包 在windows下安装一个软件很轻松,只要双击.exe的文件,安装提示连续“下一步”即可,然而linux系统下安装一个软件似乎并不那么轻松,因为我们不是在图形界面下.所以我们要 ...
- lnmp 一键搭建脚本
转载注明出处!!!!!!!!! 不足之处望多多指教. 不明之处站内私. #!/bin/bash #################################################### ...
- 【3dsMax安装失败,如何卸载、安装3dMax 2011?】
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- centos6.5下搭建oracle 11g
1.安装依赖 sudo yum install binutils compat-libstdc++-33 compat-libstdc++-33.i686 elfutils-libelf elfuti ...
- nyoj 600——花儿朵朵——【离散化、线段树插线问点】
花儿朵朵 时间限制:1000 ms | 内存限制:65535 KB 难度:5 描述 春天到了,花儿朵朵盛开,hrdv是一座大花园的主人,在他的花园里种着许多种鲜花,每当这个时候,就会有一大群游 ...
- 吴恩达《Machine Learning Yearning》总结(31-40章)
31.解读学习曲线:其他情况 下图反映了高方差,通过增加数据集可以改善. 下图反映了高偏差和高方差,需要找到一种方法来同时减少方差和偏差. 32.绘制学习曲线 情况:当数据集非常小时,比如只有100个 ...
- Android界面编程--使用活动条(ActionBar)
ActionBar的使用 1.启动ActionBar(默认状态下是启动的) 1.1 在Android配置文件(AndroidManifest.xml)中设置应用的主题为 ***.NoActionBar ...
- [转]scp命令学习
原博客地址http://www.cnblogs.com/peida/archive/2013/03/15/2960802.html scp是secure copy的简写,用于在Linux下进行远程拷贝 ...
- mockito 初识
转载:http://blog.csdn.net/zhoudaxia/article/details/33056093 在平时的开发工作中,经常会碰到开发进度不一致,导致你要调用的接口还没好,此时又需要 ...
- 数据访问层 (DAO)
数据持久化 持久化:将程序中的数据在瞬间状态下和持久状态间转换的机制(JDBC) 主要持久化操作:保存.删除.读取.和查找. 采用面向接口编程,可以降低代码间的耦合性,提高代码的可扩展性和可维护性. ...