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个串的这些子串的最长公共子序列(每个串按顺 ...
随机推荐
- web安全漏洞种类
(参考知道创宇) SQL注入: SQL注入(SQL Injection),是一个常见的发生于应用程序和数据库之间的web安全漏洞,由于在开发过程中的设计不当导致程序中忽略了检查,没有有效的过滤用户的输 ...
- zabbix 千台服务器自动添加实战
一,模式 zabbix 的自动添加 主机有梁祝方式: 自动发现-----被动模式 由服务端主动发起,Zabbix Server开启发现进程,定时扫描局域网中IP服务器.设备, 自动注册----主动 ...
- 日志收集之nxlog
一,软件介绍 nxlog 是用 C 语言写的一个开源日志收集处理软件,它是一个模块化.多线程.高性能的日志管理解决方案,支持多平台.可以处理来自许多不同来源的大量事件日志.支持的日志处理类型包括重写, ...
- (转)linux expr命令参数及用法详解
linux expr命令参数及用法详解 原文:http://blog.csdn.net/tianmohust/article/details/7628694 expr用法 expr命令一般用于整数值, ...
- 使用params
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- VS2010中VC++目录和C/C++之间的区别。VC++ Directories和C/C++的区别。
首先,这是个历史遗留问题,说起来比较复杂.其次,这个问题在微软的MSDN博客上已经专门被说起过了,英文好的请直接移步到原文:<VC++ Directories>.另外,stack over ...
- java线程中的 sleep() wait() yield()
sleep()方法是让线程休眠 可以指定时间 其实就是让线程进入阻塞状态 指定的时间过后 进入就绪状态 不释锁 相当于抱着锁睡觉 wait() 让线程进入等待状态 被唤醒后才会继续执行 ...
- js 常用事件句柄总结
HTML 4.0 的新特性之一是有能力使 HTML 事件触发浏览器中的动作(action),比如当用户点击某个 HTML 元素时启动一段 JavaScript.下面是一个属性列表,这些属性可插入 HT ...
- POST 还是 GET?
POST 还是 GET? 浏览器使用 method 属性设置的方法将表单中的数据传送给服务器进行处理.共有两种方法:POST 方法和 GET 方法. 如果采用 POST 方法,浏览器将会按照下面两步来 ...
- java基础--常用函数总结
java基础--常用函数总结 2019-3-16-23:28:01-----云林原创 1.split()字符串分割函数 将一个字符串分割为子字符串,然后将结果作为字符串数组返回. 2.Math.flo ...