HDOJ A + B for you again 1867【KMP】

A + B for you again

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 5114    Accepted Submission(s): 1286

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

Source

2008杭电集训队选拔赛——热身赛

Recommend

lcy   |   We have carefully selected several similar problems for you:  1866 1277 1358 1753 1868

题意:给定两个串。。要求组成一个最短的串。如果第一个串的后缀和第二个串的前缀相同时,可以删去一个前缀或后缀就可以。最重要的是要串最短,然后是要求是字典序最小的顺序输出。

解题思路:

一开始是想利用next存取前缀后缀最大匹配的方式做,把后面的串放前面,这样就变成求前缀后缀最大匹配,结果写完发现自己想的样例过不了,各种纠结。后来去网上找解题报告发现题意理解不完全。。。。而且发现这样想是不对的,如果是cbc  bcb 这样的最大前缀后缀后缀是4, 而题目并不是这个意思。所有,就要考虑加上KMP判断,找出最大的匹配位置就可以了。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#define maxn 100000+10

using namespace std;

char P[maxn];
char S[maxn];

int pre[200010];

void getnext(char X[maxn],int xlen)
{
    int i=0,j=-1;
    pre[0]=-1;
    while(i<xlen){
        if(j==-1||X[i]==X[j]){
            i++;j++;
            if(X[j]!=X[i])
			pre[i]=j;
			else pre[i]=pre[j];
        }
        else j=pre[j];
    }
}

int kmpsearch(char *x,char *y)
{
	int i=0,j=0;
	int xlen=strlen(x);
	int ylen=strlen(y);
	getnext(x,xlen);
	while(j<xlen&&i<ylen){
		if(j==-1||x[j]==y[i]){
			i++;j++;
		}
		else j=pre[j];
	}
	if(i>=ylen)return j;
	return 0;
}

int main()
{
    while(scanf("%s%s",P,S)!=EOF){
    	int l1=kmpsearch(P,S);
    	int l2=kmpsearch(S,P);
    	if(l1==l2){
    		if(strcmp(P,S)>0){
    			printf("%s%s\n",S,P+l1);
			}
			else printf("%s%s\n",P,S+l1);
		}
		else if(l1<l2){
			printf("%s%s\n",P,S+l2);
		}
		else{
			printf("%s%s\n",S,P+l1);
		}
    }
    return 0;
}

A + B for you again

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 5114    Accepted Submission(s): 1286

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

Source

2008杭电集训队选拔赛——热身赛

Recommend

lcy   |   We have carefully selected several similar problems for you:  1866 1277 1358 1753 1868

版权声明:本文为博主原创文章,转载请注明出处。

时间: 2024-08-09 14:06:55

HDOJ A + B for you again 1867【KMP】的相关文章

hdoj 1052 Tian Ji -- The Horse Racing【田忌赛马】 【贪心】

思路:先按从小到大排序, 然后从最快的開始比(如果i, j 是最慢的一端, flag1, flag2是最快的一端 ),田的最快的大于king的 则比較,如果等于然后推断,有三种情况: 一:大于则比較,二等于在推断田的最慢的是不是比king的最快的慢,三小于则与king的最快的比較: Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe

hdoj 5131 Song Jiang&#39;s rank list 【模拟】

Song Jiang's rank list Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 653    Accepted Submission(s): 323 Problem Description <Shui Hu Zhuan>,also <Water Margin>was written by Shi Nai'

HDOJ 1142 A Walk Through the Forest 【Dijkstra】+【DFS】

题意:从2到1的所有路径中找出最短的路,并且输出最短路径有几条. 策略:先求出最短路径,然后再找出从2到1的最短路径有几条.最短路径用dijkstra算法来求出,什么是dijkstra算法,简单来说,dijkstra算法就是路径长度递增次序产生最短路径的算法: 基本思想是:把集合V分成两组: (1)S:已求出最短路径的顶点的集合 (2)V-S=T:尚未确定最短路径的顶点集合 将T中顶点按最短路径递增的次序加入到S中, 保证:(1)从源点V0到S中各顶点的最短路径长度都不大于 从V0到T中任何顶点

hdoj 1210 Eddy&#39;s 洗牌问题 【模拟】

题意:中文题,不翻译.. 策略:观察可知,第i张牌 如果小于等于n 那么他的序号就会变为i*2, 如果大于n 那么就会变成(i-n)*2-1  故 只需要模拟下就好了 AC by SWS 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1210 代码: #include<stdio.h> int main() { int n, cur, pre; while(scanf("%d", &n) == 1){ int ans

POJ2406 Power Strings 【KMP】

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 31388   Accepted: 13074 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "

POJ3461 Oulipo 【KMP】

Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22295   Accepted: 8905 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote

【KMP】【最小表示法】NCPC 2014 H clock pictures

题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个针都是相同的,分别指向Ai,Bi(360°被分成360000小份),问能否将其中一个旋转和另一个重合. 题目思路: [KMP][最小表示法] 循环同构问题.可以写KMP,我懒得写KMP了就写了循环同构的最小表示法. 首先将Ai排序,然后求差(记得取模360000,WA了一次),接下来复制一遍开始匹配. A

【动态规划】【KMP】HDU 5763 Another Meaning

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成2种意思,问s1可以解读成几种意思(mod 1000000007). 题目思路: [动态规划][KMP] 题目有点绕,看看样例就懂了.其实不用KMP直接用substr就能做. 首先不解读成另一个意思的话,f[i]=f[i-1],接着如果当前位置能够与s2匹配,那么f[i]+=f[i-strlen(s2)]

HDU3746 Cyclic Nacklace 【KMP】

Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2538    Accepted Submission(s): 1154 Problem Description CC always becomes very depressed at the end of this month, he has checke