当前位置:网站首页>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;
}边栏推荐
- mysql
- Bibliometrix: dig out the one worth reading from thousands of papers!
- [oauth2] III. interpretation of oauth2 configuration
- DDD based on ABP -- Entity creation and update
- Ztree tree Metro style mouse through the display user-defined controls add, edit, delete, down, up operations
- The vs compiled application is missing DLL
- Error when using Fiddler hook: 502 Fiddler - connection failed
- Centos7 installs Damon stand-alone database
- Clear all spaces in the string
- Must use destructuring props assignmenteslint
猜你喜欢

深入浅出边缘云 | 2. 架构
![[oauth2] II. Authorization method of oauth2](/img/9f/0098394a341a9dfb0cf8a862f46049.png)
[oauth2] II. Authorization method of oauth2

Kotlin class and inheritance

Deep learning 1 perceptron and implementation of simple back propagation network

Bibliometrix: dig out the one worth reading from thousands of papers!

看完这篇文章,才发现我的测试用例写的就是垃圾

Class loading mechanism and parental delegation mechanism

老虎口瀑布:铜梁版小壶口瀑布

2022 IAA industry category development insight series report - phase II

Spark: get the access volume of each time period in the log (entry level - simple implementation)
随机推荐
Centos7 installs Damon stand-alone database
记不住正则表达式?这里我整理了99个常用正则
Spark Learning Notes (III) -- basic knowledge of spark core
Detailed explanation of address bus, data bus and control bus
茅台冰淇淋“逆势”走红,跨界之意却并不在“卖雪糕”
Decrypt "sea Lotus" organization (domain control detection and defense)
Moving the mouse into select options will trigger the mouseleave event processing scheme
How to set packet capturing mobile terminal
DDD based on ABP -- Entity creation and update
Atcoder beginer contest 261e / / bitwise thinking + DP
After reading this article, I found that my test cases were written in garbage
[oauth2] II. Known changes in oauth2.1
使用 Fiddler Hook 报错:502 Fiddler - Connection Failed
Clear all spaces in the string
Mysql库的操作
Differences between C language pointer and array A and &a, &a[0], etc
Complete set of JS array common operations
Production environment tidb cluster capacity reduction tikv operation steps
电赛设计报告模板及历年资源
PrestoUserError: PrestoUserError(type=USER_ERROR, name=INVALID_FUNCTION_ARGUMENT, message=“Escape st