【18年北京网络赛】Tomb Raider【递归求所有子序列】

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个字符串的LCS(Longest  Common Sequence)-最长公共字序列。

然后题目还给了一个要求,可以在任意位置开始。

这句话可以理解为,字符串是循环的,如 aabbcc-其中有一个字序列可以为:ccaabb.

意思就是循环字符串的最长公共字序列。

【题解】:

因为给定的字符串长度很小,最多才8.然后才十个字符串。

我们可以找出,第一个 字符串 的   所有的字序列 用回溯全部搜出来,先排序,然后一一匹配,

用一个n^2的复杂度匹配所有 其余 的字符串(预处理:首尾拼接-达到循环)。

 

贴上代码(队长ppr做法):

#include<bits/stdc++.h>
using namespace std;
string S[3000];
string str[20];
int cnt=0;
void printAllSub(string str, int i, string res)
{
    if (i == str.length())
    {
        if(res=="")
            return ;
        //cout << res << endl;
        S[cnt++]=res;
        return;
    }
    printAllSub(str, i + 1, res);         
    printAllSub(str, i + 1, res + str[i]);
}
bool cmp(string a,string b){
    if(a.size()==b.size()){
        return a<b;
    }
    else{
        return a.size()>b.size();
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n)){
        cnt=0;
        /*if(n==0){
            break;
        }*/
        int len[20]={0};
        for(int i=0;i<n;i++){
            cin>>str[i];
            if(i!=0){
                len[i]=str[i].size();
                str[i]+=str[i];
            }
        }
        int t=str[0].size();
        string temp=str[0];
        str[0]+=str[0];
        for (int i=0;i<t;i++){
            string temp1=str[0].substr(i,t);
            //cout<<temp1<<endl;
            printAllSub(temp1,0,"");
        }
        sort(S,S+cnt,cmp);
        /*for(int i=0;i<cnt;i++){
            cout<<S[i]<<endl;
        }
        for(int i=1;i<n;i++){
            cout<<str[i]<<endl;
        }*/
        int num=0,tot=0;
        string ans="0";
        for(int i=0;i<cnt;i++){
            string tmp=S[i];
            tot=0;
            for(int j=1;j<n;j++){
                for(int k=0;k<len[j];k++){
                    num=0;
                    for(int L=k;L<len[j]+k;L++){
                        if(str[j][L]==tmp[num]){
                            num++;
                        }
                        if(num==tmp.size()){
                            tot++;
                            break;
                        }
                    }
                    if(num==tmp.size()){
                        break;
                    }
                }
            }
            if(tot==n-1){
                ans=tmp;
                break;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

贴上代码(自己做法):

#include<bits/stdc++.h>
using namespace std;
string S[3000];
string str[20];
map<string, int >mp[20];
int cnt=0;
void printAllSub(string str, int i, string res,int No)
{
    if (i == str.length()){
        if(res=="")
            return ;
        //cout << res << endl;
        if(No==0)
            S[cnt++]=res;
        mp[No][res]=1;
        return;
    }
    printAllSub(str, i + 1, res,No);
    printAllSub(str, i + 1, res + str[i],No);
}
bool cmp(string a,string b){
    if(a.size()==b.size()){
        return a<b;
    }
    else{
        return a.size()>b.size();
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n)){
        for(int i=0;i<n;i++){
            mp[i].clear();
        }
        cnt=0;
        int len[20]={0};
        for(int i=0;i<n;i++){
            cin>>str[i];
            len[i]=str[i].size();
            str[i]+=str[i];
        }
        for(int i=0;i<n;i++){
            for (int j=0;j<len[i];j++){
                string temp=str[i].substr(j,len[i]);
                //cout<<temp1<<endl;
                printAllSub(temp,0,"",i);
            }
        }
        sort(S,S+cnt,cmp);
        /*for(int i=0;i<cnt;i++){
            cout<<S[i]<<endl;
        }
        for(int i=1;i<n;i++){
            cout<<str[i]<<endl;
        }*/
        int num=0,tot=0;
        string ans="0";
        for(int i=0;i<cnt;i++){
            int j;
            for(j=1;j<n;j++){
                if(mp[j][S[i]]==0){
                    break;
                }
            }
            if(j==n){
                ans=S[i];break;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值