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. 玩转rocketMQ

    下载地址https://github.com/alibaba/RocketMQ 安装环境需要jdk,maven,git http://maven.apache.org/download.html

  2. Oracle 10g AND Oracle 11g手工建库案例--Oracle 10g

    Oracle 10g AND Oracle 11g手工建库案例--Oracle 10g 系统环境: 操作系统: RedHat EL6 Oracle:  Oracle 10g and Oracle 11 ...

  3. 简明python教程 --C++程序员的视角(五):面向对象的编程

    面向对象的编程 在大多数时候你可以使用过程性编程,但是有些时候当你想要编写大型程序或是寻求一个更加合适的解决方案的时候,你就得使用面向对象的编程技术. 对象可以使用普通的属于对象的变量存储数据.属于一 ...

  4. crtmpserver实现防盗流和流推送验证

    Protecting your streams from webpage copy&paste flash code, listing or recording 保护流,防止在页面上被复制&a ...

  5. iOS开发-Quartz2D初识

    Quartz2D如果单独的从Quartz,那么会发现Quartz是一个开源的Java作业调度框架,单独从英文翻译的角度来看的话Quartz的英文是石英,如果有的时候不小心搜索会发现手表推荐.本文中介绍 ...

  6. 触摸事件UITouch的应用

    因为UIView或者UIViewController都是继承与UIResponder ,所以都有UITouch这个事件.当用户点击屏幕的时候,会产生触摸事件. 通过UITouch事件,可以监听到开始触 ...

  7. HDOJ-3785 寻找大富翁(优先队列)

    寻找大富翁 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  8. 开源 java CMS - FreeCMS2.2 网站管理

    项目地址:http://www.freeteam.cn/ 网站管理 FreeCMS支持站点群模式,并支持无限树级管理. 网站的相关文件在site文件夹下,每一个网站有自己的文件夹,源文件文件夹名就是自 ...

  9. CF 463D Gargari and Permutations [dp]

    给出一个长为n的数列的k个排列(1 ≤ n ≤ 1000; 2 ≤ k ≤ 5).求这个k个数列的最长公共子序列的长度 dp[i]=max{dp[j]+1,where j<i 且j,i相应的字符 ...

  10. hadoop fs:du & count统计hdfs文件(目录下文件)大小的用法

    hadoop fs 更多用法,请参考官网:http://hadoop.apache.org/docs/r1.0.4/cn/hdfs_shell.html 以下是我的使用hadoop fs -du统计文 ...