• Codeforces Round #726 (Div. 2) E1. Erase and Extend (Easy Version)


    翻译:

    这是这个问题的简单版本。唯一的区别是𝑛和𝑘上的约束。只有当所有版本的问题都解决了,你才能进行hack

    你有一个字符串𝑠,你可以对它做两种类型的操作:

    删除字符串的最后一个字符。
    复制字符串:𝑠:=𝑠+𝑠,其中+表示连接。
    每个操作可以使用任意次数(可能不使用)。

    您的任务是通过对字符串𝑠执行这些操作,找到长度恰好为𝑘的字典编纂学上最小的字符串。

    当且仅当下列条件之一满足时,字符串𝑎在字典结构上小于字符串𝑏:

    𝑎是𝑏的前缀,但𝑎≠𝑏;
    在𝑎和𝑏不同的第一个位置上,字符串𝑎在字母表中出现的字母比𝑏中相应的字母更早。
    输入
    第一行包含两个整数𝑛,𝑘(1≤𝑛,𝑘≤5000)—原始字符串𝑠的长度和所需字符串的长度。

    第二行包含字符串𝑠,由𝑛小写英文字母组成。

    输出
    打印长度为𝑘的按字典顺序最小的字符串,该字符串可以通过对字符串𝑠进行操作获得。

    例子
    inputCopy
    8 16
    dbcadabc
    outputCopy
    dbcadabcdbcadabc
    inputCopy
    4个5
    abcd
    outputCopy
    五星级
    请注意
    在第一个测试中,做一个副本是最优的:"dbcadabc"→"dbcadabcdbcadabc"。

    在第二个测试中,最好是删除最后3个字符,然后复制字符串3次,然后删除最后3个字符,使字符串的长度为𝑘。

    “abcd”→“abc”→“ab”→“a”→“aa”→“aaaa”→“aaaaaaaa”→“aaaaaaa”→“aaaaaa”→“aaaaa”。

    思路:

    可以删除最后一个,然后复制字符串,然后构成长度为k的,其字典序最小的字符串。所以我们直接从前往后每次比较,如果前边的字符比后面的小则可以替代,如果相同则就是比较下一位,然后来不断更新最小的串。

    上边的一份代码是暴力模拟T掉,因为这样模拟,遇到全是小回文串的字符串,每次都会跑完,直接被无情卡掉。

    代码:

    1. //#include
    2. //#include
    3. //#include
    4. //#include
    5. //#include
    6. //#include
    7. //#include
    8. //#include
    9. //#include
    10. //#include
    11. //#include
    12. //#include
    13. //#include
    14. //using namespace::std;
    15. //typedef long long ll;
    16. //inline __int128 read(){
    17. // __int128 x = 0, f = 1;
    18. // char ch = getchar();
    19. // while(ch < '0' || ch > '9'){
    20. // if(ch == '-')
    21. // f = -1;
    22. // ch = getchar();
    23. // }
    24. // while(ch >= '0' && ch <= '9'){
    25. // x = x * 10 + ch - '0';
    26. // ch = getchar();
    27. // }
    28. // return x * f;
    29. //}
    30. //inline void print(__int128 x){
    31. // if(x < 0){
    32. // putchar('-');
    33. // x = -x;
    34. // }
    35. // if(x > 9)
    36. // print(x / 10);
    37. // putchar(x % 10 + '0');
    38. //}
    39. //int n,k;
    40. //string s;
    41. //int main(){
    42. // ios::sync_with_stdio(false);
    43. // cin.tie(); cout.tie();
    44. // int jk=0;
    45. // cin>>n>>k>>s;
    46. // for (int i =1; i
    47. // if (s[i]!=s[i-1]) {
    48. // jk=1;break;
    49. // }
    50. // }
    51. // if (n==1||!jk) {
    52. // for (int i =1; i<=k; i++) {
    53. // cout<
    54. // }cout<<"\n";
    55. // return 0;
    56. // }
    57. // char ss=s[0];
    58. // string a;
    59. // a=a+s[0];
    60. // int bj=0;
    61. // for (int i =1; i
    62. // if (s[i]>ss) {
    63. // bj=i;
    64. // break;
    65. // }
    66. // if (s[i]==ss) {
    67. // string dd="";
    68. // dd=s[i];
    69. // int l=1,r=i+1;
    70. // if (s[l]
    71. // break;
    72. // }
    73. // while (r
    74. // dd=dd+s[l];
    75. // l++,r++;
    76. // if (s[l]
    77. // bj=r;
    78. // break;
    79. // }
    80. // }
    81. // if (bj!=0) {
    82. // break;
    83. // }
    84. // }
    85. // a=a+s[i];
    86. // }
    87. // for (int i =1; i<=k/a.size(); i++) {
    88. // cout<
    89. // }
    90. // for (int i =0; i
    91. // cout<
    92. // }cout<<"\n";
    93. // return 0;
    94. //}
    95. #include
    96. #include
    97. #include
    98. #include
    99. #include
    100. #include
    101. #include
    102. #include
    103. #include
    104. #include
    105. #include
    106. #include
    107. #include
    108. using namespace::std;
    109. typedef long long ll;
    110. inline __int128 read(){
    111. __int128 x = 0, f = 1;
    112. char ch = getchar();
    113. while(ch < '0' || ch > '9'){
    114. if(ch == '-')
    115. f = -1;
    116. ch = getchar();
    117. }
    118. while(ch >= '0' && ch <= '9'){
    119. x = x * 10 + ch - '0';
    120. ch = getchar();
    121. }
    122. return x * f;
    123. }
    124. inline void print(__int128 x){
    125. if(x < 0){
    126. putchar('-');
    127. x = -x;
    128. }
    129. if(x > 9)
    130. print(x / 10);
    131. putchar(x % 10 + '0');
    132. }
    133. int n,m;
    134. string s,d,f;
    135. int main(){
    136. ios::sync_with_stdio(false);
    137. cin.tie(); cout.tie();
    138. cin>>n>>m>>s;
    139. d=s[0];
    140. for (int i =2; i<=n; i++) {
    141. f=s.substr(0,i);
    142. if (f+d
    143. d=f;
    144. }
    145. }
    146. for (int i =0; i
    147. cout<size()];
    148. }cout<
    149. return 0;
    150. }

  • 相关阅读:
    【异常】springboot集成@Cacheable缓存乱码的问题解决方案
    Bootstrap——Bootstrap学习笔记以及案例分享
    Hadoop的概述与安装
    [iOS]-NSOperation、NSOperationQueue
    额外的迭代器
    智能穿戴领域,健康鞋步力宝品牌引领新商业模式发展
    林业数据可视化新篇章:山海鲸软件看板设计心得
    WPF使用TextBlock实现查找结果高亮显示
    flutter 应用 抓包
    智能化安全防护:AI防火墙的原理与应用
  • 原文地址:https://blog.csdn.net/weixin_63555280/article/details/128116176