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) ...
随机推荐
- JSON-学习总结
JSON 语法规则 1. JSON 语法是 JavaScript 对象表示法语法的子集. 数据在名称/值对中 数据由逗号分隔 花括号保存对象 方括号保存数组 2. JSON 值可以是: 数字(整数或浮 ...
- Mysql数据库配置文件my.cnf详解
basedir = path 使用给定目录作为根目录(安装目录). character-sets-dir = path 给出存放着字符集的目录. datadir = path 从给定目录读取数据库文件 ...
- Spring框架学习(4)spring整合hibernate
内容源自:spring整合hibernate spring整合注解形式的hibernate 这里和上一部分学习一样用了模板模式, 将hibernate开发流程封装在ORM层提供的模板类Hiber ...
- [转]php中实现事件驱动
原文: https://blog.csdn.net/yhl27/article/details/8705313 -------------------------------------------- ...
- 【转】dependency injection 的例子
Dependency Injection in PHP. Create your own DI container. / blog / PHP By my opinion one of the big ...
- Lidgren.Network – an introduction to networking in C# games
Lidgren.Network – an introduction to networking in C# games http://genericgamedev.com/tutorials/lidg ...
- hdu5293(2015多校1)--Tree chain problem(树状dp)
Tree chain problem Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- 超酷的实时颜色数据跟踪javascript类库 - Tracking.js
来源:GBin1.com 今天介绍这款超棒的Javascript类库是 - Tracking.js,它能够独立不依赖第三方类库帮助开发人员动态跟踪摄像头输出相关数据. 这些数据包括了颜色或者是人, 这 ...
- sql2012简体中文版安装
sql2012简体中文版安装 导航 介绍 安装 先决条件 装.NET3.5 关闭Windows防火墙 运行Setup.exe 安装程序支持规则 产品密钥 许可条款 产品更新 安装安装程序文件 安装程序 ...
- JMeter 十四:最佳实践
参考:http://jmeter.apache.org/usermanual/best-practices.html 1. 总是使用最新版本的JMeter 2. 使用合适数目的Thread Threa ...