• ACM/ICPC 2018亚洲区预选赛北京赛站网络赛-B: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

    题意

    有n个字符串,每个字符串首尾相连,求这n个字符串的最长公共子序列并输出

    思路

    将每个首尾相连字符串从0~len-1的每个位置形成的字符串都进行二进制枚举,将所有情况的子序列都用map标记并统计出现的次数,然后枚举map里的元素,将出现n次的子序列存进vector,对vector里的元素进行排序。排序的规则:如果两个子序列长度不同,返回长度较长的子序列,如果相同,返回字典序小的子序列

    AC代码

    #include<bits/stdc++.h>
    using namespace std;
    #define line cout<<"------------"<<endl
    const int N = 33;
    
    map<string, int>mp,vis;
    map<int, string>ms;
    vector<string>ve;
    int n;
    string s,u, str;
    bool cmp(string a, string b)
    {
        if(a.length() != b.length()) 
            return a.length() > b.length();
        else 
            return a < b;
    }
    // 求出从pos位置开始的字符串
    void change(int pos)
    {
        str.clear();
        string temp = s.substr(pos);
        str += temp;
        temp = s.substr(0,pos);
        str += temp;
    }
    int main()
    {
        while(scanf("%d",&n)!=EOF)
        {
            mp.clear();
            ve.clear();
            int nn=n;
            while(nn--)
            {
                vis.clear();
                cin>>s;
                int len=s.length();
                for(int k=0; k<len; k++)
                {
                    change(k);
                    // 对字符串进行二进制枚举,每个子序列出现的次数用map标记
                    for(int i=1;i < (1<<len); i++)
                    {
                        for(int j=0; j<len; j++)
                        {
                            if(i >> j & 1) 
                               u += str[j];
                        }
                        if(vis[u]==0)
                        {
                           mp[u]++;
                           vis[u]=1;
                        }
                        u.clear();
                    }
                }
            }
            int flag=0;
            for(auto i: mp) 
            {
                // 找出所有出现次数为n的子序列,存进vector
                if(i.second == n)
                {
                    ve.push_back(i.first);
                    flag = 1;
                }
            }
            // 对vector进行排序
            sort(ve.begin(),ve.end(),cmp);
            if(flag==0)
                cout<<0<<endl;
            else cout << ve[0] << endl;
        }
        return 0;
    }
  • 相关阅读:
    【根据条件添加属性】vue页面标签根据条件添加属性
    serialVersionUID
    onsubmit="return navTabSearch(this);"
    MyEclipse改变项目的编码方式
    Tomcat端口被占用
    可拖动图层
    顶部可以折叠的菜单工具栏
    转---- javascript prototype介绍的文章
    网页右侧弹出有缓冲效果的工具栏
    根据时间改变背景
  • 原文地址:https://www.cnblogs.com/Friends-A/p/10324354.html
Copyright © 2020-2023  润新知