链接: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,在 下一次查找中使用。

细节见代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <queue>
  7. #include <stack>
  8. #include <map>
  9. #include <set>
  10. #include <vector>
  11. #include <iomanip>
  12. #define ALL(x) (x).begin(), (x).end()
  13. #define rt return
  14. #define dll(x) scanf("%I64d",&x)
  15. #define xll(x) printf("%I64d\n",x)
  16. #define sz(a) int(a.size())
  17. #define all(a) a.begin(), a.end()
  18. #define rep(i,x,n) for(int i=x;i<n;i++)
  19. #define repd(i,x,n) for(int i=x;i<=n;i++)
  20. #define pii pair<int,int>
  21. #define pll pair<long long ,long long>
  22. #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
  23. #define MS0(X) memset((X), 0, sizeof((X)))
  24. #define MSC0(X) memset((X), '\0', sizeof((X)))
  25. #define pb push_back
  26. #define mp make_pair
  27. #define fi first
  28. #define se second
  29. #define eps 1e-6
  30. #define gg(x) getInt(&x)
  31. #define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
  32. using namespace std;
  33. typedef long long ll;
  34. ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
  35. ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
  36. 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;}
  37. inline void getInt(int* p);
  38. const int maxn = 1000010;
  39. const int inf = 0x3f3f3f3f;
  40. /*** TEMPLATE CODE * * STARTS HERE ***/
  41. std::vector<int> v[50];
  42. string ans, str;
  43. int n;
  44. int main()
  45. {
  46. //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
  47. //freopen("D:\\common_text\code_stream\\out.txt","w",stdout);
  48. int t;
  49. gbtb;
  50. cin >> t;
  51. while (t--)
  52. {
  53. // MS0(f);
  54. for (char i = 'a'; i <= 'z'; ++i)
  55. {
  56. v[i - 'a'].clear();
  57. }
  58. cin >> n;
  59. ans = "";
  60. int last = -1;
  61. cin >> ans;
  62. rep(i, 0, sz(ans))
  63. {
  64. v[ans[i] - 'a'].push_back(i);
  65. }
  66. repd(i, 2, n)
  67. {
  68. cin >> str;
  69. int len = str.length();
  70. last = -1;
  71. repd(j, 0, len - 1)
  72. {
  73. if (sz(v[str[j] - 'a']))
  74. {
  75. int id = lower_bound(ALL(v[str[j] - 'a']), last) - v[str[j] - 'a'].begin();
  76. if (id == sz(v[str[j] - 'a']))
  77. {
  78. ans.push_back(str[j]);
  79. v[str[j] - 'a'].push_back(sz(ans) - 1);
  80. last = inf;
  81. } else
  82. {
  83. last = v[str[j] - 'a'][id] + 1;
  84. }
  85. } else
  86. {
  87. ans.push_back(str[j]);
  88. v[str[j] - 'a'].push_back(sz(ans) - 1);
  89. last = inf;
  90. }
  91. }
  92. }
  93. cout << ans << endl;
  94. }
  95. return 0;
  96. }
  97. inline void getInt(int* p) {
  98. char ch;
  99. do {
  100. ch = getchar();
  101. } while (ch == ' ' || ch == '\n');
  102. if (ch == '-') {
  103. *p = -(getchar() - '0');
  104. while ((ch = getchar()) >= '0' && ch <= '9') {
  105. *p = *p * 10 - ch + '0';
  106. }
  107. }
  108. else {
  109. *p = ch - '0';
  110. while ((ch = getchar()) >= '0' && ch <= '9') {
  111. *p = *p * 10 + ch - '0';
  112. }
  113. }
  114. }

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

  1. 2018牛客网暑期ACM多校训练营(第二场)G Transform(二分)

    题意 在一个数轴上有n个集装箱,第 i 个集装箱的位置为x[i],且在集装箱内装有a[i]件货物,现在将这些集装箱内的货物进行移动(将一件货物从第 i 个集装箱移动到第 j 个集装箱的花费就为2*ab ...

  2. 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)

    2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...

  3. 2018牛客网暑期ACM多校训练营(第一场)D图同构,J

    链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 同构图:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所 ...

  4. 2018 牛客网暑期ACM多校训练营(第一场) E Removal (DP)

    Removal 链接:https://ac.nowcoder.com/acm/contest/139/E来源:牛客网 题目描述 Bobo has a sequence of integers s1, ...

  5. 2018牛客网暑期ACM多校训练营(第二场)J Farm(树状数组)

    题意 n*m的农场有若干种不同种类作物,如果作物接受了不同种类的肥料就会枯萎.现在进行t次施肥,每次对一个矩形区域施某种类的肥料.问最后枯萎的作物是多少. 分析 作者:xseventh链接:https ...

  6. 2018牛客网暑期ACM多校训练营(第一场)B Symmetric Matrix(思维+数列递推)

    题意 给出一个矩阵,矩阵每行的和必须为2,且是一个主对称矩阵.问你大小为n的这样的合法矩阵有多少个. 分析 作者:美食不可负064链接:https://www.nowcoder.com/discuss ...

  7. 2018牛客网暑期ACM多校训练营(第二场) J - farm - [随机数哈希+二维树状数组]

    题目链接:https://www.nowcoder.com/acm/contest/140/J 时间限制:C/C++ 4秒,其他语言8秒 空间限制:C/C++ 262144K,其他语言524288K ...

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

    题目链接:https://www.nowcoder.com/acm/contest/140/A 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言262144K ...

  9. 2018牛客网暑期ACM多校训练营(第一场) D - Two Graphs - [无向图同构]

    题目链接:https://www.nowcoder.com/acm/contest/139/D 题目描述 Two undirected simple graphs  and  where  are i ...

  10. 2018牛客网暑期ACM多校训练营(第一场) J - Different Integers - [莫队算法]

    题目链接:https://www.nowcoder.com/acm/contest/139/J 题目描述  Given a sequence of integers a1, a2, ..., an a ...

随机推荐

  1. 浏览器端-W3School-JavaScript-HTML DOM:HTML DOM Attribute 对象

      ylbtech-浏览器端-W3School-JavaScript-HTML DOM:HTML DOM Attribute 对象 1.返回顶部 1. HTML DOM Attribute 对象 HT ...

  2. python - lambda 函数使用

    # if we need it only once and it's quite simple def make_incrementor(n): return lambda x: x + n f = ...

  3. matplotlib之折线图

    1.案例一 # coding=utf-8 from matplotlib import pyplot as plt import random # 设置字体相关 from matplotlib imp ...

  4. C# 实现播放RTSP 标准协议码流播放

    http://www.codeproject.com/Articles/507218/Managed-Media-Aggregation-using-Rtsp-and-Rtphttp://www.st ...

  5. Android的消息机制之ThreadLocal的工作原理

    ThreadLocal 可以把一个对象保存在指定的线程中,对象保存后,只能在指定线程中获取保存的数据,对于其他线程来说则无法获取到数据. 日常开发中 ThreadLocal 使用的地方比较少,但是系统 ...

  6. 关于struts2防止表单重复提交

    struts2防表单重复提交有两种方式. 其一是action的重定向,跳转时设置type为从一个action跳转到另一个action或者另一个页面, 使用户提交后,所停留的位置,不是当前处理数据的Ac ...

  7. Redis 入门 3.3 散列类型

    3.3.1 介绍   散列类型(hash)的键值也是一种字典结构,其储存了字段(field)和字段值的映射,但字段值只能是字符串,不支持其他数据类型,换句话说,散列类型不能嵌套其他的数据类型.一个散列 ...

  8. Python学习之数据库

    9.6 表的查询 [结构]select distinct 字段1,字段2 from 表名 where 条件 group by 字段 having 筛选 order by 字段 limit 限制条数 [ ...

  9. CentOS8Linux中配置网易云网络yum源安装软件

    在CentOS8Linux中配置网易云网络yum源安装软件 前提是你的操作系统是CentOS-Linux 你已经配置好了本地yum源,并且你的网络是可用的. 本地yum源配置请参考:https://w ...

  10. 使用iwebshop開發實現QQ第三方登錄

    $appid = "101353491"; $appkey = "df4e46ba7da52f787c6e3336d30526e4"; $redirect_ur ...