链接:https://ac.nowcoder.com/acm/contest/148/J?&headNav=acm
来源:牛客网

Rikka with Nickname
时间限制: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.)
链接:https://ac.nowcoder.com/acm/contest/148/J?&headNav=acm
来源:牛客网

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.
输入描述:
链接:https://ac.nowcoder.com/acm/contest/148/J?&headNav=acm
来源:牛客网

输出描述:
For each testcase, output a single line with a single string, the result nickname.
链接:https://ac.nowcoder.com/acm/contest/148/J?&headNav=acm
来源:牛客网

示例1
输入
复制
2
2
lubenwei
niubi
3
aa
ab
abb
输出
复制
lubenweiubi
aabb

题意:

思路:

维护一个vector a[i] 数组,a[i]代表ans中字符i分别存在哪些位置。

对于每一个新尝试加入的字符,我们去vector中二分查找尽可能靠前的位置,如果找不到就只能老老实实的加入到ans中。

能找到的话,把位置赋值为last,在 下一次查找中使用。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/std::vector<int> v[50];
string ans, str;
int n;
int main()
{//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);//freopen("D:\\common_text\code_stream\\out.txt","w",stdout);int t;gbtb;cin >> t;while (t--){// MS0(f);for (char i = 'a'; i <= 'z'; ++i){v[i - 'a'].clear();}cin >> n;ans = "";int last = -1;cin >> ans;rep(i, 0, sz(ans)){v[ans[i] - 'a'].push_back(i);}repd(i, 2, n){cin >> str;int len = str.length();last = -1;repd(j, 0, len - 1){if (sz(v[str[j] - 'a'])){int id = lower_bound(ALL(v[str[j] - 'a']), last) - v[str[j] - 'a'].begin();if (id == sz(v[str[j] - 'a'])){ans.push_back(str[j]);v[str[j] - 'a'].push_back(sz(ans) - 1);last = inf;} else{last = v[str[j] - 'a'][id] + 1;}} else{ans.push_back(str[j]);v[str[j] - 'a'].push_back(sz(ans) - 1);last = inf;}}}cout << ans << endl;}return 0;
}inline void getInt(int* p) {char ch;do {ch = getchar();} while (ch == ' ' || ch == '\n');if (ch == '-') {*p = -(getchar() - '0');while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 - ch + '0';}}else {*p = ch - '0';while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 + ch - '0';}}
}

转载于:https://www.cnblogs.com/qieqiemin/p/11220038.html

2018牛客网暑期ACM多校训练营(第十场)J Rikka with Nickname(二分,字符串)相关推荐

  1. 牛客网暑期ACM多校训练营(第二场)J farm (二维树状数组)

    题目链接: https://www.nowcoder.com/acm/contest/140/J 思路: 都写在代码注释里了,非常好懂.. for_each函数可以去看一下,遍历起vector数组比较 ...

  2. 牛客网暑期ACM多校训练营(第九场)

    牛客网暑期ACM多校训练营(第九场) A. Circulant Matrix 做法:看到下标 \(xor\) 这种情况就想 \(FWT\),可是半天没思路,于是放弃了..其实这个 \(n\) 疯狂暗示 ...

  3. 牛客网暑期ACM多校训练营(第一场)

    牛客网暑期ACM多校训练营(第一场) A. Monotonic Matrix 考虑0和1的分界线,1和2的分界线,发现问题可以转化为两条不互相穿过的路径的方案数(可重叠),题解的做法就是把一条路径斜着 ...

  4. 2018牛客网暑期ACM多校训练营(第十场)A Rikka with Lowbit (树状数组)

    链接:https://ac.nowcoder.com/acm/contest/148/A 来源:牛客网 Rikka with Lowbit 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C ...

  5. 牛客网暑期ACM多校训练营(第二场): H. travel(树形线头DP)

    链接:https://ac.nowcoder.com/acm/contest/140/H 来源:牛客网 题目描述 White Cloud has a tree with n nodes.The roo ...

  6. 牛客网暑期ACM多校训练营(第三场): E. Sort String(KMP)

    链接:https://www.nowcoder.com/acm/contest/141/E 来源:牛客网 题目描述 Eddy likes to play with string which is a ...

  7. 牛客网暑期ACM多校训练营(第三场): C. Shuffle Cards(splay)

    链接:https://www.nowcoder.com/acm/contest/141/C 来源:牛客网 题目描述 Eddy likes to play cards game since there ...

  8. 牛客网暑期ACM多校训练营(第二场)A .run

    链接:https://www.nowcoder.com/acm/contest/140/A 来源:牛客网 题目描述 White Cloud is exercising in the playgroun ...

  9. 2018牛客网暑期ACM多校训练营第二场 D - money(贪心)

    题目链接 https://www.nowcoder.com/acm/contest/140#question [题目描述] White Cloud is exercising in the playg ...

最新文章

  1. 最先进的AI还不如动物聪明?首届AI-动物奥运会英国开赛!
  2. 和菜鸟一起学证券投资之股市常见概念公式1
  3. LeetCode - 231. Power of Two
  4. IDEA创建Servlet项目
  5. tab菜单的点击的动态效果和内容页面的关联显示jQuery
  6. 计算机视觉---4---多视角几何学
  7. 参数整定临界比例度实验_PID理解起来很难?系统讲解PID控制及参数调节,理论加实际才好!...
  8. 服务器上装filezilla server后,本地的ftp客户端连接不上去
  9. Vue项目npm打包推荐方式
  10. putty的的颜色配置步骤
  11. 华为云基于云原生媒体网络,又出重磅新品
  12. 许多自己正在总结的东东
  13. ngrok实现内网穿透
  14. 【C++】【GADL】读取栅格数据(tif),遍历数组
  15. 【Gym - 101234J】Zero Game【单调队列】
  16. c语言文件包含试题,C语言文件练习题含答案
  17. android adb sdk下载地址,Android SDK 下载和安装
  18. Windows Server 2008 R2远程桌面服务配置和授权激活
  19. 分享几种设为首页的代码
  20. “蔚来杯“2022牛客暑期多校训练营5 Don‘t Starve

热门文章

  1. nodejs 从TCP套接字读取并解析数据
  2. 介绍两个好玩的和Github相关的Chrome扩展 1
  3. 从OpenFOAM的源码中查找信息
  4. js鼠标移动到指定位置_Python: pyautogui模块之鼠标控制
  5. reactor和thread线程_Reactor模型详解:单Reactor多线程与主从Reactor多线程
  6. es文件浏览器怎么用_es文件浏览器电视版下载-es文件浏览器电视tv版下载v4.2.3.4 安卓最新版...
  7. UE4学习-鼠标事件(按下、释放、物体抓取、计算重量、触发开门)
  8. linux查看主机端口进程命令
  9. hbase集群之间数据迁移_hbase数据迁移到另一集群上
  10. php 代码如何使用,PHP如何使用strval()函数?用法和代码示例