题意:

给你两个字符串,输出他们合并之后的字符串,合并的时候把A的后缀和B的前缀重叠合(或者把A的前缀和B的后缀重合)。要求合并后的串既包含A右包含B,

且使得合并后的字符串尽量短,其次是使得合并后的字符串字典序尽量小.

分析:

首先A和B合并他们一定是首尾重叠相连,要求合并后字典序最小,所以当合并后串长度一样时,我们要把A或B中字典序小

  的放在前面。

然后计算A的后缀和B的前缀匹配长度为len1,计算A的前缀和B的后缀匹配长度为len2。

如果len1大,那么就把A放前面。如果len2大,那么就把B放前面。

关键点:

  求字符串匹配长度

hdu G++ 提交

#include "bits/stdc++.h"

可以
#include "bits/stdc++.h"
using namespace std; const int maxn=; int next1[maxn];
char s1[maxn],s2[maxn]; void getnext(char *t){
int len=strlen(t);
int i=,j=-;
next1[]=-;
while(i<len){
if(j==- || t[i]==t[j]){
i++;j++;
next1[i]=j;
}else
j=next1[j];
}
} int KMP(char *s,char *t){
int len1=strlen(s),len2=strlen(t);
int i=,j=;
getnext(t);
while(i<len1 && j<len2){
if(j==- || s[i]==t[j]){
i++;j++;
}else
j=next1[j];
}
if(i==len1)
return j;
return ;
}
///KMP返回的就是那个公共子串的长度
int main(){ //freopen("input.txt","r",stdin);
while(~scanf("%s%s",s1,s2)){
int x=KMP(s1,s2);///s1模式串
int y=KMP(s2,s1);
if(x==y){///相等取字典序小的
if(strcmp(s1,s2)<)
printf("%s%s\n",s1,s2+x);
else
printf("%s%s\n",s2,s1+x);
}else if(x>y)///取合并后短的
printf("%s%s\n",s1,s2+x);
else
printf("%s%s\n",s2,s1+y);
}
return ;
}

HDU 1867 A + B for you again ----KMP的更多相关文章

  1. HDU 1867 A + B for you again KMP解决问题的方法

    这是一个典型问题KMP申请书. 结果求增加两个字符串.该法的总和是相同的前缀和后缀也是字符串的字符串,您将可以合并本节. 但是,这个问题是不是问题非常明确的含义,因为不是太清楚,外观这两个字符串的顺序 ...

  2. HDU 1711 Number Sequence (字符串匹配,KMP算法)

    HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...

  3. HDU 2087 HDU 1867 KMP标准模板题

    贴两道题,其中HDU2087是中文题,故不解释题目, 思路是,一发KMP,但是特别处理最后一位的失配边为0,这样就可以保证“判断完成但是不多判断”. 第二题,很毒瘤的题,要求求出,给定字符串A,B能够 ...

  4. A + B for you again HDU - 1867(最大前缀&最大后缀的公共子缀&kmp删除法)

    Problem Description Generally speaking, there are a lot of problems about strings processing. Now yo ...

  5. Hdu 1867 KMP

    题目链接 题目意思: 给出两个字符串a, b, 求最长的公共字串c, c是a的后缀,也是b的前缀. 本题没有具体说明哪个字符串是文本串和匹配串, 所以都要考虑 思路: 查找的时候, 当文本串结束的时候 ...

  6. hdu 1867 求两个串的"和"最小 ,KMP

    题意:       给你两个字符串,让你求str1+str2,就是把1的后面和2的前面重叠的地方只显示一遍就行了 abc + bcd = abcd,要求和的长度最小,和最小的前提下求字典序最小,还有就 ...

  7. 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/Othe ...

  8. hdu 1867 kmp匹配

    #include<stdio.h> #include<string.h> #define N 100100 void getnext(int next[],char s[]) ...

  9. hdu 1867 A + B for you again

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1867 A + B for you again Description Generally speaki ...

随机推荐

  1. [CQOI2007]余数求和 (分块+数学

    题目描述 给出正整数n和k,计算G(n, k)=k mod 1 + k mod 2 + k mod 3 + … + k mod n的值,其中k mod i表示k除以i的余数.例如G(10, 5)=5 ...

  2. MySQL CONCAT()与GROUP_CONCAT()的使用

      1 . MySQL CONCAT(str1,str2, ...)  --返回连接的字符串 mysql> SELECT CONCAT('My', 'S', 'QL'); -> 'MySQ ...

  3. 邮件系统之Postfix与Dovecot

    电子邮件系统 电子邮件系统基于邮件协议来完成电子邮件的传输,常见的邮件协议有: 简单邮件传输协议(Simple Mail Transfer Protocol,SMTP):用于发送和中转发出的电子邮件, ...

  4. Hive数据导入导出的n种方式

    Tutorial-LoadingData Hive加载数据的6种方式 #格式 load data [local] inpath '/op/datas/xxx.txt' [overwrite] into ...

  5. TouTiao开源项目 分析笔记4==>一个简单APP 整体常用框架

    1.效果预览 1.1.如下图所以,到目前为止所有的功能. 2.从InitApp开始->SplashActivity->MainActivity 2.1.InitApp源代码.这是整个项目的 ...

  6. mongo创建数据库和用户

    1.linux安装mongo conf文件配置: 配置文件: dbpath=/home/data/mongodb/mongodb logpath=/home/data/logs/mongodb.log ...

  7. Installing Nginx With PHP5 (And PHP-FPM) And MySQL Support (LEMP) On Ubuntu 12.04 LTS [repost]

    from : http://www.howtoforge.com/installing-nginx-with-php5-and-php-fpm-and-mysql-support-lemp-on-ub ...

  8. USACO Section2.1 Ordered Fractions 解题报告

    frac1解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...

  9. nginx清除反向代理缓存

    nginx重启无法清除反向代理的缓存,可以清空安装目录下的proxy_cache文件夹里的内容来清除.

  10. springboot相关链接

    springboot的三种启动方式 https://blog.csdn.net/my__Sun_/article/details/72866329 springboot学历历程 https://www ...