Hot~ 2014暑期多校联合训练——正式启动报名~ 详见“杭电ACM”微博~

A + B for you again

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

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
#include<stdio.h>
#include<string.h>
const int n=100001;
int next[n];
int len1,len2;
void getnext(char s[])
{
int i=0,j=-1;
next[0]=-1;
while(i<=len2)
{
if(j==-1||s[i]==s[j])
{
i++;
j++;
next[i]=j;
}
else
j=next[j];
}
}
int kmp(char s1[],char s2[])
{
int i=0,j=0;
len1=strlen(s1);
len2=strlen(s2);
getnext(s2);
while(i<len1&&j<len2)
{
if(j==-1||s1[i]==s2[j])
{
i++;
j++;
}
else
j=next[j];
}
if(i>=len1)
return j;
else
return 0;
}
int main()
{
char str1[n],str2[n];
while(~scanf("%s%s",str1,str2))                    
/*这一题不用str1+1会更加方便,上一题剪布条的其实也可以直接用s而不用s+1,只要把getnext()和kmp()里面的一些值改一下就可以  */
{
int a=kmp(str1,str2);
int b=kmp(str2,str1);
if(a==b)
{
if(strcmp(str1,str2)>0)
{
printf("%s",str2);
printf("%s\n",str1+a);
}
else
{
printf("%s",str1);
printf("%s\n",str2+a);
}
}
else if(a>b)
{
printf("%s",str1);
printf("%s\n",str2+a);
}
else
{
printf("%s",str2);
printf("%s\n",str1+b);
}
}
}

转载于:https://www.cnblogs.com/tanjianwen/p/5245430.html

杭电1867 A + B for you again相关推荐

  1. 【杭电ACM】1097 A hard puzzle

    [杭电ACM]1097  A hard puzzle http://acm.hdu.edu.cn/showproblem.php?pid=1097 先用int手写了算法结果竟然wrong answer ...

  2. 【ACM】杭电OJ 1106 函数atoi

    函数atoi是把字符串转化成整数的函数,头文件为 #include "stdlib.h" e.g. 运行环境:Dev-C++ 5.11 杭电1106 调用了sort函数,运行的时间 ...

  3. 【ACM】杭电OJ 2037

    题目链接:杭电OJ 2037 先把b[i]进行排序,然后,b[i]与a[i+1]进行比较. #include <iostream> #include <cstdio> #inc ...

  4. 【ACM】杭电OJ 2020(排序)

    题目链接:杭电OJ 2020 排序可以有冒泡排序,选择排序,或者直接调用函数. 下面是选择排序: #include <stdio.h> #include <math.h> in ...

  5. 【ACM】杭电OJ 2018

    题目链接:杭电OJ 2018 从n>4开始,每一年的牛的数量=前一年的牛的数量+三年前的牛的数量 问:为什么是三年前? 答:假设三年前有一头小牛出生,出生的那一年即为第一年,到了第四年,即三年后 ...

  6. 【ACM】杭电OJ 1005

     题目链接:杭电OJ 1005 超时代码如下(而且开辟的数组空间大小不够): #include <stdio.h> int m[100000]; int f(int n,int a,int ...

  7. 【ACM】杭电OJ 1004

     题目链接:杭电OJ 1004 运行环境:Dev-C++ 5.11 思路: 先把先把num数组全部赋值为1:第一个颜色单独输入,从第二个开始,需要与前面的进行比较,如果前面有相同的颜色,则在目前的nu ...

  8. 【ACM】杭电OJ 2012。

    题目链接:杭电OJ 2012 思路很简单,但是有一种高效算法显示编译错误,不知道为什么 运行环境:VS2017 AC代码: #include <stdio.h> #include < ...

  9. 【ACM】杭电OJ 1003。

    运行环境VS2017  题目链接:杭电OJ 1003 主要思想是: 用d[i]来存放前i项中最大的和,得到end,然后再倒推,得起始的位置begin 然而在程序42行的疑问,大家可以讨论一下吗???? ...

最新文章

  1. 每天学习Linux(3)---pwd命令
  2. 使用apache的activemq集合JMS处理异步消息
  3. Linux磁盘怎么备份,Linux的硬盘分区表备份与还原
  4. Atitit. 破解  拦截 绕过 网站 手机 短信 验证码  方式 v2 attilax 总结
  5. ios uiview 如何刷新_ios – 从另一个UIViewController刷新表
  6. 微信小程序开发系列四:微信小程序之控制器的初始化逻辑
  7. 洛谷P1852:跳跳棋(LCA,树形结构)
  8. 11input/output
  9. php取不到post数据库,安卓post 数据到php 在写入数据库老是不成功, 数据post不到php...
  10. 将数据库中一张表中数据复制到另一张表
  11. java e.getmessage() null,浅谈Java异常的Exception e中的egetMessage()和toString()方法的区别...
  12. 虚拟机克隆后如何修复网络、主机名等问题
  13. php各种变量特点,(二)PHP语法的特点,变量,常量
  14. Linux Unix shell 编程指南学习笔记(第二部分)
  15. 【交叉/综合/新兴】 2019年-中国计算机学会推荐国际学术会议和期刊目录(十)
  16. Typecho 源码分析(8)-- 后台插件列表
  17. 多功能音频工具的旗舰音乐编辑工具!Music Studio
  18. 华为、魅族手机不显示Log的原因
  19. 用Pythn言何实现你的股票量交模型
  20. dcloud html5 sdk,DCloud H5与Android Native的交互--5+ SDK插件开发

热门文章

  1. 通过源码分析Mybatis是如何返回数据库生成的自增主键值?
  2. 关于高并发,我想告诉你这些!
  3. 刚毕业的我,参与了一场大厂之间的争夺之战
  4. 你写的 Java 代码是如何一步步输出结果的
  5. 从考研说起:大学、职场、个人选择
  6. 7.wait和waitpid
  7. 私有云的优缺点_2019年中国云计算行业竞争格局与发展趋势分析「图」
  8. 计算机二级各个科目的区别,计算机二级考试的各个科目的内容及区别.doc
  9. android camera入门,android 基础一 Camera1 预览、拍照
  10. java爬虫下载图片到磁盘_java入门爬虫(爬取网页的图片下载到本地磁盘)