QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#177045#5084. Longest Substringnathanlee726WA 7ms19340kbC++173.0kb2023-09-12 14:56:592023-09-12 14:57:00

Judging History

你现在查看的是最新测评结果

  • [2023-09-12 14:57:00]
  • 评测
  • 测评结果:WA
  • 用时:7ms
  • 内存:19340kb
  • [2023-09-12 14:56:59]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define rep(i, l, n) for(int i = l; i < n; i++)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
#define endl '\n'

#ifdef LOCAL
#define debug(args...) LKJ("\033[1;32m["#args"]\033[0m", args)
template<class I> void LKJ(I&&x) { cerr << x << endl; }
template<class I, class ...T> void LKJ(I&&x, T&&...t)
{ cerr << x << ", ", LKJ(t...);}
#else 
#define debug(...) ((void)0)
#endif

struct SAM {
	const int P = 50000;
	vector<map<char, int>> ch;
	vector<int> len, link;
	int sz, last;
	void init_() { 
		sz = 1, last = 0;
		ch.assign(P * 2, map<char, int>());
		len.assign(P * 2, 0);
		link.assign(P * 2, -1);
	}
	void extend(char c) {
		int cur = sz ++;
		len[cur] = len[last] + 1;
		int p = last;
		while(p != -1 && !ch[p].count(c)) {
			ch[p][c] = cur;
			p = link[p];
		}
		if(p == -1) link[cur] = 0;
		else {
			int q = ch[p][c];
			if(len[p] + 1 == len[q]) link[cur] = q;
			else {
				int cl = sz ++;
				ch[cl] = ch[q];
				len[cl] = len[p] + 1;
				link[cl] = link[q];
				while(p != -1 && ch[p].count(c) && ch[p][c] == q) {
					ch[p][c] = cl, p = link[p];
				}
				link[q] = link[cur] = cl;
			}
		}
		last = cur;
	}
}sam;

int in[100010];
pii ans[5005];
set<int> endpoints[100010];

int main(){
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);
    string s;
    cin >> s;
    int n = sz(s);
    sam.init_();
    int cc = 0;
    for(char c: s) sam.extend(c), endpoints[sam.last].insert(++cc);
    rep(i, 1, sam.sz){
        in[sam.link[i]]++;
    }
    queue<int> q;
    rep(i, 0, sam.sz){
        debug(sam.link[i], sam.len[i]);
        if(in[i] == 0) q.push(i);
    }
    while(!q.empty()){ 
        int p = q.front();q.pop();
        if(p == 0) continue;
        debug(p);
        int cnt = 0, now = 0;
        auto ptr = endpoints[p].lower_bound(now + sam.len[p]);
        while(ptr != endpoints[p].end()) {
            debug(now + sam.len[p], *ptr);
            now = *ptr;
            debug(now);
            cnt++;
            ptr = endpoints[p].lower_bound(now + sam.len[p]);
        }
        if(ans[sz(endpoints[p])].first < cnt) ans[sz(endpoints[p])].first = cnt, ans[sz(endpoints[p])].second = sam.len[p];
        else if(ans[sz(endpoints[p])].first == cnt) ans[sz(endpoints[p])].second = max(ans[sz(endpoints[p])].second, sam.len[p]);
        //ans[cnt] = max(ans[cnt], sam.len[p]);
        if(sz(endpoints[sam.link[p]]) == 0){
            endpoints[sam.link[p]] = endpoints[p];
        }
        else{
            if(sz(endpoints[sam.link[p]]) < sz(endpoints[p])) swap(endpoints[sam.link[p]], endpoints[p]);
            for(int i: endpoints[p]) endpoints[sam.link[p]].insert(i);
        }
        debug(sam.link[p], in[sam.link[p]]);
        in[sam.link[p]]--;
        if(in[sam.link[p]] == 0) q.push(sam.link[p]);
    }
    rep(i, 1, n + 1) cout << ans[i].second << " \n"[i == n];
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 13168kb

input:

ababa

output:

5 2 1 0 0

result:

ok single line: '5 2 1 0 0'

Test #2:

score: 0
Accepted
time: 0ms
memory: 13396kb

input:

aaaaaaaa

output:

8 7 6 5 4 3 2 1

result:

ok single line: '8 7 6 5 4 3 2 1'

Test #3:

score: 0
Accepted
time: 2ms
memory: 13400kb

input:

a

output:

1

result:

ok single line: '1'

Test #4:

score: 0
Accepted
time: 0ms
memory: 13572kb

input:

abcdefghijklmnopqrstuvwxyz

output:

26 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

result:

ok single line: '26 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'

Test #5:

score: -100
Wrong Answer
time: 7ms
memory: 19340kb

input:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

50000 49999 49998 49997 49996 49995 49994 49993 49992 49991 49990 49989 49988 49987 49986 49985 49984 49983 49982 49981 49980 49979 49978 49977 49976 49975 49974 49973 49972 49971 49970 49969 49968 49967 49966 49965 49964 49963 49962 49961 49960 49959 49958 49957 49956 49955 49954 49953 49952 49951 ...

result:

wrong answer 1st lines differ - expected: '50000 49999 49998 49997 49996 ...4 13 12 11 10 9 8 7 6 5 4 3 2 1', found: '50000 49999 49998 49997 49996 ...0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'