使用set TLE on #7 求助
查看原帖
使用set TLE on #7 求助
378220
AmaZingFantasy楼主2021/8/14 11:06

RT

已知c++ set插入和查询一个数都要 O(NlogN)O(N \log N) 的复杂度。我的算法也是如此,读入字符串,判断在这个 set 里是否出现过,如果有,就插入,否则一个个枚举st1,st2,st3这样的字符串,如果可以插入,就插入,并输出改编后的字符串。

我即断了同步,也开了o2,咋还t呢

#include <iostream>
#include <set>
using namespace std;
typedef long long l;
set<string>s;
string hecheng(string st,l shu){
    string h;
    while(shu > 0){
        h+=shu%10+48;
        shu/=10;
    }
    return st+h;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    l n;
    cin>>n;
    for(l i=0;i<n;i++){
        string st;
        cin>>st;
        l x=0;
        if(s.count(st)==0){
            cout<<"OK\n";
            s.insert(st);
        }else{
            while(s.count(hecheng(st,x)) == 1){
                x++;
            }
            string k=hecheng(st,x);
            cout<<k<<"\n";
            s.insert(k);
        }
    }
    return 0;
}```
求大佬指教
2021/8/14 11:06
加载中...