HDUOJ---1867 A + B for you again

时间:2022-05-05
本文章向大家介绍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