当前位置:网站首页>String application - calculate the longest true prefix of a string
String application - calculate the longest true prefix of a string
2022-07-24 14:47:00 【Running star dailu】
Title Description
Given a string , Such as ABCDAB, be ABCDAB The true prefix of has :{ A, AB,ABC, ABCD, ABCDA }ABCDAB The true suffix of has :{ B, AB,DAB, CDAB, BCDAB } therefore , The longest equivalent string between the true prefix and the true suffix of this string is AB, We call it the string “ The longest true prefix ”. Try to implement a function string matched_Prefix_Postfix(string str), Get the input string str The longest true prefix of . If there is no longest true prefix, output empty
Input
The first 1 That's ok : The number of strings n The first 2 Go to the first place n+1 That's ok :n A string
Output
n The longest true prefix , If there is no longest true prefix, output empty.
The sample input
6
a
ab
abc
abcd
abcda
abcdabSample output
empty
empty
empty
empty
a
abCode
#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;
}边栏推荐
- Leetcode high frequency question 56. merge intervals, merge overlapping intervals into one interval, including all intervals
- PrestoUserError: PrestoUserError(type=USER_ERROR, name=INVALID_FUNCTION_ARGUMENT, message=“Escape st
- Learning rate adjustment strategy in deep learning (1)
- 小熊派 课程导读
- Spark: get the access volume of each time period in the log (entry level - simple implementation)
- Su Chunyuan, founder of science and technology · CEO of Guanyuan data: making business use is the key to the Bi industry to push down the wall of penetration
- The vs compiled application is missing DLL
- Caffe framework and production data source for deep learning
- Research Summary / programming FAQs
- Which brokerage has the lowest commission? I want to open an account. Is it safe to open an account on my mobile phone
猜你喜欢

茅台冰淇淋“逆势”走红,跨界之意却并不在“卖雪糕”

Meaning of 7 parameters of thread pool

REST风格

Dameng real-time active and standby cluster construction

VSCode如何调试Nodejs

Must use destructuring props assignmenteslint

Su Chunyuan, founder of science and technology · CEO of Guanyuan data: making business use is the key to the Bi industry to push down the wall of penetration

Isprs2018/ cloud detection: cloud/shadow detection based on spectral indexes for multi/hyp multi / hyperspectral optical remote sensing imager cloud / shadow detection
![Rasa 3.x 学习系列-Rasa [3.2.3] - 2022-07-18 新版本发布](/img/fd/c7bff1ce199e8b600761d77828c674.png)
Rasa 3.x 学习系列-Rasa [3.2.3] - 2022-07-18 新版本发布

使用 Fiddler Hook 报错:502 Fiddler - Connection Failed
随机推荐
Grpc middleware implements grpc call retry
spark:获取日志中每个时间段的访问量(入门级-简单实现)
Rasa 3.x learning series -rasa fallbackclassifier source code learning notes
[oauth2] IV. oauth2authorizationrequestredirectfilter
pytorch with torch.no_ grad
pip换源
The vs compiled application is missing DLL
Caffe framework and production data source for deep learning
threw exception [Circular view path [index]: would dispatch back to the current handler URL [/index]
Explain the edge cloud in simple terms | 2. architecture
Is it safe for Huatai Securities to open an account? Can it be handled on the mobile phone?
Decrypt "sea Lotus" organization (domain control detection and defense)
CSDN垃圾的没有底线!
Native asynchronous network communication executes faster than synchronous instructions
Property datasource is required exception handling [idea]
PrestoUserError: PrestoUserError(type=USER_ERROR, name=INVALID_FUNCTION_ARGUMENT, message=“Escape st
Binlog and iptables prevent nmap scanning, xtrabackup full + incremental backup, and the relationship between redlog and binlog
DDD based on ABP -- Entity creation and update
dataframe 分组后排序的前n行 nlargest argmax idmax tail !!!!
AtCoder Beginner Contest 261E // 按位思考 + dp