UVALive 4875

//这题值得总结的地方就是尽量写函数,,不能说什么封装吧,,也会使代码量
//减少很多,更别提简洁了
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <vector>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAX=100,INF=1<<30;
int p,T,n;
int ptr;

char c;
string ans,val,tryIt;
string ioQueue;

inline
bool judge(const string & str){
    bool is=1;
    ioQueue.clear();
    for(int i=0;i<2;i++){
        if(ptr>=n){
            is=0;
            break;
        }
        tryIt.clear();
        cin>>c; tryIt.push_back(c);
        cin>>c; tryIt.push_back(c);
        ioQueue+=tryIt;
        ++ptr;
        if(tryIt!=str){
            is=0;
            break;
        }
    }
//    cout<<"judge: "<<str<<" "
//        <<ioQueue<<" "<<(int)ioQueue.size()/2<<endl;
    for(int i=(int)ioQueue.size()-1;i>=0;i--)
        cin.putback(ioQueue[i]);//val string not back
    ptr-=(int)ioQueue.size()/2;
    return is;
}

string toHex(int count){
    string str;
    str.clear();
    int x=(count&240)>>4;
    if(x<10)
        c=x+'0';
    else
        c=x-10+'A';
    str.push_back(c);

    x=count&15;
    if(x<10)
        c=x+'0';
    else
        c=x-10+'A';
    str.push_back(c);
    return str;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("i.txt", "r", stdin);
    freopen("o.txt", "w", stdout);
#endif
    bool is;
    int repeat,length;
    string temp;

    cin>>p;

    while(p--){
        cin>>T>>n;
        ptr=0;
        ans.clear();
        while(ptr<n){
            val.clear();
            cin>>c; val.push_back(c);
            cin>>c; val.push_back(c);
//            cout<<"POS: "<<val<<endl;
            ++ptr;
            is=judge(val);
            if(is){//is a run
                repeat=1;
                while(1){
                    if(ptr>=n)
                        break;

                    tryIt.clear();
                    cin>>c; tryIt.push_back(c);
                    cin>>c; tryIt.push_back(c);
                    ++ptr;
                    if(tryIt!=val){
                        cin.putback(tryIt[1]);
                        cin.putback(tryIt[0]);
                        --ptr;
                        break;
                    }
                    ++repeat;
                    if(repeat>=130)
                        break;
                }
                ans+=toHex(repeat+128-3);
                ans+=val;
            }
            else{
                temp.clear();
                temp+=val;
                length=1;
                while(1){
                    if(ptr>=n)
                        break;
                    val.clear();
                    cin>>c; val.push_back(c);
                    cin>>c; val.push_back(c);
                    ++ptr;
                    is=judge(val);
                    if(!is){
                        temp+=val;
                        ++length;
                        if(length>=128)
                            break;
                    }
                    else{
                        cin.putback(val[1]);
                        cin.putback(val[0]);
                        --ptr;
                        break;
                    }
                }
                ans+=toHex(length-1);
                ans+=temp;
            }
        }
        cout<<T<<" "<<(int)ans.size()/2<<endl;
        for(int i=0;i<(int)ans.size();i++){
            if(i&&i%80==0)
                cout<<endl;
            cout<<ans[i];
        }
        cout<<endl;
    }


    return 0;
}

你可能感兴趣的:(c,String)