HDU 1867(kmp应用)
时间:2022-07-28
本文章向大家介绍HDU 1867(kmp应用),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
题意描述
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.
思路
使用两次kmp,分别求出p匹配s和s匹配p的相同前后缀的个数,如果相同,则使用strcpy()来比较两个字符串的字典序大小,否则按照题意输出
AC代码
#include<bits/stdc++.h>
#define x first
#define y second
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long LL;
const int N=2*1e5+10;
const int M=150;
const int INF=0x3f3f3f3f;
const int MOD=998244353;
int n,m,ne[N];
char p[N],s[N];
void get_next(char* str){
int len=strlen(str);
int i=0,j=-1;
ne[0]=-1;
while(i<len){
if(j==-1 || str[i]==str[j]){
i++;
j++;
ne[i]=j;
}else{
j=ne[j];
}
}
}
int get_kmp(char* str1,char* str2){
int len1=strlen(str1);
int len2=strlen(str2);
get_next(str2);
int i=0,j=0;
while(i<len1){
if(j==-1 || str1[i]==str2[j]){
i++;
j++;
}else{
j=ne[j];
}
}
return j;
}
void solve(){
while(scanf("%s%s",s,p)!=EOF){
int k1=get_kmp(p,s);
int k2=get_kmp(s,p);
if(k1>k2){
printf("%s%s",p,s+k1);
}else if(k1<k2){
printf("%s%s",s,p+k2);
}else{
if(strcmp(s,p)>0) printf("%s%s",p,s+k2);
else if(strcmp(s,p)<0) printf("%s%s",s,p+k2);
else printf("%s",p);
}
puts("");
}
}
int main(){
//IOS;
solve();
return 0;
}
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Android studio so库找不到问题解决办法
- Android使用ViewPager实现屏幕滑动效果
- Android 自定义view实现进度条加载效果实例代码
- Android电池电量监听的示例代码
- Android 解决ScrollView嵌套CridView显示问题
- Android利用zxing快速集成二维码扫描的实例教程
- Android中使用SharedPreferences完成记住账号密码的功能
- Android Intent封装的实例详解
- Android自定义Drawable实现圆角效果
- Android ApplicationInfo 应用程序信息的详解
- Android UI控件Switch的使用方法
- Android如何读写CSV文件方法示例
- Android 静默安装和卸载的方法
- Android自定义单例AlertDialog详解
- Android Build类的详解及简单实例