求助 TLE了
查看原帖
求助 TLE了
757864
ruye楼主2024/3/30 11:49
int n;
string s;
vector<int> get_divisors(int x) {
    vector<int> res;
    for (int i = 1; i <= x / i; i ++ )
        if (x % i == 0) {
            res.push_back(i);
            if (i != x / i) res.push_back(x / i);
        }
    sort(res.begin(), res.end());
    return res;
}

void solve() {
    cin >> n >> s;
    auto a = get_divisors(n);

    for(auto t : a) {// 根号n
        int cnt = 0;
        map<char, int> mp[t];
        for(int i = 0; i < n; i ++ ) mp[i % t][s[i]] ++ ;
        for(int i = 0; i < t; i ++ ) {// n
            if(mp[i].size() != 1) {
                int minn = 3;
                if(mp[i].size() == 2) {
                    for(auto [x, c] : mp[i]) 
                        minn = min(minn, c);
                }
                cnt += minn;
            }
            if(cnt >= 2) break;
        }
        if(cnt <= 1) {
            cout << t << endl;
            return ;
        }
    }
}

void init() {

}


这种该怎么去判断代码的时间复杂度会不会超时 %%

2024/3/30 11:49
加载中...