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=;
#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 (简单题)的更多相关文章
- BZOJ 2683: 简单题
2683: 简单题 Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 913 Solved: 379[Submit][Status][Discuss] ...
- 【BZOJ-1176&2683】Mokia&简单题 CDQ分治
1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 1854 Solved: 821[Submit][St ...
- Bzoj4066 简单题
Time Limit: 50 Sec Memory Limit: 20 MBSubmit: 2185 Solved: 581 Description 你有一个N*N的棋盘,每个格子内有一个整数,初 ...
- Bzoj2683 简单题
Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 1071 Solved: 428 Description 你有一个N*N的棋盘,每个格子内有一个整数, ...
- 这样leetcode简单题都更完了
这样leetcode简单题都更完了,作为水题王的我开始要更新leetcode中等题和难题了,有些挖了很久的坑也将在在这个阶段一一揭晓,接下来的算法性更强,我就要开始分专题更新题目,而不是再以我的A题顺 ...
- [BZOJ2683][BZOJ4066]简单题
[BZOJ2683][BZOJ4066]简单题 试题描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x ...
- HDU 1753 大明A+B(字符串模拟,简单题)
简单题,但要考虑一些细节: 前导0不要,后导0不要,小数长度不一样时,有进位时,逆置处理输出 然后处理起来就比较麻烦了. 题目链接 我的代码纯模拟,把小数点前后分开来处理,写的很繁杂,纯当纪念——可怜 ...
- 团体程序设计天梯赛-练习集L1-014. 简单题
L1-014. 简单题 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 这次真的没骗你 —— 这道超级简单的题目没有任何输入. ...
- bzoj 4066: 简单题 kd-tree
4066: 简单题 Time Limit: 50 Sec Memory Limit: 20 MBSubmit: 234 Solved: 82[Submit][Status][Discuss] De ...
随机推荐
- java绘制带姓的圆
public class ImageGenerator { private static final Color[] colors = new Color[] { new Color(129, 198 ...
- html中内联元素和块级元素的区别
1.下表列出了内联元素和块级元素的主要区别 html中内联元素和块级元素的区别 块级元素 行内元素 独占一行,默认情况下,其宽度自动填满其父元素宽度 相邻的行内元素会排列在同一行里,直到一行排不下,才 ...
- 2017ICPC沈阳赛现场赛 L-Tree (dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6228 题目大意:给一棵树,需要用k种颜色给树上的节点染色,问你在最优的染色方案下,相同颜色的节点连接的 ...
- css3 清除浮动
eg:三个div,父级div下面有两个div分别float:left和float:right <style> .container{width:400px;border:3px soild ...
- SSH配置与修改
ssh文件路径:/etc/ssh/ ssh的日志文件:/var/log/secure 端口修改:./sshd_config 服务启停: service sshd start/stop/restart ...
- 一分钟小知识:scroll-behavior 让你的页面导航滚动更丝滑~
中午在[掘金]潜水摸鱼,看到这一个沸点,个人已经撸出特效: 下面放上 作者 的 掘金 地址 #掘金沸点# https://juejin.im/pin/5d649eaaf265da19752533d ...
- sublime text 3 快捷操作
快捷键:1.通用 ↑↓← → 上下左右移动光标 Alt 调出菜单 Ctrl + Shift + P 调出命令板(Command Palette) Ctrl + ` 调出控制台 2.编辑 Ctrl + ...
- mysql delete from left 多表条件删除
DELETE n from news n LEFT JOIN news2 d ON n.id=d.id WHERE d.id IS null
- 接口代码(requests库安装)
一. 首先用cd:Scripts路径名命令,进入到python--Scripts目录下:然后键入pip install requests 进行安装,有可能会要求你升级pip,键入python -m ...
- Python3解leetcode Binary Tree PathsAdd DigitsMove Zeroes
问题描述: Given an array nums, write a function to move all 0's to the end of it while maintaining the r ...