【HDU - 1867 】A + B for you again(KMP,next数组应用)

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/83545276

题干:

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

题目大意:

  就是题干有点坑,,,

解题报告:

扫描二维码关注公众号,回复: 3871600 查看本文章

   这题做法很多啊,,,可以直接两个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 
 
 */

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/83545276