1. 程式人生 > >【HDU - 1867 】A + B for you again(KMP,next陣列應用)

【HDU - 1867 】A + B for you again(KMP,next陣列應用)

題幹:

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

題目大意:

  就是題幹有點坑,,,

解題報告:

   這題做法很多啊,,,可以直接兩個KMP,也可以先把兩個字串連起來,然後匹配完了之後比較看選哪一種。。(因為不一定是第一個字串一定在前(取字尾)第二個字串一定在後(取字首),所以這題意有點坑的其實、、)

AC程式碼:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
char s1[MAX],s2[MAX];
char ss[MAX*2];
int Next[MAX<<1];
void getNext() {
	int k = -1,j = 0;
	int len = strlen(ss);
	Next[0] = -1;
	while(j < len) {
		if(k == -1 || ss[j] == ss[k]) {
			k++,j++;
			Next[j] = k;
		}
		else k=Next[k];
	}
}
int main()
{
	while(~scanf("%s",s1)) {
		scanf("%s",s2);		
		int len1 = strlen(s1);
		int len2 = strlen(s2);
		strcpy(ss,s2);
		strcat(ss,s1);
		getNext();
		int ans1 = Next[strlen(ss)];
		while(ans1 > len1 || ans1 > len2) ans1 = Next[ans1];
		
		strcpy(ss,s1);
		strcat(ss,s2);
		getNext();
		int ans2 = Next[strlen(ss)];
		while(ans2 > len1 || ans2 > len2) ans2 = Next[ans2];
		if((len2-ans1) + len1 < (len1 - ans2) + len2) {
			printf("%s",s1);
			printf("%s\n",s2+ans1);
		}
		else if((len2-ans1) + len1 > (len1 - ans2) + len2) {
			printf("%s",s2);
			printf("%s\n",s1+ans2);
		}
		else {
			if(strcmp(s1,s2)<0) {
				printf("%s",s1);
				printf("%s\n",s2+ans1);
			}
			else {
				printf("%s",s2);
				printf("%s\n",s1+ans2);
			}
		}
	}
	return 0 ;
 }
 /*
 aa sa
 
 應該是saa 
 
 */

AC程式碼2:

//法2
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
char s1[MAX],s2[MAX];
char ss[MAX*2];
int Next[MAX<<1];
void getNext(char ss[]) {
	int k = -1,j = 0;
	int len = strlen(ss);
	Next[0] = -1;
	while(j < len) {
		if(k == -1 || ss[j] == ss[k]) {
			k++,j++;
			Next[j] = k;
		}
		else k=Next[k];
	}
}
int KMP(char ch1[],char ch2[]) {
	getNext(ch2);
	int i=0,j=0;
	int len1 = strlen(ch1);
	int len2 = strlen(ch2);
	while(i<len1) {
		if(j == -1 || ch1[i] == ch2[j]) i++,j++;
		else j=Next[j];
	}
	return j;
}
int main()
{
	while(~scanf("%s",s1)) {
		scanf("%s",s2);		
		int len1 = KMP(s1,s2);
		int len2 = KMP(s2,s1);
		if(len1 > len2) {
			printf("%s",s1);
			printf("%s",s2+len1);
		}
		else if(len1 < len2) {
			printf("%s",s2);
			printf("%s",s1+len2);
		}
		else {
			if(strcmp(s1,s2) > 0) {
				printf("%s",s2);
				printf("%s",s1+len2);
			}
			else {
				printf("%s",s1);
				printf("%s",s2+len1);
			}
		}
		puts("");
	}
	return 0 ;
 }
 /*
 aa sa
 
 應該是saa 
 
 */