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个串的这些子串的最长公共子序列(每个串按顺 ...
随机推荐
- switch case 注意事项
switch case常见的注意事项: 1.case后面常量值的顺序可以任意,一般按顺序编写 2.default顺序也可以编写在switch中的任意位置 当所有case都不满足时则执行default ...
- 【Tensorflow】 Object_detection之模型训练日志结果解析
日志展示 指标说明: AP值表示正确识别物体的个数占总识别出的物体个数的百分数 AR值表示正确识别物体的个数占测试集中物体的总个数的百分数 IoU值即生成的框/掩膜与数据集中的标准的面积之交处于面积之 ...
- docker 摘要(入门版)
Docker 安装 macOS或者windows 下载boot2docker工具 CentOS yum install docker-io -y systemctl start docker dock ...
- nyoj 1023——还是回文——————【区间dp】
还是回文 时间限制:2000 ms | 内存限制:65535 KB 难度:3 描述 判断回文串很简单,把字符串变成回文串也不难.现在我们增加点难度,给出一串字符(全部是小写字母),添加或删除一 ...
- 结合manage.py,在flask项目中使用websocket模块--- flask-socketio
前言: - 为什么我要使用 flask-socketio模块,而不是flask-sockets? - 因为flask-socketio与前端流行的websocket库socke ...
- c#学习基础(2)存储、值类型和引用类型、变量
程序运行时,它的数据必须存储在内存中,数据项需要多大的内存.存储在什么地方以及如何存储都依赖该数据项的类型 运行中的程序使用两个区域来存储数据:栈和堆 栈是一个内存数组,是一个LIFO(last in ...
- (三)css之浮动&定位
众所周知,一个页面可能包含多个div,如何对这些div进行排列,以便具有较好的显示效果呢? css提供了浮动和定位两个属性进行div的排列,下面主要针对浮动和定位进行详细地阐述. (一)何为浮动? 浮 ...
- sass(混合mixin的调用、@content)
sass中使用@mixin声明混合,可以传递参数,参数名以$符号开始,多个参数以逗号分开,也可以给参数设置默认值.声明的@mixin通过@include来调用 1.无参数mixin scss.styl ...
- css background 背景知识详解
background 背景属性 我们知道元素有前景色color,与之对应的还有背景色,通过background我们可以为元素添加实色(background-color)和任意多个背景图片(backgr ...
- .NET开源工作流RoadFlow-表单设计-标签(label)
LABEL标签即在表单中添加一个文本标签: 字号:标签显示文字的大小. 颜色:标签显示文字的颜色. 样式:以粗体和斜体显示.