蒟蒻求教,想法是预处理所有数的约数,预处理时间复杂度应该是 O(nlnn),找合法方案的过程感觉上应该不会重复太多次(感觉可能是这里的锅,但是不会证明这里有问题),自己测了一个全是 a 的数据时间也能过,不知道为什么会 T 一个点,有没有大佬能讲一下原因。
#include<iostream>
#include<vector>
#include<ctime>
using namespace std;
const int N=5e5+10;
int n,q,res;
char s[N];
namespace Hash
{
typedef unsigned long long ull;
const int mod=131;
ull mul[N],h[N];
void init(char s[])
{
mul[0]=1;
for(int i=1;i<=n;i++)
{
mul[i]=mul[i-1]*mod;
h[i]=h[i-1]*mod+s[i];
}
}
ull get(int l,int r)
{
return h[r]-h[l-1]*mul[r-l+1];
}
bool check(int l1,int r1,int l2,int r2)
{
return get(l1,r1)==get(l2,r2);
}
}
vector<int>fac[N];
void init()
{
for(int i=2;i<=n;i++)
for(int j=i;j<=n;j+=i)
fac[j].emplace_back(i);
}
void dfs(int l,int r,int x)
{
if(x==1)return;
for(int t:fac[x])
{
int len=(r-l+1)/t;
if(Hash::check(l,r-len,l+len,r))
{
res=min(res,len);
return dfs(l,l+len-1,len);
}
}
}
int main()
{
// freopen("test.in","r",stdin);
// freopen("test.out","w",stdout);
// double now=clock();
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n>>s+1>>q;
Hash::init(s);
init();
while(q--)
{
int l,r;
cin>>l>>r;
res=r-l+1;
dfs(l,r,res);
cout<<res<<'\n';
}
// cout<<(clock()-now)/CLOCKS_PER_SEC;
return 0;
}