AtcoderABC-141E-Who Says a Pun?

AtcoderABC-141E-Who Says a Pun?

 

 AtcoderABC-141E-Who Says a Pun?

 

可以用二分查找因为f(x)(长度为x的双等子串是否存在)具有单调性。

#include<bits/stdc++.h>
using namespace std;

int n;
string s;

bool check(int x) {
    map<string, int> v;
    for(int i = 1; i <= n + 1 - x; i++) {
        string a = s.substr(i - 1, x);
        if(v[a]) {
            if(i - v[a] >= x) return true;
        }
        else v[a] = i;
    }
    return false;
}

int main() {
    cin >> n >> s;
    int l = 0, r = n / 2;
    while(l <= r) {
        int m = (l + r) >> 1;
        if(check(m)) l = m + 1;
        else r = m - 1;
    }
    cout << r << endl;
}

 

上一篇:Who Says a Pun?


下一篇:VCSA 6.5无法访问,报错“503 Service Unavailable”的解决方法