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 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
题意讲解: 给你两个字符串 s1,s2让你把它们,连接起来前后相同的重合一起,不再输出;
连接规则:
         假设     s1 + s2  能匹配的长度为 len1 ;
                  s2 + s1 能匹配的长度为  len2 ;
如果 len1 = len2 判断谁在前就要看谁得字典序小了,当然小的在前
如果 len1 > len2   输出匹配后的字符串 s1+s2
如果 len1 < len2   输出匹配后的字符串 s2+s1
 
所以AC代码如下:略长
 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<string>
#include<cmath>
using namespace std;
const int N = 1e5+;
char s1[N],s2[N];
int next[][N],len1,len2;
int x1,x2;
void solve1(int len1)//寻找第一个字符串的next数组
{
int i = ;
int j = -;
next[][] = -;
while(i<len1)
{
if(j== - || s1[i] == s1[j])
{
++i;
++j;
next[][i] = j;
}
else
{
j = next[][j];
}
}
}
void solve2(int len2)//寻找第二个字符串的next数组
{
int i = ;
int j = -;
next[][] = -;
while(i<len2)
{
if(j== - || s2[i] == s2[j])
{
++i;
++j;
next[][i] = j;
}
else
{
j = next[][j];
}
}
}
int solve(char *s3,char *s4,int len,int x) //xx:表示字符串s3和s4匹配时,是从s3的第xx个开始匹配的
{
int j=,i=;
int xx=;
while(i<len)
{
if(j==- || s3[i] == s4[j])
{
i++;
j++;
}
else
{
j = next[x][j];
xx=i-j;
}
}
return xx;
}
int main()
{
while(~scanf("%s %s",s1,s2))
{
memset(next,-,sizeof(next));
len1 = strlen(s1);
len2 = strlen(s2);
solve1(len1);
solve2(len2);
int x1 = solve(s1,s2,len1,);
int x2 = solve(s2,s1,len2,);
//判断能匹配字符串的长度
int xx1 = len1 - x1;
int xx2 = len2 - x2;
//当s1在前或者s2在前连接的字符串总长度是相同的,则要按照字典序小的在前,
//例如:s1:abcefg s2:efgabc 都能匹配对方三个,所以要按照字典序abcefg 在前;
if(xx1 == xx2)
{
if(strcmp(s1,s2)<)
{
for(int i=; i<x1; i++)
printf("%c",s1[i]);
printf("%s\n",s2);
}
else
{
for(int i=; i<x2; i++)
printf("%c",s2[i]);
printf("%s\n",s1);
}
}
//接下来就看,谁能匹配谁的多了,xx1 表示s2匹配s1 的长度,xx2表示 s1 匹配 s2的长度;
//例如s1: abcdef s2: hjabcd ;这时s2,在前先输出;反之s1在前;
else if(xx1 > xx2)
{
for(int i=; i<x1; i++)
printf("%c",s1[i]);
printf("%s\n",s2); }
else
{
for(int i=; i<x2; i++)
printf("%c",s2[i]);
printf("%s\n",s1);
}
}
return ;
}

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

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

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

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

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

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

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

  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 1711 KMP算法模板题

    题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串 ...

  6. hdu 4468 spy 极其精彩的一道kmp灵活运用题

    出的超级好的一道题.至于好在哪里,请思考题目: 题意抽象出来为给定一个字符串r,找出它的一个最短后缀s,使得这个r可以被 s的某前缀+s的某前缀+......+s的某前缀+s本身构造出来. 具体题目描 ...

  7. HDU 3613 Best Reward(拓展KMP算法求解)

    题目链接: https://cn.vjudge.net/problem/HDU-3613 After an uphill battle, General Li won a great victory. ...

  8. HDU 1711 Number Sequence (字符串处理 KMP)

    题目链接 Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...

  9. hdu 1358:Period(KMP算法,next[]数组的使用)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  10. hdu 3336:Count the string(数据结构,串,KMP算法)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. android非法字符的判定、表情符号的判定

    public class EmojiEditText extends EditText {// 输入表情前的光标位置private int cursorPos; // 输入表情前EditText中的文 ...

  2. 关于单例模式的N种实现方式

    在开发中经常用到单例模式,单例模式也算是设计模式中最容易理解,也是最容易手写代码的模式,所以也常作为面试题来考.所以想总结一下单例模式的理论知识,方便同学们面试使用. 单例模式实现的方式只有两种类型, ...

  3. cmake处理多源文件目录的方法

    cmake处理源代码分布在不同目录中的情况也很简单,现在假设我们的源代码分布情况如下: 源代码的分布情况 其中src目录下的文件要编译成一个链接库 第一步,项目主目录中的CMakelist.txt 在 ...

  4. python遍历目录的方法 walk listdir

    这篇文章里描述了这些情况: https://www.cnblogs.com/jiaxin359/p/7324077.html 不用递归的时候,用 listdir 需要递归的时候,用walk

  5. C# 调用 Web Service 时出现 : 407 Proxy Authentication Required错误的解决办法

    // 记得 using System.Net; System.Net.WebProxy myProxy = new System.Net.WebProxy("localhost:9099&q ...

  6. LVS+keepalived+nginx

    LVS是Linux Virtual Server的简写,基于4层协议不处理,不响应,只转发,速度更快 wget -c http://www.linuxvirtualserver.org/softwar ...

  7. C# this.Hide()

    C# this.Hide() 第一次用的时候是在_Load函数里: BookSystem bs = new BookSystem();             bs.ShowDialog();     ...

  8. 畅通project(杭电1232)

    畅通project Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  9. 集合系列之fail-fast 与fail-safe 区别

    一:快速失败(fail—fast) 在用迭代器遍历一个集合对象时,如果遍历过程中对集合对象的内容进行了修改(增加.删除.修改),则会抛出Concurrent Modification Exceptio ...

  10. 常见的web前端性能优化

    一. 语义化HTML:语义化HTML的好处是可以使代码简洁清晰.支持不同设备.利于搜索引擎.便于团队开发: 减少DOM节点:加速页面渲染: 给图片加上正确的宽高值:这可以减少页面重绘,同时防止图片缩放 ...