1. 程式人生 > >#1829 : Tomb Raider(雜湊)

#1829 : 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

當然,也可以直接map去重

!!! unordered_map: 一定要開 C++11,要不然,沒法用

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define rep(i,a,b) for(int i=a;i<b;++i)
#define per(i,a,b) for(int i=b-1;i>=a;--i)

const int N=15;

char s[11][20];
int len[11];

//hash
/*
初始化:
first:我們要看有多少個不同的取值
nex: has_link:  我們要看所有的取值
算是理解又深了一步吧
*/
const int M=6600010;
const ULL P=100007;
int fir[P],nxt[M],cnt=0;
ULL has_link[M];
int HAS(ULL x)
{
    ULL t=x%P;
    for(int i=fir[t]; i!=-1; i=nxt[i]){

	//	printf("i:%d \n",i);
        if(has_link[i]==x)return 1;
    }
    nxt[cnt]=fir[t];
    fir[t]=cnt;
    has_link[cnt]=x;//在雜湊串的尾部
    cnt++;
    return 0;
}
//



unordered_map<ULL,int> exi;


int main()
{
    int n;
    while(scanf("%d",&n)==1)
    {

        exi.clear();

        rep(i,0,n)scanf("%s",s[i]);
        rep(i,0,n)len[i]=strlen(s[i]);

        for(int j=0; j<n; ++j) for(int i=0; i<len[j]; i++)s[j][i+len[j]]=s[j][i];

        for(int k=0; k<n; ++k)
        {
          //  printf("k:%d\n",k);
            rep(kk,0,cnt)has_link[kk]=0;
            rep(kk,0,P)fir[kk]=-1;
			cnt=0;
		
            int sta=1<<(len[k]*2);
            for(int j=1; j<sta; ++j)
            {
                int num=0,mi=2*len[k],mx=0;
                ULL res=0;
                for(int i=0; i<2*len[k]; ++i)
                {
                    if((1<<i)&j)
                    {
                        mi=min(mi,i),mx=max(mx,i);
                        res=res*P+(ULL)(s[k][i]-'a'+1);
                    }
                }
                if(mx-mi+1>len[k])continue;

               // printf("j:%d res:%llu\n",j,res);
                if(HAS(res))continue;
                exi[res]++;
            }
        }

      //  printf("*****\n");

        string ans="";int mmm=0;
        for(int k=0; k<1; ++k)
        {
            rep(kk,0,cnt)has_link[kk]=0;
            rep(kk,0,P)fir[kk]=-1;
			cnt=0;

            int sta=1<<(len[k]*2);
            for(int j=1; j<sta; ++j)
            {
                int num=0,mi=2*len[k],mx=0;
                ULL res=0;string tmp="";
                for(int i=0; i<2*len[k]; ++i)
                {
                    if((1<<i)&j)
                    {
                        num++;
                        tmp+=s[k][i];
                        mi=min(mi,i),mx=max(mx,i);
                        res=res*P+(ULL)(s[k][i]-'a'+1);
                    }
                }
                if(mx-mi+1>len[k])continue;
                if(HAS(res)||num<mmm||exi[res]<n)continue;

               //cout<<"tmp:"<<tmp<<endl;
                //exi[res]++;
                if(num>mmm){
                    mmm=num;
                    ans=tmp;
                }
                else if(num==mmm){
                    ans=min(ans,tmp);
                }
            }
        }
        if(mmm!=0) cout<<ans<<endl;
        else printf("0\n");
    }
    return 0;
}