其他分享
首页 > 其他分享> > 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;
}

 

标签:Pun,return,cout,int,Who,else,AtcoderABC,false,check
来源: https://www.cnblogs.com/hanasaki/p/11563658.html