HDU1867 A + B for you again KMP应用

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

解题思路

对两个字符串分别KMP,得到相同前后缀长度。再根据长度判断该如何输出

#include <cstdio>
#include <cstring>
const int maxn = 100010;
int next[maxn];
char s1[maxn],s2[maxn];
int len1,len2;
void get_next(char *a)
{
    int len = strlen(a);
    int k = -1;
    int j = 0;
    next[j] = k;
    while(a[j] != ‘\0‘) {
        if(k == -1 || a[j] == a[k]) {
            k++;j++;
            next[j] = k;
        }else k = next[k];
    }
}
int kmp(char *a,char *b)
{ //a主串 b模式串
    get_next(b);
    int i = 0,j = 0;
    while(a[i] != ‘\0‘) {
        if(j == -1 || a[i] == b[j]) {
            i++;j++;
        }else j = next[j];
    }
    return j;
}
int main()
{
    while(scanf("%s%s",s1,s2) != EOF) {
        len1 = strlen(s1);
        len2 = strlen(s2);
        int str1 = kmp(s1,s2);
        int str2 = kmp(s2,s1);
        if(str1 == str2) {
            if(strcmp(s1,s2) < 0) printf("%s%s\n",s1,s2+str1);
            else printf("%s%s\n",s2,s1+str1);
        }else if(str1 < str2) {
            printf("%s%s\n",s2,s1+str2);
        }else printf("%s%s\n",s1,s2+str1);
    }
    return 0;
}
时间: 2024-07-30 14:27:25

HDU1867 A + B for you again KMP应用的相关文章

HDU1867:A + B for you again(KMP)

题意:找出一个最大的公共子串,这个子串是一个字符串的尾串(tail substring ),同时是另外那个字符串的头串(head substring),是满足A+B的长度strlen(A+B)达到最小值,这里面要注意的一个问题是,谁做模式串P是不一定的,所以要分别比较不同字符串作为模式的KMP值. 思路:让两个串分别做模式串.看谁KMP的结束的时候匹配的字符字符的个数最多就好. 当然也可以把两个串连接起来再求一次next[len1+len2]即可(求next相当于自我匹配),这时候要注意ans=

HDU1867 A + B for you again【KMP】

A + B for you again Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9765 Accepted Submission(s): 2410 Problem Description Generally speaking, there are a lot of problems about strings processing.

HDU1867 A + B for you again(KMP)

A + B for you again Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7978    Accepted Submission(s): 2005 Problem Description Generally speaking, there are a lot of problems about strings process

hdu1867(kmp)

今天估计一直要刷kmp,kmp,kmp,kmp.... 这题目非常容易理解,就是A+B问题,不同的是,要找到A串后缀与B串前缀的最长串. 比如 ABC+BC -> ABC  , ABC+BCD =ABCD   ,ABCD+ BC=ABCDBC 用的就是kmp啦,输入两个串 str1 str2 ,以str1模式串,str2为文本匹配,以str2为模式串,str1为文本串,分别匹配出最长的长度.再比较. /***********************************************

Educational Codeforces Round 21 G. Anthem of Berland(dp+kmp)

题目链接:Educational Codeforces Round 21 G. Anthem of Berland 题意: 给你两个字符串,第一个字符串包含问号,问号可以变成任意字符串. 问你第一个字符串最多包含多少个第二个字符串. 题解: 考虑dp[i][j],表示当前考虑到第一个串的第i位,已经匹配到第二个字符串的第j位. 这样的话复杂度为26*n*m*O(fail). fail可以用kmp进行预处理,将26个字母全部处理出来,这样复杂度就变成了26*n*m. 状态转移看代码(就是一个kmp

hiho 1015 KMP算法 &amp;&amp; CF 625 B. War of the Corporations

#1015 : KMP算法 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一只河蟹,于是河蟹就向小Hi和小Ho提出了那个经典的问题:“小Hi和小Ho,你们能不能够判断一段文字(原串)里面是不是存在那么一些……特殊……的文字(模式串)?” 小Hi和小Ho仔细思考了一下,觉得只能想到很简单的做法,但是又觉得既然河蟹先生这么说了,就

hdu2328 Corporate Identity 扩展KMP

Beside other services, ACM helps companies to clearly state their "corporate identity", which includes company logo but also other signs, like trademarks. One of such companies is Internet Building Masters (IBM), which has recently asked ACM for

KMP算法详解

这几天学习kmp算法,解决字符串的匹配问题,开始的时候都是用到BF算法,(BF(Brute Force)算法是普通的模式匹配算法,BF算法的思想就是将目标串S的第一个字符与模式串T的第一个字符进行匹配,若相等,则继续比较S的第二个字符和 T的第二个字符;若不相等,则比较S的第二个字符和T的第一个字符,依次比较下去,直到得出最后的匹配结果.BF算法是一种蛮力算法.)虽然也能解决一些问题,但是这是常规思路,在内存大,数据量小,时间长的情况下,还能解决一些问题,但是如果遇到一些限制时间和内存的字符串问

2016——3——16 kmp习题

1.传送门:http://begin.lydsy.com/JudgeOnline/problem.php?id=2725 题目大意:找一个串在另一个串中出现的次数 题解:kmp(纯裸题) 2.传送门:http://begin.lydsy.com/JudgeOnline/problem.php?id=2732 题目大意:给你一个字符串,让你求出最大重复周期(最大周期不和本身重合) 题解:kmp或者扩展kmp(但我并未用这种方法),我用的是kmp,但是一直WA,原来是求next数组把while写成了