zoukankan      html  css  js  c++  java
  • hihocoder 1829

     题目链接:https://hihocoder.com/problemset/problem/1829

    时间限制: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

    题意:

    给出 $n$ 个环形的字符串,求出它们共同的最长子序列。

    不分顺时针逆时针。

    题解:

    由于数据量较小,考虑选择 $n$ 个串中长度最短的那个串 $s[idx]$,若存在LCS,必然是 $s[idx]$ 的一个子序列(子序列可以不连续),所以按状压的方式枚举 $s[idx]$ 的所有子序列,

    对于每个子序列,去其他剩下的 $n-1$ 个串里看是否都能找得到,若是都能找到,说明就是一个公共子序列。

    最后,在所有的公共子序列里找到最大的那个,就是LCS,输出时对其字典序排序一下即可。

    #include<bits/stdc++.h>
    using namespace std;
    
    int n;
    string s[12];
    
    bool Find(const string &big,const string &sma)
    {
        int len1=big.size(),len2=sma.size();
        for(int st=0;st<len1;st++)
        {
            int pos=0;
            for(int dx=0;dx<len1;dx++)
            {
                if(big[(st+dx)%len1]==sma[pos]) pos++;
            }
            if(pos>=len2) return 1;
            pos=0;
            for(int dx=0;dx<big.size();dx++)
            {
                if(big[(st-dx+len1)%len1]==sma[pos]) pos++;
            }
            if(pos>=len2) return 1;
        }
        return 0;
    }
    
    int main()
    {
        while(cin>>n)
        {
            int m=10,idx;
            for(int i=1;i<=n;i++)
            {
                cin>>s[i];
                if(m>s[i].size()) m=s[i].size(),idx=i;
            }
    
            int maxi=0;
            string ans;
            for(int sta=0;sta<=(1<<s[idx].size())-1;sta++)
            {
                string tmp;
                int tot=0;
                for(int i=0;i<s[idx].size();i++)
                {
                    if(sta&(1<<i))
                    {
                        tmp.insert(tmp.end(),1,s[idx][i]);
                        tot++;
                    }
                }
    
                bool ok=1;
                for(int i=1;i<=n;i++)
                {
                    if(!Find(s[i],tmp)) ok=0;
                }
    
                if(ok && maxi<tot)
                {
                    maxi=tot;
                    ans=tmp;
                }
            }
    
            if(maxi>0)
            {
                sort(ans.begin(),ans.end());
                cout<<ans<<endl;
            }
            else cout<<0<<endl;
        }
    }

    时间复杂度:设字符串长度为 $L$,则 $Oleft( {2^L nL^3 } ight)$。

  • 相关阅读:
    @PathVariable("cart_id") @RequestParam("ur") @RequestBody 比较
    将字符串数组解析为数组
    org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class ...
    nacos+openFeign #找不到服务 feign.FeignException$NotFound: status 404 reading
    java生成一个范围的数字
    栏中栏
    百度PaddlePaddle入门-4(5步法之八股文)
    百度PaddlePaddle入门-3(框架)
    百度PaddlePaddle入门-2(使用Numpy构建神经网络)
    MachineLearning入门-3(hello word)
  • 原文地址:https://www.cnblogs.com/dilthey/p/9689989.html
Copyright © 2011-2022 走看看