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
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.
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.
#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算法的应用)的更多相关文章
- HDU 1711 Number Sequence (字符串匹配,KMP算法)
HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...
- HDU 1867 A + B for you again ----KMP
题意: 给你两个字符串,输出他们合并之后的字符串,合并的时候把A的后缀和B的前缀重叠合(或者把A的前缀和B的后缀重合).要求合并后的串既包含A右包含B, 且使得合并后的字符串尽量短,其次是使得合并后的 ...
- HDU 1867 A + B for you again KMP解决问题的方法
这是一个典型问题KMP申请书. 结果求增加两个字符串.该法的总和是相同的前缀和后缀也是字符串的字符串,您将可以合并本节. 但是,这个问题是不是问题非常明确的含义,因为不是太清楚,外观这两个字符串的顺序 ...
- A + B for you again HDU - 1867(最大前缀&最大后缀的公共子缀&kmp删除法)
Problem Description Generally speaking, there are a lot of problems about strings processing. Now yo ...
- hdu 1711 KMP算法模板题
题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串 ...
- hdu 4468 spy 极其精彩的一道kmp灵活运用题
出的超级好的一道题.至于好在哪里,请思考题目: 题意抽象出来为给定一个字符串r,找出它的一个最短后缀s,使得这个r可以被 s的某前缀+s的某前缀+......+s的某前缀+s本身构造出来. 具体题目描 ...
- HDU 3613 Best Reward(拓展KMP算法求解)
题目链接: https://cn.vjudge.net/problem/HDU-3613 After an uphill battle, General Li won a great victory. ...
- HDU 1711 Number Sequence (字符串处理 KMP)
题目链接 Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...
- hdu 1358:Period(KMP算法,next[]数组的使用)
Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- hdu 3336:Count the string(数据结构,串,KMP算法)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- ImageWriter制作ubuntu的U盘启动盘
转自:http://my.oschina.net/f839903061/blog/197935?p={{currentPage+1}} 1.工具从ubuntn中文网中下载指定软件:ImageWrite ...
- UITabBarController 和 UINavigationController 的详解
首先得搞清这两个控制器之间的层级关系,我们直接看官网给的图,如下所示: 从这张图可以看到:最右边的Assembled views是呈现给用户的界面,它左边的Window是最底层的窗口,重点来了,再往左 ...
- 在ios程序中自己主动滚动TableView到某行的方法
比方tableview窗体能够显示 30 行, 我想在填充tableview 100 条数据后 选择第 50行, 能把这一行显示到窗体内, 就像手动拖滚动栏到 第 50行一样,要怎样实现呢? ] an ...
- [转]MSSQL多列取最大或者最小值
本文转自:http://blog.csdn.net/wufeng4552/article/details/4681510 /* lvl1 lvl2 lvl3 lvl4 lvl 4 3 4 1 3 2 ...
- python学习:基础概念
Python 包管理工具解惑 python packaging 一.困惑 作为一个 Python 初学者,我在包管理上感到相当疑惑(嗯,是困惑).主要表现在下面几个方面: 这几个包管理工具有什么不同? ...
- vmware虚拟机 C硬盘空间 无损扩容 新测
摘自: http://hi.baidu.com/y276827893/item/78a351f427726549932af214 其实上面一步的话, 虚拟机设置 里选择磁盘,实用工具里也有这个功能的. ...
- 配置thinkphp3.2 404页面
ThinkPHP自身提供了 404 页面的处理机制,我们只需要在控制器 中添加一个 EmptyController.class.php,并且实现以下方法即可,方法如下: <? class Em ...
- 【云计算】OpenStack项目全面介绍
关于OpenStack孵化项目trove(DBaaS)之我见:http://blog.csdn.net/ddl007/article/details/17253751 OpenStack Trove将 ...
- java中的序列化和反序列化学习笔记
须要序列化的Person类: package cn.itcast_07; import java.io.Serializable; /* * NotSerializableException:未序列化 ...
- Office办公 如何打印顺丰发票
1 关注顺丰速递,我-我的钱包 2 我的钱包-发票申请 3 勾选要打印发票的项目,点击申请发票 4 点击提交,确认发送的邮箱(他是把PDF发到指定邮箱) 最后PDF效果如下 ...