LA 2755 Hidden Password(字符串最小表示法)
参考论文如何寻找字符串的最小表示法
include<cstdio>
include<cstring>
include<iostream>
include<algorithm>
include<queue>
using namespace std;
const int maxn=200000+100;
int T;
int L;
char str[maxn],strtmp[maxn];
void solve();
int main()
{
scanf("%d",&T);
while(T--){
solve();
}
return 0;
}
void solve()
{
// int n=L;
scanf("%d",&L);
scanf("%s",str);
strcpy(strtmp,str);
strcat(str,strtmp);
int i,j;
i=0;j=1;
int k=0;
while(i<L&&j<L){
while(k<L&&str[i+k]==str[j+k]){
k++;
}
if(k==L) break;
if(str[i+k]<str[j+k]){
j=j+k+1;
}
else{
i=max(i+k+1,j);
j=i+1;
}
k=0;
}
printf("%d\n",i);
}
本文介绍了一种用于寻找字符串最小表示法的算法实现。通过将输入字符串复制并拼接自身,利用双指针技巧比较字符,从而找到该字符串的最小循环节。此算法适用于竞赛编程及字符串处理任务。
&spm=1001.2101.3001.5002&articleId=77508252&d=1&t=3&u=195124e6ea844e859d0437a67ff8cfdf)
458

被折叠的 条评论
为什么被折叠?



