G1 n log n被卡常求助.TLE on #17
  • 板块学术版
  • 楼主Xiphi
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/5/3 01:07
  • 上次更新2024/5/3 10:45:44
查看原帖
G1 n log n被卡常求助.TLE on #17
667250
Xiphi楼主2024/5/3 01:07
#include<bits/stdc++.h>
#define _for(i,x,y) for(int i=x;i<=y;++i)
#define _forp(i,x,y,z) for(int i=x;i<=y;i+=z)
#define _rep(i,x,y) for(int i=x;i<y;++i)
using namespace std;
int T,n,l,r;
string s;
string t;
string pattern,text;
vector<int> getNext() {
  int m = pattern.length();
  vector<int> next(m, 0);
  int j = 0;
  for (int i = 1; i < m; i++) {
    while (j > 0 && pattern[i] != pattern[j]) {
      j = next[j - 1];
    }
    if (pattern[i] == pattern[j]) {
      j++;
    }
    next[i] = j;
  }
  return next;
}

bool check(int x){
	pattern.clear();
text=s;
	_for(i,0,x-1){
		pattern=pattern+text[i];
	}
	int N = text.length();
  int m = pattern.length();
  vector<int> Nex = getNext();
  int j = 0;
vector<pair<int,int>> S;
  for (int i = 0; i < N; i++) {
    while (j > 0 && text[i] != pattern[j]) {
      j = Nex[j - 1];
    }
    if (text[i] == pattern[j]) {
      j++;
    }
    if (j == m) {
      S.push_back({i - m + 1,i});
      j = Nex[j - 1];
    }
  }
    pair<int,int> it=*S.begin();
    int cnt=0,r=0;
    while(1){
    	cnt++;
		int xx=(it).first,yy=(it).second;
		pair<int,int> XX={yy+1,0};
		while(r<S.size()&&S[r]<=XX){
			r++;
		}
		it=S[r];
		if(r>=S.size()) break;
	}
	return cnt>=l;
    
}
int main(){
//	freopen("test.in","r",stdin);
//	freopen("test.out","w",stdout);
	cin>>T;
	while(T--){
		cin>>n>>l>>r>>s;
		int l=1,r=n,res=0;
		while(l<=r){
			int mid=l+r>>1;
			if(check(mid)) l=mid+1,res=mid;
			else r=mid-1;
		}
		cout<<res<<'\n';
	}
	return 0;
}




2024/5/3 01:07
加载中...