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就可以了...
代码如下:
 #include <stdio.h>
#include <string.h>
#define maxn 100000
int next[maxn+];
char ps[maxn+],pt[maxn+];
void get_next(char const * t,int *next,int const lent)
{
int i=,j=-;
memset(next,,sizeof(next));
next[]=-;
while(i<lent)
{
if(j==-||t[i]==t[j])
{
++i;
++j;
if(t[i]!=t[j])
next[i]=j;
else
next[i]=next[j]; }
else
j=next[j];
}
} int ext_kmp(char const * ps, char const *pt,int const lens,int const lent)
{
int i=lens-lent-,j=-;
get_next(pt,next,lent);
if(i<-)i=-;
while(i<lens)
{
if(j==-||ps[i]==pt[j])
{
++i;
++j;
}
else
j=next[j];
}
return j;
} int main()
{
int i,ansa,ansb;
int lens,lent;
while(scanf("%s%s",ps,pt)!=EOF)
{
lens=strlen(ps);
lent=strlen(pt);
ansa=ext_kmp(ps,pt,lens,lent);
ansb=ext_kmp(pt,ps,lent,lens);
if(ansa>ansb)
{
printf("%s",ps);
for(i=ansa;i<lent;i++)
printf("%c",pt[i]);
}
else if(ansa<ansb)
{
printf("%s",pt);
for(i=ansb;i<lens;i++)
printf("%c",ps[i]);
}
else
{
if(strcmp(ps,pt)<)
{
printf("%s",ps);
for(i=ansa;i<lent;i++)
printf("%c",pt[i]);
}
else
{
printf("%s",pt);
for(i=ansb;i<lens;i++)
printf("%c",ps[i]);
}
}
putchar();
}
return ;
}
 
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. iOS:转载:UIControl的使用

    主要功能: UIContol(控件是所有控件的基类 如:(UIButton)按钮主要用于与用户交互,通常情况下我们不会直接使用UIControl,而是子类化它. 常用属性: BOOL enabled ...

  2. 数学图形之将曲线(curve)转化成曲面管

    在我关于数学图形的博客中,一开始讲曲线的生成算法.然后在最近的章节中介绍了圆环,还介绍了螺旋管以及海螺的生成算法.一类是曲线,一类是环面,为什么不将曲线变成环的图形,毕竟曲线看上去太单薄了,这一节我将 ...

  3. OTL翻译(3) -- OTL的主要类

    相比于传统的C++类库而言,OTL更像是一个代码容器,里面复杂,但对外的接口简单.OTL在处理程序方面受到了STL的影响. OTL有一个模板框架,它实现了otl_stream的概念.该框架由模板类和内 ...

  4. 容器list使用之erase(其他容器也通用)

    今天无论如何要写点东西,算是搞清楚了一点东西吧.有点小小的成就感. 之前在程序里面使用了list容器,其中用到了erase()函数,之前一直没出现问题,这两天突然莫名奇妙.花了点时间,搞清楚了eras ...

  5. 曲线平滑-B样条曲线 【转】

    版权声明:本文为博主原创文章,未经博主允许不得转载. 3D空间曲线三次B样条平滑示例: struct D_DOT3D //D_DOT3D示例,未完全实现 { double x,y,z; } doubl ...

  6. window安装Scrapy———解决报错问题

    系统是WIN10 64位Python是3.5.2今天安装pip install Scrapy  来安装发现报错Microsoft Visual C++ 14.0 is required 检查发现电脑中 ...

  7. 网上收集:跟着 8 张思维导图学习 Javascript【转】

    学习的道路就是要不断的总结归纳,好记性不如烂笔头,so,下面将po出8张javascript相关的思维导图. 思维导图小tips:思维导图又叫心智图,是表达发射性思维的有效的图形思维工具 ,它简单却又 ...

  8. vue-router路由模式详解

    一.路由模式解析 要讲vue-router的路由模式,首先要了解的一点就是路由是由多个URL组成的,使用不同的URL可以相应的导航到不同的位置. 如果有进行过服务器开发或者对http协议有所了解就会知 ...

  9. 如何开机就启动node.js程序

      npm install -g qckwinsvc 定位到安装目录,node_modules/.bin/ 运行如下命令: > qckwinsvc prompt: Service name: [ ...

  10. [LeetCode-20]Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...