• Rikka with Nickname (简单题)


    Rikka with Nickname 

    链接:https://www.nowcoder.com/acm/contest/148/J
    来源:牛客网

    时间限制:C/C++ 2秒,其他语言4秒
    空间限制:C/C++ 262144K,其他语言524288K
    64bit IO Format: %lld


    题目描述

     

    Sometimes you may want to write a sentence into your nickname like "lubenwei niubi". But how to change it into a single word? Connect them one by one like "lubenweiniubi" looks stupid. To generate a better nickname, Rikka designs a non-trivial algorithm to merge a string sequence s1...sn into a single string. The algorithm starts with s=s1 and merges s2...sn into s one by one. The result of merging t into s is the shortest string r which satisfies s is a prefix of r and t is a subsequence of r.(If there are still multiple candidates, take the lexicographic order smallest one.) String s is a prefix of r if and only if |s| ≤ |r| and for all index i ∈ [1, |s|], si = ri. String s is a subsequence of r if and only if there is an index sequence which satisfies . For example, if we want to generate a nickname from "lubenwei niubi", we will merge "niubi" into "lubenwei", and the result is "lubenweiubi". Now, given a sentence s1...sn with n words, Rikka wants you to calculate the resulting nickname generated by this algorithm.
    输入描述:
    The first line contains a single number t(1 ≤ t ≤ 3), the number of testcases.For each testcase, the first line contains one single integer n(1 ≤ n ≤ 106).Then n lines follow, each line contains a lowercase string .
    输出描述:
    For each testcase, output a single line with a single string, the result nickname.

     

    示例

    输入

    2
    2
    lubenwei
    niubi
    3
    aa
    ab
    abb

    输出

    lubenweiubi
    aabb

    题解:水题,一开始做的时候用了string的find、erase就TLE了,把这些换成普通的搜索时间竟然大大减少了

    下面第一份是超时的代码:

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<int,int> PII;
    const ll mod=998244353;
    #define pb push_back
    #define mp make_pair
    #define all(x) (x).begin(),(x).end()
    #define fi first
    #define se second
    //head
    #define MAX 100005
    int T;
    int n;
    string s,ans,t,ss;
    int main()
    {
          cin.tie(0);
        cout.tie(0);
        ios::sync_with_stdio(0);
        cin>>T;
        while(T--)
        {
            cin>>n;
            cin>>s;
            ans=s;
            for(int d=0;d<n-1;d++)
            {
                t=ans;
                cin>>s;
                int k=0;
                for(int i=0;i<s.size();i++)
                {
                    int tt=t.find(s[i]);
                    if(tt>=0)
                    {
                        k++;
                        tt++;
                        t.erase(0,tt);
                    }
                    else break;
                }
                for(int j=k;j<s.size();j++)
                    ans+=s[j];
            }
            cout<<ans<<endl;
        }
        return 0;
    }

    这是AC代码:

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<int,int> PII;
    const ll mod=998244353;
    #define pb push_back
    #define mp make_pair
    #define all(x) (x).begin(),(x).end()
    #define fi first
    #define se second
    //head
    #define MAX 100005
    int T;
    int n;
    string s,ans,t,ss;
    int main()
    {
          cin.tie(0);
        cout.tie(0);
        ios::sync_with_stdio(0);
        cin>>T;
        while(T--)
        {
            cin>>n;
            cin>>s;
            ans=s;
            for(int d=0;d<n-1;d++)
            {
                t=ans;
                cin>>s;
                int k=0;
                int f=0;
              while(k<t.size())
              {
                  if(s[f]==t[k])f++,k++;
                  else k++;
                  if(f==s.size())
                    break;
              }
                for(int j=f;j<s.size();j++)
                    ans+=s[j];
            }
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    [SoapUI] 在执行某个TestSuite之前先执行login或者其他什么前置步骤
    [Groovy] How to check if element in groovy array/hash/collection/list?
    [Groovy] List和Map用法搜集
    [Groovy] 实战 Groovy, for each 剖析
    Groovy基本语法
    [BAT] xcopy拷贝远程服务器共享文件到本地
    [BAT] Access Denied in XCopy
    [BAT] 执行xcopy命令后出现Invalid num of parameters错误的解决办法
    [Automation] 自动化测试度量标准
    jsp页面直接编写csss
  • 原文地址:https://www.cnblogs.com/zhgyki/p/9502746.html
Copyright © 2020-2023  润新知