A + B for you again

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

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
 
Author
Wang Ye
 
简述一下题目的意思:对于s和t两个串,将这两个串合并为一个串,但是要满足下面的 规则:
比如: 如果s串在前,t在后, 且s串后缀和t串的前缀有重合的部分,合并这部分....比如 sdsds  sdsa  -->  sdsdsa
但是要满足下面的要求:
s和t 随意组合,可以s在前,t在后,亦可以t在前s在后,但是必须报保证s+t的串长度最小,若果两者组合的长度相等,则按照字典序的顺序组合输出....
比如 abc  cdea   -->abcdea
对此,我们可以用kmp来ac就可以了...
代码如下:
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define maxn 100000
  4. int next[maxn+];
  5. char ps[maxn+],pt[maxn+];
  6. void get_next(char const * t,int *next,int const lent)
  7. {
  8. int i=,j=-;
  9. memset(next,,sizeof(next));
  10. next[]=-;
  11. while(i<lent)
  12. {
  13. if(j==-||t[i]==t[j])
  14. {
  15. ++i;
  16. ++j;
  17. if(t[i]!=t[j])
  18. next[i]=j;
  19. else
  20. next[i]=next[j];
  21.  
  22. }
  23. else
  24. j=next[j];
  25. }
  26. }
  27.  
  28. int ext_kmp(char const * ps, char const *pt,int const lens,int const lent)
  29. {
  30. int i=lens-lent-,j=-;
  31. get_next(pt,next,lent);
  32. if(i<-)i=-;
  33. while(i<lens)
  34. {
  35. if(j==-||ps[i]==pt[j])
  36. {
  37. ++i;
  38. ++j;
  39. }
  40. else
  41. j=next[j];
  42. }
  43. return j;
  44. }
  45.  
  46. int main()
  47. {
  48. int i,ansa,ansb;
  49. int lens,lent;
  50. while(scanf("%s%s",ps,pt)!=EOF)
  51. {
  52. lens=strlen(ps);
  53. lent=strlen(pt);
  54. ansa=ext_kmp(ps,pt,lens,lent);
  55. ansb=ext_kmp(pt,ps,lent,lens);
  56. if(ansa>ansb)
  57. {
  58. printf("%s",ps);
  59. for(i=ansa;i<lent;i++)
  60. printf("%c",pt[i]);
  61. }
  62. else if(ansa<ansb)
  63. {
  64. printf("%s",pt);
  65. for(i=ansb;i<lens;i++)
  66. printf("%c",ps[i]);
  67. }
  68. else
  69. {
  70. if(strcmp(ps,pt)<)
  71. {
  72. printf("%s",ps);
  73. for(i=ansa;i<lent;i++)
  74. printf("%c",pt[i]);
  75. }
  76. else
  77. {
  78. printf("%s",pt);
  79. for(i=ansb;i<lens;i++)
  80. printf("%c",ps[i]);
  81. }
  82. }
  83. putchar();
  84. }
  85. return ;
  86. }
 
Recommend

HDUOJ---1867 A + B for you again的更多相关文章

  1. hduoj 1455 && uva 243 E - Sticks

    http://acm.hdu.edu.cn/showproblem.php?pid=1455 http://uva.onlinejudge.org/index.php?option=com_onlin ...

  2. hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...

  3. hduoj 4706 Herding 2013 ACM/ICPC Asia Regional Online —— Warmup

    hduoj 4706 Children's Day 2013 ACM/ICPC Asia Regional Online —— Warmup Herding Time Limit: 2000/1000 ...

  4. hdu 1867 A + B for you again

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1867 A + B for you again Description Generally speaki ...

  5. HIT 1867 经理的烦恼

    题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=1867 每次更新时判断是否素数,如果从非素数变成素数就Update(x, 1),如果从素数变成非素数就U ...

  6. hdu-oj 1874 畅通工程续

    最短路基础 这个题目hdu-oj 1874可以用来练习最短路的一些算法. Dijkstra 无优化版本 #include<cstdio> #include<iostream> ...

  7. C#版 - HDUoj 5391 - Zball in Tina Town(素数) - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. HDUoj 5 ...

  8. C++版 - HDUoj 2010 3阶的水仙花数 - 牛客网

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - ...

  9. HDUOJ题目HTML的爬取

    HDUOJ题目HTML的爬取 封装好的exe/app的GitHub地址:https://github.com/Rhythmicc/HDUHTML 按照系统选择即可. 其实没什么难度,先爬下来一个题目的 ...

  10. HDU - 4535 ZZULI 1867: 礼上往来【错位排序】

    1867: 礼上往来 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 216  Solved: 65 SubmitStatusWeb Board Desc ...

随机推荐

  1. Ganglia开源集群监视项目

    Ganglia是UC Berkeley发起的一个开源集群监视项目,设计用于测量数以千计的节点.Ganglia的核心包含gmond.gmetad以及一个Web前端.主要 是用来监控系统性能,如:cpu ...

  2. 如何判断某版本的.NET Framework是否安装

    1..NET Framework .NET Framework2.0    键:[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\N ...

  3. 【BZOJ】【1251】序列终结者

    Splay 还是splay序列维护,这题我WA了的原因是:在Push_up的时候,当前子树的max我是直接取的L.R和v[x]的最大值,但是如果没有左/右儿子,默认是会访问0号结点的mx值,而这个值没 ...

  4. Objective-C面向对象之实现类

    一般涉及到面向对象都会C#,Java都不可避免的涉及到类,C#中类的后缀名是.cs,Java中是.java,Object-C中一般用两个文件描述一个类,后缀名为.h为类的声明文件,用于声明成员变量和方 ...

  5. retrofit okhttp RxJava bk Gson Lambda 综合示例【配置】

    项目地址:https://github.com/baiqiantao/retrofit2_okhttp3_RxJava_butterknife.git <uses-permission andr ...

  6. 只用CSS做到完全居中

    我们都知道 margin:0 auto; 的样式能让元素水平居中,而 margin: auto; 却不能做到垂直居中……直到现在.但是,请注意!想让元素绝对居中,只需要声明元素高度,并且附加以下样式, ...

  7. IE9对HTML5中一部分属性不提供支持的原因

    为什么在IE9中对于HTML5标准中的离线应用程序以及CSS3中的一部分不提供支持?笔者间接了解到了这个原因. 微软日前已经发布了Internet Explorer 9(以下简称IE9)正式版.在该版 ...

  8. MongoDB学习笔记(六)--复制集+sharding分片 && 总结

    复制集+sharding分片                                                               背景 主机 IP 服务及端口 Server A ...

  9. [Node.js]22. Level 4: Dependency

    Add two dependencies to your package.json file, connect and underscore. You'll want to useconnect ve ...

  10. 【Nodejs】使用put方式向后端查询数据并在页面显示

    前端代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Ty ...