C++ 彰雲嘉區複賽106-4 變位字判斷

用map很方便(如果是C++11更方便)

#include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<string>
#include<cstdlib>
using namespace std;

int main(){
    ios::sync_with_stdio(0);cin.tie(0);

    string a,b;
    int n;
    cin>>n;
    getline(cin,a);
    while(n--){
        getline(cin,a);
        getline(cin,b);
        map<char,int> ma,mb;

        for(int i=0;i<a.size();i++){
            char c=a[i];
            if(islower(c)){
                ma[c]++;
            }else if(isupper(c)){
                ma[tolower(c)]++;
            }
        }

        for(int i=0;i<b.size();i++){
            char c=b[i];
            if(islower(c)){
                mb[c]++;
            }else if(isupper(c)){
                mb[tolower(c)]++;
            }
        }

        bool isv=true;
        map<char,int>::iterator ta=ma.begin(),tb=mb.begin();
        for(ta=ma.begin();ta!=ma.end() && tb!=mb.end();ta++){
            pair<char,int> pa=(*ta),pb=(*tb);
            if(pa!=pb){
                isv=false;
            }
            tb++;
        }
        if(ma.size()!=mb.size()) isv=false;
        cout<<isv<<"\n";
    }
    return 0;
}

出處:GreenJudge

C++ 彰雲嘉區複賽106-3 費波南希數列

用到最簡單DP和二分搜(都可以不用)

#include<iostream>
#include<algorithm>
using namespace std;

int main(){
    ios::sync_with_stdio(0);cin.tie(0);

    int n,f[200];
    cin>>n;

    f[0]=-1;f[1]=1;f[2]=1;
    for(int i=3;i<100;i++){
        f[i]=f[i-1]+f[i-2];
        if(f[i]<0) f[i]=0x3f3f3f3f;
    }

    while(n--){
        int a;
        cin>>a;
        int it=lower_bound(f,f+100,a)-f;
        if(f[it]==a){
            cout<<it;
        }else{
            cout<<-1;
        }
        cout<<"\n";
    }
    return 0;
}

出處:GreenJudge

C++ 彰雲嘉區複賽106-2 跑長編碼與資料壓縮

字串基本應用

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

string num[10]={"000","001","010","011","100","101","110","111"};

string print(int sum,char bit){
    string re(1,bit);
    re+=num[sum];
    re+=" ";
    return re;
}

int main(){
    ios::sync_with_stdio(0);cin.tie(0);

    int iptNum=0;
    cin>>iptNum;

    string bit,out;
    getline(cin,bit);

    while(iptNum--){
        out="";
        getline(cin,bit);
        int j,sum=1;
        long long rate=0;
        for(j=1;j<bit.size();j++){
            if(bit[j]!='0' && bit[j]!='1'){
                cout<<"-1\n";
                j++;
                break;
            }

            if(bit[j-1]==bit[j] && sum<7){
                sum++;
            }else{
                out+=print(sum,bit[j-1]);
                rate+=4;
                sum=1;
            }
        }
        out+=print(sum,bit[j-1]);
        rate+=4;
        rate*=1000;
        rate/=bit.size();
        rate=(rate+5)/10;

        if(bit[j-1]=='0' || bit[j-1]=='1'){
            cout<<out<<rate<<"\n";
        }
    }
    return 0;
}

出處:GreenJudge