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) ...
随机推荐
- 实现自动文本摘要(python,java)
参考资料:http://www.ruanyifeng.com/blog/2013/03/automatic_summarization.html http://joshbohde.com/blog/d ...
- scrapy 抓取数据被禁止的解决方法
在用抓取头条新闻的数据时出现以下问题:禁止抓取,结果数据没有出来 后来经过查询得知需要把settings.py里面 修改为ROBOTSTXT_OBEY = False就可以了, 默认True
- 工程web-inf 下文件,路径访问
直接用相对路径../即可 效果:
- Android 使用 Gradle 多渠道打包
安卓开发完毕.对于一个开放应用而言,我们须要公布到不同的应用市场,同一时候我们也须要统计不同市场的用户下载量. (通过启动应用后获取不同市场apk中的不同值来区分) 以下用一个详细的实例来说明: 1. ...
- 说说C#之父——安德斯·海尔斯伯格
安德斯·海尔斯伯格(Anders Hejlsberg,1960.12~),丹麦人,Turbo Pascal编译器的主要作者,Delphi和.NET之父! 看到照片的那一刹那儿,我就觉得帅爆了,53岁的 ...
- ftp上传下载至网站
完整的命令行模式解析! 1. 首先open 域名(Ip)形式即可 实例: open 60.205.45.115 2.后面输入用户名(主机名): bxw2713600302 3.输入密码:密码默认显示不 ...
- POJ 2976 Dropping tests (最大化平均值)
题目链接:click here~~ [题目大意]给你n个分数的值,要求最小不选k个,使得最后分数相加结果平均值最大 [解题思路]:最大化平均值:參见:click here~~ 代码: #include ...
- QueryRunner
在相继学习了JDBC和数据库操作之后,我们明显感到编写JDBC代码并非一件轻松的事儿.为了帮助我们更高效的学习工作,从JDBC的繁重代码中解脱出来,xx给我们详尽介绍了一个简化JDBC操作的组件——D ...
- 图片转成base64, base64转成图片
1.我们在看代码时经常在img或css背景图片中看到: src=”data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHgAAAB4CAMAAAAOusbgA ...
- mount挂载WINDOWS分区和目录
转自:http://blog.163.com/sg_liao/blog/static/29577083200942811445981/ 一,挂载共享目录 sudo mount -t cifs -o ...