ACM-ICPC2018北京网络赛 Tomb Raider(暴力)

题目2 : Tomb Raider

时间限制: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
#include<bits/stdc++.h>
#define MAX 15
using namespace std;
typedef long long ll;

string s[MAX];
int n;
string ss,ans;

int find(string x){
    int lenx=x.length();
    for(int i=1;i<n;i++){
        int len=s[i].length();
        int f=0;
        for(int k=0;k<len;k++){
            if(s[i][k]==x[0]){
                int c=0;int l=-1;
                for(int j=k;j<len;j++){
                    if(s[i][j]==x[c]){
                        if(l==-1) l=j;
                        c++;
                        if(c>=lenx&&(j-l+1)<=(len/2)){
                            f=1;
                            break;
                        }
                    }
                }
                if(f==1) break;
            }
        }
        if(f==0) return 0;
    }
    return 1;
}
void dfs(int en,string x,int st){
    if(st>=en){
        if(find(x)==1){
            if(x.length()>ans.length()){
                ans=x;
            }
            else if(x.length()==ans.length()){
                if(x<ans){
                    ans=x;
                }
            }
        }
        return;
    }
    for(int i=0;i<=1;i++){
        if(i==1) dfs(en,x+ss[st],st+1);
        else dfs(en,x,st+1);
    }
}
int main()
{
    int t,i,j;
    while(~scanf("%d",&n)){
        for(i=0;i<n;i++){
            cin>>s[i];
            s[i]+=s[i];
        }
        ans="{";
        int len=s[0].length();
        for(i=0;i<len;i++){
            int minn=min(len-i,len/2);
            for(j=1;j<=minn;j++){
                ss=s[0].substr(i,j);
                dfs(j,"",0);
            }
        }
        if(ans=="{") printf("0\n");
        else cout<<ans<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/yzm10/p/9690877.html

时间: 2024-10-27 04:55:52

ACM-ICPC2018北京网络赛 Tomb Raider(暴力)的相关文章

acm 2015北京网络赛 F Couple Trees 主席树+树链剖分

提交 题意:给了两棵树,他们的跟都是1,然后询问,u,v 表 示在第一棵树上在u点往根节点走 , 第二棵树在v点往根节点走,然后求他们能到达的最早的那个共同的点 解: 我们将第一棵树进行书链剖,然后第二棵树采用主席树,他的信息来自他的父亲节点,每个点存他在第一棵树 树链剖分后的位置,这样我们每次查询uv的时候我们只要 我们选取u和top[u]这段区间在主席树v这颗树上找,在这个区间能取到的最大值,一旦存在,这个最大值就我们要的,这个点保存着他到根节点这条路上所有点在第一棵树剖分后的位置 #inc

2015北京网络赛A题The Cats&#39; Feeding Spots

题意:给你一百个点,找个以这些点为中心的最小的圆,使得这个圆恰好包含了n个点,而且这个圆的边界上并没有点 解题思路:暴力枚举每个点,求出每个点到其他点的距离,取第n大的点,判断一下. 1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 #include<iostream> 5 #include<memory.h> 6 using namespace std; 7 const i

2015北京网络赛 Couple Trees 倍增算法

2015北京网络赛 Couple Trees 题意:两棵树,求不同树上两个节点的最近公共祖先 思路:比赛时看过的队伍不是很多,没有仔细想.今天补题才发现有个 倍增算法,自己竟然不知道.  解法来自 qscqesze ,这个其实之前如果了解过倍增的话还是不是很难,不过这题的数据也不是很给力,极限数据理论上是过不了的.  其他解法有树链剖分?并不是很清楚.就这样水过了吧... 1 #include <iostream> 2 #include <cstdio> 3 #include &l

hihocoder1236(北京网络赛J):scores 分块+bitset

北京网络赛的题- -.当时没思路,听大神们说是分块+bitset,想了一下发现确实可做,就试了一下,T了好多次终于过了 题意: 初始有n个人,每个人有五种能力值,现在有q个查询,每次查询给五个数代表查询的五种能力值,输出有多少个人每种能力值都比查询的小 n和q都是50000,每种能力值最大也为50000 思路: 对于某一个大小的能力值,有哪些人的此项能力值比他小可以用一个50000的bitset表示.这样我们在查询的时候就可以拿到5个对应的bitset,对其进行and就可以得出最终的人数 这样每

2015北京网络赛 D-The Celebration of Rabbits 动归+FWT

2015北京网络赛 D-The Celebration of Rabbits 题意: 给定四个正整数n, m, L, R (1≤n,m,L,R≤1000). 设a为一个长度为2n+1的序列. 设f(x)为满足x≤ai≤m+x且ai的异或和为0 的序列a的个数. 求 ∑Rx=Lf(x)mod1000000007 思路:因为对于每一个第一次分配后的a序列对应唯一的x,所以我们就枚举x然后在求序列的个数.

HDU-4041-Eliminate Witches! (11年北京网络赛!!)

Eliminate Witches! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1124    Accepted Submission(s): 426 Problem Description Kaname Madoka is a Magical Girl(Mahou Shoujo/Puella Magi). The duty of

Tomb Raider(暴力模拟)

Tomb Raider https://hihocoder.com/problemset/problem/1829?sid=1394836 时间限制: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

2015北京网络赛 F Couple Trees 暴力倍增

Couple Trees Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/problemset/problem/1232 Description "Couple Trees" are two trees, a husband tree and a wife tree. They are named because they look like a couple leaning on each other.

hihoCoder #1388 : Periodic Signal ( 2016 acm 北京网络赛 F题)

时间限制:5000ms 单点时限:5000ms 内存限制:256MB 描述 Profess X is an expert in signal processing. He has a device which can send a particular 1 second signal repeatedly. The signal is A0 ... An-1 under n Hz sampling. One day, the device fell on the ground accidenta