HDU 1867 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): 4636    Accepted Submission(s): 1193

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杭电集训队选拔赛——热身赛

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1867

题目大意:给两个串,合并它们,如果其中一个的前缀和另一个的后缀相同,则相同部分为它们合并后的公共部分,有多种合并方式先考虑它们的长度,优先得到短的,长度相同时优先得到字典序小的

题目分析:把串s1和串s2分别与对方进行模式匹配,设匹配后得到的匹配长度为l1和l2,若l1==l2则比较字典序,比如如果s1 < s2,则输出s1和s2中与s1非公共的部分及s2 + l2,若l1 < l2输出s2和s1 + l2,s1 +l2就是s1中与s2非公共的部分

#include <cstdio>
#include <cstring>
int const MAX = 1e5 + 5;
int next[MAX];
char s1[MAX], s2[MAX];

void get_next(char *a)
{
    int i = 0, j = -1;
    next[0] = -1;
    while(a[i] != '\0')
    {
        if(j == -1 || a[i] == a[j])
        {
            i++;
            j++;
            if(a[i] == a[j])
                next[i] = next[j];
            else
                next[i] = j;
        }
        else
             j = next[j];
    }
}

int KMP(char *a, char *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)
    {
        int l1 = KMP(s1, s2);
        int l2 = KMP(s2, s1);
        if(l1 == l2)
        {
            if(strcmp(s1, s2) < 0)
                printf("%s%s\n", s1, s2 + l1);
            else
                printf("%s%s\n", s2, s1 + l1);
        }
        else if(l1 < l2)
            printf("%s%s\n", s2, s1 + l2);
        else
            printf("%s%s\n", s1, s2 + l1);
    }
}
时间: 2024-11-06 05:35:08

HDU 1867 A + B for you again (KMP应用)的相关文章

HDU 1867 A + B for you again ----KMP

题意: 给你两个字符串,输出他们合并之后的字符串,合并的时候把A的后缀和B的前缀重叠合(或者把A的前缀和B的后缀重合).要求合并后的串既包含A右包含B,且使得合并后的字符串尽量短,其次是使得合并后的字符串字典序尽量小. 分析: 首先A和B合并他们一定是首尾重叠相连,要求合并后字典序最小,所以当合并后串长度一样时,我们要把A或B中字典序小的放在前面. 然后计算A的后缀和B的前缀匹配长度为len1,计算A的前缀和B的后缀匹配长度为len2. 如果len1大,那么就把A放前面.如果len2大,那么就把

HDU 1867 A + B for you again KMP题解

本题又是一个典型的KMP应用. 求两个字符串相加的结果,相加的规律是一个字符串的后缀和另一个字符串的前缀相同,就可以合并这个部分. 不过本题的题意不是很清晰,因为没有太明确指出这两个字符串的出现顺序是无关的,只是需要输出合并后长度最短的结果,如果合并后长度一样,那么就按照字典顺序,输出字典顺序在前的字符串. 思路: 1 使用kmp在s2查找s1,那么最终结束的时候next table的值就是s1前缀和s2的后缀相同的最长的长度了. 2 输入两个字符串s1和s2,那么就可以在s2中查找s1,得到长

hdu 1867 A + B for you again

题目: 链接:点击打开链接 题意: 找出两个字符串的后缀和前缀公共串,输出最短的串,并且为最小字典序. 算法: KMP算法. 思路: 取最小字典序输出... 代码: #include<iostream> #include<cstdio> #include<cstring> using namespace std; #define MAXN 100010 char s1[MAXN],s2[MAXN]; int next[MAXN]; void get_nextval(ch

HDU 2594 Simpsons’ Hidden Talents (字符串-KMP)

Simpsons' Hidden Talents Problem Description Homer: Marge, I just figured out a way to discover some of the talents we weren't aware we had. Marge: Yeah, what is it? Homer: Take me for example. I want to find out if I have a talent in politics, OK? M

Hdu 1867 KMP

题目链接 题目意思: 给出两个字符串a, b, 求最长的公共字串c, c是a的后缀,也是b的前缀. 本题没有具体说明哪个字符串是文本串和匹配串, 所以都要考虑 思路: 查找的时候, 当文本串结束的时候, 返回匹配串的位 1 /************************************************************************* 2 > File Name: 1867.cpp 3 > Author: Stomach_ache 4 > Mail:

HDU 1867 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): 4496    Accepted Submission(s): 1157 Problem Description Generally speaking, there are a lot of problems about strings process

hdu 5442 Favorite Donut 最大表示法+KMP

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5442 题意: 有一个由小写字母组成的字符串(长度为n),首尾相接,求顺时针转和逆时针转的情况下,长度为n的最大字典序的字符串的首位的位置. 如果顺时针和逆时针求得的字符串相同,则选择开始位置较前的,如果开始位置也相同,则选择顺时针的. 如abcd,那么顺时针可以是abcd,bcda,cdab,dabc.逆时针可以是adcb,dcba,cbad,badc. 思路: 顺时针的情况下,直接求最大字典序的位

hdu 3869 Color the Simple Cycle (kmp+polya计数)

先把2*n个数字接成一个模式串P,复制两次的P为串T,然后在T上进行KMP找对P匹配的多个终点,然后就是用Polya定理了,需要求逆元. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #define mod 1000000007 6 using namespace std; 7 const int maxn=100005; 8 i

HDU 4300 Clairewd’s message(初遇拓展KMP)

昨晚一不小心学了拓展KMP,被虐了一晚,最终是这份资料救了我...http://wenku.baidu.com/view/8e9ebefb0242a8956bece4b3.html 说得简单易懂. 然后学了kuangbin的模版: /* * 扩展KMP算法 */ //next[i]:x[i...m-1]与x[0...m-1]的最长公共前缀 //extend[i]:y[i...n-1]与x[0...m-1]的最长公共前缀 void pre_EKMP(char x[],int m,int next[