ACM-ICPC 2018 北京赛区网络预赛 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

数据量非常小,根本用不到什么算法,暴搜出每一个字符串的所有子序列,每一个字符串的所有子序列加入一个set里,最后求n个set的交集中最长的那个字符串字典序最小的即可

#include <bits/stdc++.h>
using namespace std;
set<string> st[11];
string t,ans;
int len;
void dfs(int i,int cur)
{
    if(cur == len) {
        if(ans == "") return;
        st[i].insert(ans);
        int _len = ans.length();
        for(int j = 1; j < _len; j++) {
            st[i].insert(ans.substr(j,len - j) + ans.substr(0,j));
        }
        return;
    }
    for(int j = 0; j <= 1; j++) {
        if(j == 0) ans.push_back(t[cur]);
        dfs(i,cur + 1);
        if(j == 0) ans.erase(ans.end() - 1);
    }
}
int main(void)
{
    int n;
    while(scanf("%d",&n) != EOF) {
        for(int i = 0; i < n; i++) {
            st[i].clear();
        }
        for(int i = 0; i < n; i++) {
            cin >> t;
            len = t.length();
            dfs(i,0);
        }
        set<string>::iterator it;
        string ans = "0";
        int flag;
        for(it = st[0].begin(); it != st[0].end(); it++) {
            t = *it;
            flag = 0;
            for(int i = 1; i < n; i++) {
                if(!st[i].count(t)) {
                    flag = 1;
                    break;
                }
            }
            if(flag == 0) {
                if(ans == "0") {
                    ans = t;
                }
                else if(t.length() > ans.length()) {
                    ans = t;
                }
                else if(t.length() == ans.length() && ans > t) {
                    ans = t;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值