当前位置:网站首页>串应用- 计算一个串的最长的真前后缀
串应用- 计算一个串的最长的真前后缀
2022-07-21 16:26:00 【奔跑的星黛露】
题目描述
给定一个串,如ABCDAB,则ABCDAB的真前缀有:{ A, AB,ABC, ABCD, ABCDA }ABCDAB的真后缀有:{ B, AB,DAB, CDAB, BCDAB } 因此,该串的真前缀和真后缀中最长的相等串为AB,我们称之为该串的“最长的真前后缀”。试实现一个函数string matched_Prefix_Postfix(string str),得到输入串str的最长的真前后缀。若不存在最长的真前后缀则输出empty
输入
第1行:串的个数 n第2行到第n+1行:n个字符串
输出
n个最长的真前后缀,若不存在最长的真前后缀则输出empty。
样例输入
6
a
ab
abc
abcd
abcda
abcdab
样例输出
empty
empty
empty
empty
a
ab
代码
#include "bits/stdc++.h"
using namespace std;
const int maxn=1e5+20;
int t,n,nex[maxn];
string mains,s,ress;
void getnext(){
int i=0,j=-1,len=s.length();
nex[0]=-1;
while(i<len){
if(j==-1 || s[i]==s[j]){
nex[++i]=++j;
}
else j=nex[j];
}
}
int find(){
int i,j;
int l1=mains.length(),l2=s.length();
for(i=0,j=0;i<l1 && j<l2;){
if(j==-1 || mains[i]==s[j]){
i++,j++;
}
else j=nex[j];
}
if(j==l2){
return i-j+1;
}
return -1;
}
int main(){
// freopen("123.in","r",stdin);
cin>>t;
while(t--){
cin>>s;
memset(nex,0,sizeof nex);
getnext();
int cnt=nex[s.length()];
if(cnt==0 || cnt==-1) puts("empty");
else{
string ans=s.substr(0,cnt);
cout<<ans<<endl;
}
}
return 0;
}
边栏推荐
- Multithreading foundation of concurrent programming
- Outlook 教程,如何在 Outlook 中创建任务和待办事项?
- 智能运维场景解析:如何通过异常检测发现业务系统状态异常
- 怎样同构+跨端,懂得小程序+kbone+finclip就够了!
- CString转int
- 问题来了!拔掉网线几秒,再插回去,原本的 TCP 连接还存在吗?
- Programming in CoDeSys to realize serial communication
- IP协议——网段划分
- IRP结构的MdlAddress,UserBuffer,SystemBuffer三种内存的区别
- Tiktok web reverse tutorial
猜你喜欢
lombok的注解@Accessors
web3再牛 也没能逃出这几个老巨头的手掌心
Outlook 教程,如何在 Outlook 中设置规则?
Let me show you eight fallacies in software design
uniapp开发的微信小程序如何上传至微信小程序平台-完整简单步骤
Mutual certification of product compatibility between tapdata and Youxuan database
tsconfig. JSON cannot find any input in the configuration file. What should I do?
Dynamics crm: precautions for batch importing data to update records
How to use document tools for API management?
How to change the console font to console?
随机推荐
ROS机械臂 Movelt 学习笔记1 | 基础准备
Wechat payment native (I) preparation and related knowledge
How to change the console font to console?
编程学习的资料分享
数据库学习 – select(多表联查)[通俗易懂]
The relevant person in charge of the state Internet Information Office answered reporters' questions on the decision to impose administrative penalties related to network security review on didi Globa
Dynamics crm: deeply analyze the impact of sandbox and asynchronous services on plug-in and workflow in on premise server
微信支付Native(一)准备和相关知识
跟我读论文丨Multi-Model Text Recognition Network
单片机入门:LED灯循环左移点亮
電腦上如何開啟多個微信,微信多開
着手社区建设掌握的两个概念
wallys/new product/DR7915/MT7915+MT7975/WiFi6 MiniPCIe Module 2T2R
Introduction to 51 single chip microcomputer: LED lights flash at different frequencies
文件上传下载与Excel、数据表数据之间的转换
pytorch 32 onnx模型每次输出的结果都会变的解决方案
开发动态 | StoneDB 2022年版本发布里程碑
使用OpenCv+Arduino实现挂机自动打怪
20天能拿下PMP吗?
Multithreading and concurrent programming (3)