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=;
#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();
cout.tie();
ios::sync_with_stdio();
cin>>T;
while(T--)
{
cin>>n;
cin>>s;
ans=s;
for(int d=;d<n-;d++)
{
t=ans;
cin>>s;
int k=;
for(int i=;i<s.size();i++)
{
int tt=t.find(s[i]);
if(tt>=)
{
k++;
tt++;
t.erase(,tt);
}
else break;
}
for(int j=k;j<s.size();j++)
ans+=s[j];
}
cout<<ans<<endl;
}
return ;
}

这是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=;
#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();
cout.tie();
ios::sync_with_stdio();
cin>>T;
while(T--)
{
cin>>n;
cin>>s;
ans=s;
for(int d=;d<n-;d++)
{
t=ans;
cin>>s;
int k=;
int f=;
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 ;
}

Rikka with Nickname (简单题)的更多相关文章

  1. BZOJ 2683: 简单题

    2683: 简单题 Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 913  Solved: 379[Submit][Status][Discuss] ...

  2. 【BZOJ-1176&2683】Mokia&简单题 CDQ分治

    1176: [Balkan2007]Mokia Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 1854  Solved: 821[Submit][St ...

  3. Bzoj4066 简单题

    Time Limit: 50 Sec  Memory Limit: 20 MBSubmit: 2185  Solved: 581 Description 你有一个N*N的棋盘,每个格子内有一个整数,初 ...

  4. Bzoj2683 简单题

    Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 1071  Solved: 428 Description 你有一个N*N的棋盘,每个格子内有一个整数, ...

  5. 这样leetcode简单题都更完了

    这样leetcode简单题都更完了,作为水题王的我开始要更新leetcode中等题和难题了,有些挖了很久的坑也将在在这个阶段一一揭晓,接下来的算法性更强,我就要开始分专题更新题目,而不是再以我的A题顺 ...

  6. [BZOJ2683][BZOJ4066]简单题

    [BZOJ2683][BZOJ4066]简单题 试题描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x ...

  7. HDU 1753 大明A+B(字符串模拟,简单题)

    简单题,但要考虑一些细节: 前导0不要,后导0不要,小数长度不一样时,有进位时,逆置处理输出 然后处理起来就比较麻烦了. 题目链接 我的代码纯模拟,把小数点前后分开来处理,写的很繁杂,纯当纪念——可怜 ...

  8. 团体程序设计天梯赛-练习集L1-014. 简单题

    L1-014. 简单题 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 这次真的没骗你 —— 这道超级简单的题目没有任何输入. ...

  9. bzoj 4066: 简单题 kd-tree

    4066: 简单题 Time Limit: 50 Sec  Memory Limit: 20 MBSubmit: 234  Solved: 82[Submit][Status][Discuss] De ...

随机推荐

  1. 工作流引擎 springmvc SSM 流程审批 Java Activiti 后台框架源码

    工作流模块  1.模型管理    :web在线流程设计器.预览流程xml.导出xml.部署流程 2.流程管理    :导入导出流程资源文件.查看流程图.根据流程实例反射出流程模型.激活挂起 3.运行中 ...

  2. RabbitMQ ——整体架构

    一 .概述 从整体上讲Rabbitmq就是一个生产者消费者的模型. 我们将中间的整个broker就当做是一个消息中间件的实体就可以了. 单从这个方面上讲,生产者发送消息到broker上面,然后消费者从 ...

  3. 将ibatis迁移到mybatis3的过程记录

    将ibatis迁移到mybatis3的过程记录 场景:有些以前的老项目是使用ibatis开发的,现在有转换成mybatis3的需求. 环境准备:需要安装Ant,以下是本人的安装版本,具体怎么安装不再赘 ...

  4. mysql02---客户端与服务器模型

    目录 一.客户端与服务器模型 连接MySQL方式 总结: 二.MySQL服务器构成 三.MySQL的结构 一.客户端与服务器模型 1.mysql是一个典型的C/S服务结构 1.1 mysql自带的客户 ...

  5. 【leetcode】44. Wildcard Matching

    题目如下: 解题思路:本题和[leetcode]97. Interleaving String非常相似,同样可以采用动态规划的方法.记dp[i][j] = 1或者0 表示pattern[0:i]是否匹 ...

  6. Raspbian 在虚拟机上运行,运行Flask,供宿主机访问

    Raspbian 在虚拟机上运行,启动Flask,供宿主机访问 参考ref 1, 在virtualbox上跑起来Raspbian OS 参考ref 2, 在Raspbian上安装并运行Falsk, 注 ...

  7. eval函数让我忧伤

    今天首次接触这个eval函数,让我忧伤了一把.我把当成字符串拼接,结果错得天远地远.大体情况是下面这句代码,一个劲的给我报NameError: name 'qinfeng' is not define ...

  8. 2,Spring MVC 学习总结(二)- 方法(Action)参数映射

    一,Controller层方法(Action)参数映射 1,自动参数映射 1.1,基本数据类型参数映射 方法的参数可以是任意基本数据类型,如果方法参数名与http中请求的参数名称相同时会进行自动映射. ...

  9. shell脚本学习(6)awk 编排字段

    awk能取出文本字段重新编排 1 awk的用法 awk ‘program’ [file] 2 其中program 可以写成 ‘parrtern {action}’    pattern 或 actio ...

  10. Visible Lattice Points

    题目链接 题意:给个N*N的矩形点,求在原点看去能看到多少个点 思路:除了(1,0),(0,1),(1,1)外其他点的xy都互质.所以求欧拉函数.fhi[i]从2加到n,再是两倍,再加3. #incl ...