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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ACM-ICPC(国际大学生程序设计竞)是一项面向大学生的计算机编程竞,涉及算法和数据结构等领域。在比中,选手需要解决一系列编程问题,使用合适的算法和数据结构来实现正确和高效的解决方案。 对于整理ACM-ICPC模板,以下是一些建议: 1. 了解比要求:首先,你需要了解ACM-ICPC的具体要求和规则。这包括了解比所涉及的算法和数据结构,以及题目的类型和难度等。 2. 收集资料:收集与ACM-ICPC相关的资料,包括经典算法和数据结构的实现代码、常见问题的解题思路等。可以参考教材、博客、论文等资源。 3. 整理模板:将收集到的资料整理成模板。可以按照算法和数据结构的分类进行整理,例如排序算法、图算法、字符串算法等。对每个模板,添加必要的注释和示例代码,以便理解和使用。 4. 测试代码:对每个模板编写测试代码,确保它们的正确性和可靠性。可以使用已知的测试用例或自行设计测试用例。 5. 更新与扩充:定期更新和扩充模板,以适应ACM-ICPC中新出现的算法和数据结构。同时,根据自己的经验和理解,对模板进行优化和改进。 6. 练习和复习:在比之前,利用整理好的模板进行练习和复习。尝试解决一些经典问题,使用模板中的算法和数据结构进行实现,并进行优化。 希望这些建议对你整理ACM-ICPC模板有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值