中石大第39次CSP培训Week1题解

A.文字处理软件


Key:STL-string

AC代码

    #include<bits/stdc++.h>
    #define endl '\n'
    #define int int long long
    #define pb push_back
    #define bs bitset
    #define val(a) (a<'a' ? (a-'A'+'a') : a)
    #define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
    using namespace std;

    typedef pair<char,int> PCI;
    typedef pair<int,int> PII;
    typedef pair<long long, long long> PLL;
    typedef priority_queue<int> PQ;
    typedef priority_queue<int, vector<int>, greater<int>> Q;
    const int N = 2e5+10, MAX = 1e9, INF = -1e9;

    int q;
    string s;
    string t;
    int op;
    int l,r;

    void solve(){
        cin>>q>>s;
        while(q--){
            cin>>op;
            if(op==1){
                cin>>t;
                s+=t;
                cout<<s<<endl;
            }
            else if(op==2){
                cin>>l>>r;
                t=s.substr(l,r);
                s=t;
                cout<<s<<endl;
            }
            else if(op==3){
                cin>>l>>t;
                s.insert(l,t);
                cout<<s<<endl;
            }
            else{
                cin>>t;
                if(s.find(t)>s.size())cout<<-1<<endl;
                else cout<<s.find(t)<<endl;
            }
        }
    }

    signed main()
    {
        fast();
        
        int t=1;
        //cin>>t;
        while(t--){
            solve();
        }
        return 0;
    }

B.字符串哈希


Key:STL-string,STL-map

AC代码

    #include<bits/stdc++.h>
    #define endl '\n'
    #define int int long long
    #define pb push_back
    #define bs bitset
    #define val(a) (a<'a' ? (a-'A'+'a') : a)
    #define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
    using namespace std;

    typedef pair<char,int> PCI;
    typedef pair<int,int> PII;
    typedef pair<long long, long long> PLL;
    typedef priority_queue<int> PQ;
    typedef priority_queue<int, vector<int>, greater<int>> Q;
    const int N = 2e5+10, MAX = 1e9, INF = -1e9;

    int n;
    string s;
    map<string,int> mp;

    void solve(){
        cin>>n;
        while(n--){
            cin>>s;
            mp[s]++;
        }
        cout<<mp.size()<<endl;
        return ;
    }

    signed main()
    {
        fast();
        
        int t=1;
        //cin>>t;
        while(t--){
            solve();
        }
        return 0;
    }

C.Travel


Key:STL-vector

AC代码

    #include<bits/stdc++.h>
    #define endl '\n'
    #define int int long long
    #define pb push_back
    #define bs bitset
    #define val(a) (a<'a' ? (a-'A'+'a') : a)
    #define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
    using namespace std;

    typedef pair<char,int> PCI;
    typedef pair<int,int> PII;
    typedef pair<long long, long long> PLL;
    typedef priority_queue<int> PQ;
    typedef priority_queue<int, vector<int>, greater<int>> Q;
    const int N = 10, MAX = 1e9, INF = -1e9;

    int ans=0;
    int n,k;
    int g[N][N];
    vector<int> v;

    void solve(){
        cin>>n>>k;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                cin>>g[i][j];
            }
        }
        for(int i=1;i<=n;i++){
            v.pb(i);
        }
        do{
            int e=0;
            for(int i=0;i<n-1;i++){
                e+=g[v[i]][v[i+1]];
            }
            e+=g[v[n-1]][v[0]];
            if(e==k)ans++;
        }while(next_permutation(v.begin()+1,v.end()));

        cout<<ans<<endl;
        return ;
    }

    signed main()
    {
        fast();
        
        int t=1;
        //cin>>t;
        while(t--){
            solve();
        }
        return 0;
    }

D.队列


Key:STL-queue

AC代码

    #include<bits/stdc++.h>
    #define endl '\n'
    #define int int long long
    #define pb push_back
    #define bs bitset
    #define val(a) (a<'a' ? (a-'A'+'a') : a)
    #define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
    using namespace std;

    typedef pair<char,int> PCI;
    typedef pair<int,int> PII;
    typedef pair<long long, long long> PLL;
    typedef priority_queue<int> PQ;
    typedef priority_queue<int, vector<int>, greater<int>> Q;
    const int N = 2e5+10, MAX = 1e9, INF = -1e9;

    int n;
    int op;
    int x;
    queue<int> q;

    void solve(){
        cin>>n;
        while(n--){
            cin>>op;
            if(op==1){
                cin>>x;
                q.push(x);
            }
            else if(op==2){
                if(q.empty())cout<<"ERR_CANNOT_POP"<<endl;
                else q.pop();
            }
            else if(op==3){
                if(q.empty())cout<<"ERR_CANNOT_QUERY"<<endl;
                else cout<<q.front()<<endl;
            }
            else{
                cout<<q.size()<<endl;
            }
        }
        return ;
    }

    signed main()
    {
        fast();
        
        int t=1;
        //cin>>t;
        while(t--){
            solve();
        }
        return 0;
    }

E.相似度计算


Key:STL-set,数学-集合论

AC代码

    #include<bits/stdc++.h>
    #define endl '\n'
    #define int int long long
    #define pb push_back
    #define bs bitset
    #define val(a) (a<'a' ? (a-'A'+'a') : a)
    #define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
    using namespace std;

    typedef pair<char,int> PCI;
    typedef pair<int,int> PII;
    typedef pair<long long, long long> PLL;
    typedef priority_queue<int> PQ;
    typedef priority_queue<int, vector<int>, greater<int>> Q;
    const int N = 2e5+10, MAX = 1e9, INF = -1e9;

    set<string> a;
    set<string> b;
    int x=0;
    int m,n;
    string s;


    void solve(){
        cin>>m>>n;
        for(int i=1;i<=m;i++){
            cin>>s;
            for(auto &i:s){
                i=val(i);
            }
            a.insert(s);
        }
        for(int i=1;i<=n;i++){
            cin>>s;
            for(auto &i:s){
                i=val(i);
            }
            b.insert(s);
        }
        for(auto i:a){
            if(b.find(i)!=b.end())x++;
        }
        cout<<x<<endl;
        cout<<a.size()+b.size()-x<<endl;
        return ;
    }

    signed main()
    {
        fast();
        
        int t=1;
        //cin>>t;
        while(t--){
            solve();
        }
        return 0;
    }

F.因子化简


Key:数学-数论-质数唯一分解定理

AC代码

    #include<bits/stdc++.h>
    #define endl '\n'
    #define int int long long
    #define pb push_back
    #define bs bitset
    #define val(a) (a<'a' ? (a-'A'+'a') : a)
    #define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
    using namespace std;

    typedef pair<char,int> PCI;
    typedef pair<int,int> PII;
    typedef pair<long long, long long> PLL;
    typedef priority_queue<int> PQ;
    typedef priority_queue<int, vector<int>, greater<int>> Q;
    const int N = 2e5+10, MAX = 1e9, INF = -1e9;

    int x,k;
    vector<PII> v;

    void solve(){
        cin>>x>>k;
        for(int i=2;i*i<=x;i++){
            int num=0;
            while(x%i==0){
                num++;
                x/=i;
            }
            v.pb({i,num});
        }
        if(x!=1)v.pb({x,1});
        int ans=1;
        for(auto i:v){
            if(i.second>=k)ans*=pow(i.first,i.second);
        }
        cout<<ans<<endl;
        v.clear();
        return ;
    }

    signed main()
    {
        fast();
        
        int t=1;
        cin>>t;
        while(t--){
            solve();
        }
        return 0;
    }

G.矩阵乘法


Key:数学-线性代数

AC代码

    #include<bits/stdc++.h>
    #define endl '\n'
    #define int int long long
    #define pb push_back
    #define bs bitset
    #define val(a) (a<'a' ? (a-'A'+'a') : a)
    #define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
    using namespace std;

    typedef pair<char,int> PCI;
    typedef pair<int,int> PII;
    typedef pair<long long, long long> PLL;
    typedef priority_queue<int> PQ;
    typedef priority_queue<int, vector<int>, greater<int>> Q;
    const int N = 1005, MAX = 1e9, INF = -1e9;

    int n,m,k;
    int a[N][N];
    int b[N][N];

    void solve(){
        cin>>n>>m>>k;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin>>a[i][j];
            }
        }
        for(int i=1;i<=m;i++){
            for(int j=1;j<=k;j++){
                cin>>b[i][j];
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=k;j++){
                int e=0;
                for(int q=1;q<=m;q++){
                    e+=a[i][q]*b[q][j];
                }
                cout<<e<<" ";
            }
            cout<<endl;
        }
        return ;

    }

    signed main()
    {
        fast();
        
        int t=1;
        //cin>>t;
        while(t--){
            solve();
        }
        return 0;
    }

H.A-B数对


Key:数学-组合数学

AC代码

    #include<bits/stdc++.h>
    #define endl '\n'
    #define int int long long
    #define pb push_back
    #define bs bitset
    #define val(a) (a<'a' ? (a-'A'+'a') : a)
    #define fast() ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
    using namespace std;

    typedef pair<char,int> PCI;
    typedef pair<int,int> PII;
    typedef pair<long long, long long> PLL;
    typedef priority_queue<int> PQ;
    typedef priority_queue<int, vector<int>, greater<int>> Q;
    const int N = 2e5+10, MAX = 1e9, INF = -1e9;

    int n,c;
    int e;
    map<int,int> mp;
    int ans=0;

    void solve(){
        cin>>n>>c;
        for(int i=1;i<=n;i++){
            cin>>e;
            mp[e]++;
        }
        for(auto i:mp){
            int t=i.first;
            if(mp.find(t+c)!=mp.end()){
                ans+=mp[t]*mp[t+c];
            }
        }
        cout<<ans<<endl;
        return ;
    }

    signed main()
    {
        fast();
        
        int t=1;
        //cin>>t;
        while(t--){
            solve();
        }
        return 0;
    }
posted @ 2025-07-23 15:54  Oaths  阅读(6)  评论(0)    收藏  举报