HDUOJ---1867 A + B for you again
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+1];
5 char ps[maxn+1],pt[maxn+1];
6 void get_next(char const * t,int *next,int const lent)
7 {
8 int i=0,j=-1;
9 memset(next,0,sizeof(next));
10 next[0]=-1;
11 while(i<lent)
12 {
13 if(j==-1||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-1,j=-1;
31 get_next(pt,next,lent);
32 if(i<-1)i=-1;
33 while(i<lens)
34 {
35 if(j==-1||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)<0)
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(10);
84 }
85 return 0;
86 }
Recommend
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- LeetCode95|字符串中的第一个唯一字符
- LeetCode94|Pow(x,n)
- LeetCode93|数值的整数次方
- LeetCode92|排序数组
- 缓存 | 从本地缓存到分布式缓存, Guava, Caffeine, Memcached, Redis
- WebView三问—B站真题
- C++核心准则T.140:为所有可能重用的操作命名
- Service三问
- 事件分发机制三问
- C++核心准则T.141:如果你需要只在一个地方使用的简单的函数对象,使用无名的lambda表达式
- 基于DOM的XML文件解析类
- C++核心准则T.143:避免无意中编写非通用代码
- XML文件解析实践(DOM解析)
- golang 多协程的同步方法总结
- C++核心准则T.144:不要特化函数模板