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. Oracle 10g AND Oracle 11g手工建库案例--Oracle 10g

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

  2. 如何判断一个文本文件内容的编码格式 UTF-8 ? ANSI(GBK)

    转自:http://blog.csdn.net/jiangqin115/article/details/42684017 UTF-8编码的文本文档,有的带有BOM (Byte Order Mark, ...

  3. jquery json实现二级动态联动

    以下为代码!需要导入json架包 function getCity1(){ var unitid = document.getElementById('addformunitid').value; $ ...

  4. go语言进阶之为结构体类型添加方法

    1.为结构体类型添加方法 示例: package main import "fmt" type Person struct { name string //名字 sex byte ...

  5. Longest Valid Parentheses leetcode java

    题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...

  6. OpenGL使用libPng读取png图片

    #include<stdarg.h> #include<png.h> #include<glut.h> #include<math.h> #includ ...

  7. scala多线程

    object Test { def main(args: Array[String]) { //创建线程池 val threadPool:ExecutorService=Executors.newFi ...

  8. COM的一些基本概念

      Windows lets you share code at the binary level using DLLs. After all, that's how Windows apps fun ...

  9. [ kvm ] 四种简单的网络模型

    1. 隔离模式:虚拟机之间组建网络,该模式无法与宿主机通信,无法与其他网络通信,相当于虚拟机只是连接到一台交换机上.    2. 路由模式:相当于虚拟机连接到一台路由器上,由路由器(物理网卡),统一转 ...

  10. 使用Fiddler抓包调试https下的页面

    众所周知https技术诞生以来,一个很重要的作用就是加密通信内容.所以在项目团队将业务站点实施完https改造以后,原先使用fiddler进行抓包的美好生活到头了.其实fiddler本身是支持对于ht ...