前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDUOJ---1867 A + B for you again

HDUOJ---1867 A + B for you again

作者头像
Gxjun
发布2018-03-22 11:22:37
5960
发布2018-03-22 11:22:37
举报
文章被收录于专栏:ml

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就可以了...

代码如下:

代码语言:javascript
复制
 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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2013-12-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • A + B for you again
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档