当前位置:网站首页>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;
}边栏推荐
- Binlog and iptables prevent nmap scanning, xtrabackup full + incremental backup, and the relationship between redlog and binlog
- Attributeerror: module 'distutils' has no attribute' version error resolution
- C operator priority memory formula
- 老虎口瀑布:铜梁版小壶口瀑布
- Mini examination - examination system
- Fraud detection cases and Titanic rescued cases
- Learning rate adjustment strategy in deep learning (1)
- 【MATLAB】MATLAB画图系列二 1.元胞与数组转化 2.属性元胞 3.删除nan值 4.合并多fig为同一fig 5.合并多fig至同一axes
- Summary of feature selection: filtered, wrapped, embedded
- PCA of [machine learning]
猜你喜欢

Production environment tidb cluster capacity reduction tikv operation steps

IEEE Transaction期刊模板使用注意事项

Number of bytes occupied by variables of type char short int in memory

Detailed explanation of IO model (easy to understand)

Regular expression and bypass cases
![Rasa 3.x learning series -rasa [3.2.4] - 2022-07-21 new release](/img/1e/27f107d514ded6641410cc5a45764b.png)
Rasa 3.x learning series -rasa [3.2.4] - 2022-07-21 new release

Deep learning 1 perceptron and implementation of simple back propagation network

TypeError: Cannot read property ‘make‘ of undefined

Typo in static class property declarationeslint

LeetCode高频题56. 合并区间,将重叠的区间合并为一个区间,包含所有区间
随机推荐
The server switches between different CONDA environments and views various user processes
老虎口瀑布:铜梁版小壶口瀑布
[oauth2] IV. oauth2authorizationrequestredirectfilter
Learning and thinking about the relevant knowledge in the direction of building network security knowledge base
Conversion of timestamp and time in Excel
VSCode如何调试Nodejs
The spiral matrix of the force buckle rotates together (you can understand it)
Learning rate adjustment strategy in deep learning (1)
Attributeerror: module 'distutils' has no attribute' version error resolution
spark:指定日期输出相应日期的日志(入门级-简单实现)
onBlur和onChange冲突解决方法
Binlog and iptables prevent nmap scanning, xtrabackup full + incremental backup, and the relationship between redlog and binlog
看完这篇文章,才发现我的测试用例写的就是垃圾
正则表达和绕过案例
Fraud detection cases and Titanic rescued cases
PrestoUserError: PrestoUserError(type=USER_ERROR, name=INVALID_FUNCTION_ARGUMENT, message=“Escape st
2.4. properties of special profile
Ztree tree Metro style mouse through the display user-defined controls add, edit, delete, down, up operations
Isprs2018/ cloud detection: cloud/shadow detection based on spectral indexes for multi/hyp multi / hyperspectral optical remote sensing imager cloud / shadow detection
Conflict resolution of onblur and onchange