HDU 1876 A + B for you again(KMP)

原创 2018年04月15日 19:13:21

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


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


题目大意:

给出两个串,要求将相同的前缀后缀合并,合并后保证串长度最短的前提下字典序最小。

思路:主要是两个串的前后缀匹配,匹配的三种方法可看:

https://blog.csdn.net/baodream/article/details/79943459

通过两次匹配看谁更短和字典序最小进行一些处理。


代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>

using namespace std;

#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)

const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const int N = 1e6+5;

string str,mo;
int Next[N];

void Get_next()
{
    int len=mo.length();
    int j=-1;
    Next[0]=-1;
    int i=0;
    while(i<len)
    {
        while(j!=-1&&mo[i]!=mo[j])
            j=Next[j];
        Next[++i]=++j;
    }
}

int KMP()
{
    int len = str.length();
    int molen = mo.length();
    int i = 0, j = 0;
    while(i<len)
    {
        while(j!=-1&&str[i]!=mo[j])
            j = Next[j];
        ++i;
        ++j;
    }
    if(j==0||j==-1)
        return 0;
    return j;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    std::ios::sync_with_stdio(false);
    while(cin>>str>>mo)
    {
        Get_next();
        int cnt1 = KMP();

        string t = str;
        str = mo;
        mo = t;
        Get_next();
        int cnt2 = KMP();   //把第二个字符串放前面匹配

        int cnt = cnt2;
        if(cnt1>cnt2)      //如果第一种方法前后匹配得更多,则交换回来
        {
            cnt = cnt1;
            t = str;
            str = mo;
            mo = t;
        }
        else if(cnt1==cnt2)   //一样多的话,看字典序谁更小
        {
            if(mo<str)
            {
                t = str;
                str = mo;
                mo = t;
            }
        }
        cout<<str;
        for(int i=cnt;i<mo.length();i++)
            cout<<mo[i];
        cout<<endl;
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baodream/article/details/79951981

HDU-2057 A+B Again

#include #include using namespace std; int main() { __int64 n,m,temp; while(scanf("%I64X...
  • somniloquy_
  • somniloquy_
  • 2015-07-27 18:39:41
  • 243

HDU1867:A + B for you again

Problem Description Generally speaking, there are a lot of problems about strings processing. Now y...
  • libin56842
  • libin56842
  • 2013-01-12 17:57:16
  • 1857

HDU2057 A + B Again【水题】

题目大意:给你两个带符号的16进制数A和B,输出A+B 思路:A和B比较大,用64位整数来存储,输入的时候用X%来控制输入、输出, 但是X%输入、输出的是无符号16进制数,应该再加个判断。 附带: 格...
  • u011676797
  • u011676797
  • 2015-01-03 14:57:45
  • 863

[杭电]A + B Again

题目:http://acm.hdu.edu.cn/showproblem.php?pid=2057 思路:有两个注意点:             (1)由于...
  • wjk20120522
  • wjk20120522
  • 2014-05-04 15:18:24
  • 723

D - A + B for you again

Generally speaking, there are a lot of problems about strings processing. Now you encounter another ...
  • ACM77
  • ACM77
  • 2017-07-21 17:20:39
  • 159

hdu2057A + B Again

A + B Again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To...
  • sungaochao
  • sungaochao
  • 2014-11-20 22:17:22
  • 360

HDU 2057 A + B Again(水)

刚刚学时想手动模拟十六进制加减法,Tooyoung, 注意:十六进制输入十六进制输出就ok。 题目告知A长度不会超过15位 A 最大为fffffffffffffff,转换成10进制为  115292...
  • qq_33266889
  • qq_33266889
  • 2016-11-28 14:48:19
  • 361

HDOJ 题目1867A + B for you again(KMP)

A + B for you again Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth...
  • yu_ch_sh
  • yu_ch_sh
  • 2014-09-01 01:16:27
  • 379

HDU 1867 A + B for you again kmp算法

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1867 题意:给定两个字符串,把一个字符串接在另一个字符串的后面,相接部位若有相同,则可以略去其中一个字符...
  • discreeter
  • discreeter
  • 2016-07-24 15:42:49
  • 307

hdu1867 A + B for you again(kmp)

题目链接:点这里!!!! 题意:(简单易懂自己看) 题解:直接kmp,但是要注意在+号前面的字符串指针一定要指到最后!!! 代码: #include #incl...
  • u014325920
  • u014325920
  • 2016-04-20 21:19:24
  • 157
收藏助手
不良信息举报
您举报文章:HDU 1876 A + B for you again(KMP)
举报原因:
原因补充:

(最多只允许输入30个字)