1. 程式人生 > >【18年北京網路賽】Tomb Raider【遞迴求所有子序列】

【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;
}