A + B for you again

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9291    Accepted Submission(s): 2274

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以及next数组。。
 #include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(s1) memset(s1,0,sizeof(s1))
#define meminf(s1) memset(s1,0x3f,sizeof(s1))
#define ll long long
using namespace std;
int nex[],l1,l2;
void getn(int n,char c[])
{
int i=,j=-;
nex[]=-;
while(i<n)
{
if(j==-||c[i]==c[j])
{
i++;j++;nex[i]=j;
}
else j=nex[j];
}
return;
}
int kmp(char s1[],char s2[])
{
int i,j=,n,m;
n=strlen(s1);
m=strlen(s2);
getn(m,s2);
for(i=;i<n;i++)
{
while(j&&s1[i]!=s2[j])j=nex[j];
if(s1[i]==s2[j])j++;
}
return j;
}
int main()
{
char s1[],s2[];
wshile(~scanf("%s%s",s1,s2))
{
l1=strlen(s1);l2=strlen(s2);
int h=kmp(s1,s2);
int hh=kmp(s2,s1);
if(h>hh||(h==hh&&strcmp(s1,s2)<)) printf("%s%s\n",s1,s2+h);
else printf("%s%s\n",s2,s1+hh);
}
return ;
}

hdu 1867 A+B again for you的更多相关文章

  1. hdu 1867 A + B for you again

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

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

    题意: 给你两个字符串,输出他们合并之后的字符串,合并的时候把A的后缀和B的前缀重叠合(或者把A的前缀和B的后缀重合).要求合并后的串既包含A右包含B, 且使得合并后的字符串尽量短,其次是使得合并后的 ...

  3. Hdu 1867 KMP

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

  4. HDU 1867 A + B for you again 字符匹配

    解题报告:给你两个字符串,让你连接起来,没有前后顺序,要求是长度最短优先,其次是字典序最小.这题我用的是KMP,做两次匹配,分别把第一次跟第二次输入的字符串放前面,然后比较两次得到的字符窜的长度和字典 ...

  5. 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 ...

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

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

  7. hdu 1867 kmp匹配

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

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

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

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

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

随机推荐

  1. LOL游戏基本代码

    class Hero: def __init__(self, new_nickname, new_aggressivity, new_life_value, new_money, new_armor ...

  2. 第37课 thinkphp5添加商品基本信息及通过前置钩子上传商品主图 模型事件(勾子函数)

    目录 手册地址: before_insert(新增之前的操作) 要实现的功能 思路 触发条件: 1. 控制器里必须要调用模型的save()方式保存数据,用insert()触发不了勾子函数的 2. 模型 ...

  3. c++ 操作符优先级

    优先级 操作符 描述 例子 结合性 1 ()[]->.::++-- 调节优先级的括号操作符数组下标访问操作符通过指向对象的指针访问成员的操作符通过对象本身访问成员的操作符作用域操作符后置自增操作 ...

  4. CDOJ:1636-梦后楼台高锁,酒醒帘幕低垂(Kruskal+暴力)

    梦后楼台高锁,酒醒帘幕低垂 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit ...

  5. 版本控制之GitHub — — 第一步的理解

    GitHub是时下最流行的版本控制的一门“技术”,此之前svn(subversion)也是同样的作用. 至于版本控制:Git是分布式的,而svn是中心式的(或者叫集中式的)版本控制系统,这是两者之间理 ...

  6. Tomcat之web.xml中的<url-pattern>标签

    关于web.xml配置中的<url-pattern> 标签<url-pattern> <url-pattern>是我们用Servlet做Web项目时需要经常配置的标 ...

  7. C#串口扫描枪的简单实现

    原文:C#串口扫描枪的简单实现 串口扫描枪的简单实现 基于串口通讯的扫描枪的实现,主要借助SerialPort类,表示串行端口资源.实现很简单: 工具:usb转RS232转接头/个,扫描枪/套, 扫描 ...

  8. CSU 1326: The contest(分组背包)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1326 题意: n个题目,每个题目都有一个价值Pi和相对能力消耗Wi,但是有些题目因为太坑不能同时做 ...

  9. Thread和Runnable的子类调用

    实现线程的两种方式: 继承Thread类. 实现Runnable接口. 下面是一个小案例: public class Thread和Runnable { public static void main ...

  10. cf984e Elevator

    ref我好菜啊 #include <iostream> #include <cstring> #include <cstdio> #include <cmat ...