A + B for you again

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4496    Accepted Submission(s): 1157

Problem Description
Generally
speaking, there are a lot of problems about strings processing. Now you
encounter another such problem. If you get two strings, such as “asdf”
and “sdfg”, the result of the addition between them is “asdfg”, for
“sdf” is the tail substring of “asdf” and the head substring of the
“sdfg” . However, the result comes as “asdfghjk”, when you have to add
“asdf” and “ghjk” and guarantee the shortest string first, then the
minimum lexicographic second, the same rules for other additions.
 
Input
For
each case, there are two strings (the chars selected just form ‘a’ to
‘z’) for you, and each length of theirs won’t exceed 10^5 and won’t be
empty.
 
Output
Print the ultimate string by the book.
 
Sample Input
asdf sdfg
asdf ghjk
 
Sample Output
asdfg
asdfghjk
题意讲解: 给你两个字符串 s1,s2让你把它们,连接起来前后相同的重合一起,不再输出;
连接规则:
         假设     s1 + s2  能匹配的长度为 len1 ;
                  s2 + s1 能匹配的长度为  len2 ;
如果 len1 = len2 判断谁在前就要看谁得字典序小了,当然小的在前
如果 len1 > len2   输出匹配后的字符串 s1+s2
如果 len1 < len2   输出匹配后的字符串 s2+s1
 
所以AC代码如下:略长
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<queue>
  6. #include<string>
  7. #include<cmath>
  8. using namespace std;
  9. const int N = 1e5+;
  10. char s1[N],s2[N];
  11. int next[][N],len1,len2;
  12. int x1,x2;
  13. void solve1(int len1)//寻找第一个字符串的next数组
  14. {
  15. int i = ;
  16. int j = -;
  17. next[][] = -;
  18. while(i<len1)
  19. {
  20. if(j== - || s1[i] == s1[j])
  21. {
  22. ++i;
  23. ++j;
  24. next[][i] = j;
  25. }
  26. else
  27. {
  28. j = next[][j];
  29. }
  30. }
  31. }
  32. void solve2(int len2)//寻找第二个字符串的next数组
  33. {
  34. int i = ;
  35. int j = -;
  36. next[][] = -;
  37. while(i<len2)
  38. {
  39. if(j== - || s2[i] == s2[j])
  40. {
  41. ++i;
  42. ++j;
  43. next[][i] = j;
  44. }
  45. else
  46. {
  47. j = next[][j];
  48. }
  49. }
  50. }
  51. int solve(char *s3,char *s4,int len,int x) //xx:表示字符串s3和s4匹配时,是从s3的第xx个开始匹配的
  52. {
  53. int j=,i=;
  54. int xx=;
  55. while(i<len)
  56. {
  57. if(j==- || s3[i] == s4[j])
  58. {
  59. i++;
  60. j++;
  61. }
  62. else
  63. {
  64. j = next[x][j];
  65. xx=i-j;
  66. }
  67. }
  68. return xx;
  69. }
  70. int main()
  71. {
  72. while(~scanf("%s %s",s1,s2))
  73. {
  74. memset(next,-,sizeof(next));
  75. len1 = strlen(s1);
  76. len2 = strlen(s2);
  77. solve1(len1);
  78. solve2(len2);
  79. int x1 = solve(s1,s2,len1,);
  80. int x2 = solve(s2,s1,len2,);
  81. //判断能匹配字符串的长度
  82. int xx1 = len1 - x1;
  83. int xx2 = len2 - x2;
  84. //当s1在前或者s2在前连接的字符串总长度是相同的,则要按照字典序小的在前,
  85. //例如:s1:abcefg s2:efgabc 都能匹配对方三个,所以要按照字典序abcefg 在前;
  86. if(xx1 == xx2)
  87. {
  88. if(strcmp(s1,s2)<)
  89. {
  90. for(int i=; i<x1; i++)
  91. printf("%c",s1[i]);
  92. printf("%s\n",s2);
  93. }
  94. else
  95. {
  96. for(int i=; i<x2; i++)
  97. printf("%c",s2[i]);
  98. printf("%s\n",s1);
  99. }
  100. }
  101. //接下来就看,谁能匹配谁的多了,xx1 表示s2匹配s1 的长度,xx2表示 s1 匹配 s2的长度;
  102. //例如s1: abcdef s2: hjabcd ;这时s2,在前先输出;反之s1在前;
  103. else if(xx1 > xx2)
  104. {
  105. for(int i=; i<x1; i++)
  106. printf("%c",s1[i]);
  107. printf("%s\n",s2);
  108.  
  109. }
  110. else
  111. {
  112. for(int i=; i<x2; i++)
  113. printf("%c",s2[i]);
  114. printf("%s\n",s1);
  115. }
  116. }
  117. return ;
  118. }

HDU 1867 A + B for you again(KMP算法的应用)的更多相关文章

  1. HDU 1711 Number Sequence (字符串匹配,KMP算法)

    HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...

  2. HDU 1867 A + B for you again ----KMP

    题意: 给你两个字符串,输出他们合并之后的字符串,合并的时候把A的后缀和B的前缀重叠合(或者把A的前缀和B的后缀重合).要求合并后的串既包含A右包含B, 且使得合并后的字符串尽量短,其次是使得合并后的 ...

  3. HDU 1867 A + B for you again KMP解决问题的方法

    这是一个典型问题KMP申请书. 结果求增加两个字符串.该法的总和是相同的前缀和后缀也是字符串的字符串,您将可以合并本节. 但是,这个问题是不是问题非常明确的含义,因为不是太清楚,外观这两个字符串的顺序 ...

  4. A + B for you again HDU - 1867(最大前缀&最大后缀的公共子缀&kmp删除法)

    Problem Description Generally speaking, there are a lot of problems about strings processing. Now yo ...

  5. hdu 1711 KMP算法模板题

    题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串 ...

  6. hdu 4468 spy 极其精彩的一道kmp灵活运用题

    出的超级好的一道题.至于好在哪里,请思考题目: 题意抽象出来为给定一个字符串r,找出它的一个最短后缀s,使得这个r可以被 s的某前缀+s的某前缀+......+s的某前缀+s本身构造出来. 具体题目描 ...

  7. HDU 3613 Best Reward(拓展KMP算法求解)

    题目链接: https://cn.vjudge.net/problem/HDU-3613 After an uphill battle, General Li won a great victory. ...

  8. HDU 1711 Number Sequence (字符串处理 KMP)

    题目链接 Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...

  9. hdu 1358:Period(KMP算法,next[]数组的使用)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  10. hdu 3336:Count the string(数据结构,串,KMP算法)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. \r和\n的区别

    /n  是换行/r   是回车 这是两码事,换行是指移动到下一行,回车是指移动到行首,我们通常所说的“回车”其实是这两个动作的结合.

  2. Shell合并两个文件成一个文件的两列paste,awk

    Shell合并两个文件成一个文件的两列 发布时间:2014-07-20   编辑:www.jquerycn.cn Shell合并两个文件成一个文件的两列,提供了两种方法,普通shell脚本,awk脚本 ...

  3. 用CRF++开源工具做文本序列标注教程

    本文只介绍如何快速的使用CRF++做序列标注,对其中的原理和训练测试参数不做介绍. 官网地址:CRF++: Yet Another CRF toolkit 主要完成如下功能: 输入 -> &qu ...

  4. linux自定义开机启动服务和chkconfig使用方法

    linux自定义开机启动服务和chkconfig使用方法 1. 服务概述在linux操作系统下,经常需要创建一些服务,这些服务被做成shell脚本,这些服务需要在系统启动的时候自动启动,关闭的时候自动 ...

  5. 利用PMKID破解PSK的实际测试与影响评估

    在2018年8月4日,一位研究员在hashcat论坛中发布了一篇帖子,表示他研究WPA3协议密码破解方法的过程中,发现了一个针对WPA/WPA2协议密码破解的新方法,使用PMKID(the Pairw ...

  6. Delphi中Frame的使用方法(2)

    Frame在写代码时和一般组件有什么不同呢?比如(1)中的客户信息的frame,如果想重写客户编辑按钮的click事件,会发生什么呢: procedure TBusOnSiteManager.Fram ...

  7. saltstack常用语法

    一.常用语法 1.添加用户 示例1: #添加zabbix用户和组 zabbix: group.present: - name: zabbix - gid: 1001 user.present: - f ...

  8. 流畅的python第十五章上下文管理器和else块学习记录

    with 语句和上下文管理器for.while 和 try 语句的 else 子句 with 语句会设置一个临时的上下文,交给上下文管理器对象控制,并且负责清理上下文.这么做能避免错误并减少样板代码, ...

  9. 3D游戏图形技术解析(7)——视差映射贴图(Parallax Mapping)【转】

    http://www.cnblogs.com/taotaobujue/articles/2781371.html 视差映射贴图(Parallax Mapping) ● 传统纹理贴图的弊端 纹理贴图大家 ...

  10. DevExpress 中 DateEdit 控件 格式化显示和编辑的日期格式为: yyyy-MM-dd

    摘自: http://blog.sina.com.cn/s/blog_76b5256c0100zkwk.html 1需要显示的日期为2012年3月12日需要如下设置 Properties-Mask-E ...