1. 程式人生 > 其它 >HDUOJ---1867 A + B for you again

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