• Educational Codeforces Round 81 题解


    A题

    肯定是1越多越好,因为位数越大越大,所以当n是2的倍数的时候,全是1

    但是n可能不是n的倍数,此时应该有(n-3)/2个1和1个7,并且7在前面

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cmath>
    #include<vector>
    #include<cstring>
    using namespace std;
    typedef long long ll;
    const int N=1e5;
    int main(){
        int t;
        cin>>t;
        while(t--){
            int n;
            cin>>n;
            string res="";
            int i;
            if(n==2){
                cout<<1<<endl;
                continue;
            }
            if(n%2==0){
                int f=n/2;
                for(i=1;i<=f;i++)
                res+="1";
                cout<<res<<endl;
            }
            else{
                int f=(n-3)/2;
                res+="7";
                for(i=1;i<=f;i++)
                res+="1";
                cout<<res<<endl;
            }
        } 
    }
     
    View Code

    B题

    本题有几点需要注意

    1.如果所给x是0,要特判空字符串情况,也就是sum初始为1,其他为0

    2.循环的题目第一看循环节,我们知道题目不是求在哪个位置相等,而是求个数,所以只需要考虑第一节

    3.分两种情况,因为要排除无穷的情况,假如一个循环节内1的个数和0的相等,那么我们就需要遍历一下这个循环节,如果存在满足x的,那么一定有无穷个,否则是0个。

    4.个数不相同的情况,那么我们可以想到,假设一个循环节差t,满足条件的情况肯定时t的倍数加上最后一个循环节中的部分差d等于x

    也就是说(x-d)%t==0,找到一个这样的情况ans++,但是要注意的是,两者必须是同号的,因为模数为0两者不一定同号,如果不同号则没有意义,是相反的。

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cmath>
    #include<vector>
    #include<cstring>
    using namespace std;
    typedef long long ll;
    const int N=1e5;
    int main(){
        int t;
        cin>>t;
        while(t--){
            int n,x;
            string s;
            cin>>n>>x;
            cin>>s;
            int sum=0;
            if(x==0)
            sum+=1;
            int i;
            int cnt1=0;
            int cnt2=0;
            for(i=0;i<n;i++){
                if(s[i]=='0')
                cnt1++;
                else
                cnt2++; 
            }
            int sign=cnt1-cnt2;
            if(sign==0){
                int tmp1=0;
                int tmp2=0;
                for(i=0;i<n;i++){
                    if(s[i]=='0')
                    tmp1++;
                    else
                    tmp2++;
                    if(tmp1-tmp2==x)
                    sum=-1;
                }
                if(sum!=-1)
                sum=0;
            }
            else{
                int tmp1=0;
                int tmp2=0;
                for(i=0;i<n;i++){
                    if(s[i]=='0')
                    tmp1++;
                    else 
                    tmp2++;
                    int d=tmp1-tmp2;
                    if((x-d)%sign==0&&(x-d)/sign>=0)
                    sum++;
                }
            }
            cout<<sum<<endl;
        }
    }
     
    View Code

    C题

    本题是暴力题,思路很好想,只需要设计一个指针遍历t字符串,答案不存在只有可能是t中字符串s中没有

    然后不停映射s就行,如果t中连续的子串不是s中的递增的,那么就要ans++;

    问题是实现方法,我在做本题的时候就是苦于无法实现s中字符位置的映射,后来发现可以利用stl中的vector容器来遍历,然后用二分来查找看是否满足条件

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cmath>
    #include<vector>
    #include<cstring>
    using namespace std;
    typedef long long ll;
    const int N=1e5;
    vector<int> v[N];
    int main(){
        int t;
        cin>>t;
        while(t--){
            int i;
            for(i=0;i<26;i++)
            v[i].clear();
            string s;
            string t;
            cin>>s>>t;
            int sum=0;
            int num[27];
            memset(num,0,sizeof num);
            for(i=0;i<s.size();i++){
                num[s[i]-'a']++;
                v[s[i]-'a'].push_back(i);
            }
            int p=-1;
            int ans=1;
            for(i=0;i<t.size();i++){
                if(!num[t[i]-'a']){
                    ans=-1;
                    break;
                }
                else{
                    if(p>=v[t[i]-'a'][num[t[i]-'a']-1]){
                        ans++;
                        p=v[t[i]-'a'][0];
                    }
                    else{
                    p=v[t[i]-'a'][upper_bound(v[t[i]-'a'].begin(),v[t[i]-'a'].end(),p)-v[t[i]-'a'].begin()];
                }
                }
                
            }
            cout<<ans<<endl;
        }
    }
     
    View Code
  • 相关阅读:
    Python 图片识别
    Python发送邮件
    python argparse模块的使用
    datetime中时间的formatter整理
    redis持久化那些事(kēng)儿
    用python自建一个DNS服务器
    从windows到linux的换行转换工具dos2unix
    pypy入门:pypy的安装及使用介绍
    LRU缓存算法与pylru
    用python实现矩阵转置
  • 原文地址:https://www.cnblogs.com/ctyakwf/p/12247848.html
Copyright © 2020-2023  润新知