Google 2013 campus test-R1

Reading Phone Number

#include<iostream>
#include<fstream>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<algorithm>
#include<sstream>
#define _USE_MATH_DEFINES
#include<math.h>

using namespace std;
string Sub(char val,int count){
    if(count==0)return "";
    string pre,after;
    bool sep=false;
    switch(count){
    case 1:
        pre="";
        break;
    case 2:
        pre="double";
        break;
    case 3:
        pre="triple";
        break;
    case 4:
        pre="quadruple";
        break;
    case 5:
        pre="quintuple";
        break;
    case 6:
        pre="sextuple";
        break;
    case 7:
        pre="septuple";
        break;
    case 8:
        pre="octuple";
        break;
    case 9:
        pre="nonuple";
        break;
    case 10:
        pre="decuple";
        break;
    default:
        sep=true;
        break;
    }
    switch(val){
    case '0':
        after="zero";
        break;
    case '1':
        after="one";
        break;
    case '2':
        after="two";
        break;
    case '3':
        after="three";
        break;
    case '4':
        after="four";
        break;
    case '5':
        after="five";
        break;
    case '6':
        after="six";
        break;
    case '7':
        after="seven";
        break;
    case '8':
        after="eight";
        break;
    case '9':
        after="nine";
        break;
    }
    if(sep){
        pre=after;
        for(int i=1;i<count;i++){
            pre+=" "+after;
        }
        return pre;
    }
    if(pre=="")return after;
    else return pre+" "+after;
}
int main(){
    
    ifstream ifs("H:/Test/A-large-practice.in");
    ofstream ofs("H:/Test/A-large-practice.out");
    int t;
    ifs>>t;
    string N,F,O;
    stringstream ss;
    for(int iii=0;iii<t;iii++){
        ifs>>N>>F;
        vector<string> len;
        int start=0;
        for(int i=0;i<F.length();i++)
        {
            if(F[i]=='-'){
                len.push_back(F.substr(start,i-start));
                start=i+1;
            }
        }
        len.push_back(F.substr(start,F.length()-start));
        O.clear();
        int index=0;
        for(int i=0;i<len.size();i++){
            char current=N[index];
            int count=1;
            int leni;
            ss.clear();
            ss<<len[i];
            ss>>leni;
            for(int j=1;j<leni;j++)
            {
                index++;
                 if(N[index]!=current){
                    if(O=="")O+=Sub(current,count);
                    else O+=" "+Sub(current,count);
                    current=N[index];
                    count=1;
                }
                else{
                    count++;
                }
            }
            if(O=="")O+=Sub(current,count);
            else O+=" "+Sub(current,count);
            index++;
        }
        ofs<<"Case #"<<iii+1<<": "<<O<<endl;
    }
    ifs.close();
    ofs.close();
}
View Code

Rational Number Tree

#include<iostream>
#include<fstream>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<algorithm>
#include<sstream>
#include<queue>
#include<sstream>
//#define _USE_MATH_DEFINES
#include<math.h>
using namespace std;
void GetAt(vector<unsigned long long>& vec,unsigned long long n){
    if(n==1){
        vec[0]=1;
        vec[1]=1;
        return;
    }
    GetAt(vec,n/2);
    if(n%2==0){
        vec[1]=vec[0]+vec[1];
    }
    else{
        vec[0]=vec[1]+vec[0];
    }
}
unsigned long long  GetPos(unsigned long long p,unsigned long long q){
    if(p>q)return GetPos(p-q,q)*2+1;
    else if(p<q)return GetPos(p,q-p)*2;
    else return 1;
}
int main(){
    ifstream ifs("H:/Test/B-large-practice.in");
    ofstream ofs("H:/Test/B-large-practice.out");
    stringstream ss;
    int t;
    ifs>>t;
    int id;
    string s;
    unsigned long long n,p,q;
    for(int iii=0;iii<t;iii++){
        ifs>>id;
        ofs<<"Case #"<<iii+1<<": ";
        if(id==1){
            ifs>>s;
            ss.clear();
            ss<<s;
            ss>>n;
            vector<unsigned long long> vec;
            vec.resize(2);
            GetAt(vec,n);
            ofs<<vec[0]<<" "<<vec[1]<<endl;
        }
        else{
            ifs>>p>>q;
            ofs<<GetPos(p,q)<<endl;
        }
    }
    ifs.close();
    ofs.close();
}
View Code

Sorting

#include<iostream>
#include<fstream>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<algorithm>
#include<sstream>
//#define _USE_MATH_DEFINES
#include<math.h>
using namespace std;

int main(){

    ifstream ifs("H:/Test/C-large-practice.in");
    ofstream ofs("H:/Test/C-large-practice.out");
    int t;
    ifs>>t;
    int N,val;
    vector<int>v1,i1;
    vector<int>v2,i2;
    for(int iii=0;iii<t;iii++){
        ifs>>N;
        v1.clear();
        v2.clear();
        i1.clear();
        i2.clear();
        for(int i=0;i<N;i++){
            ifs>>val;
            int valv;
            if(val>0)valv=val;
            else valv=-val;
            if(valv%2==1){
                v1.push_back(val);
                i1.push_back(i);
            }
            else{
                v2.push_back(val);
                i2.push_back(i);
            }
        }
        if(v1.size()!=0)sort(v1.begin(),v1.end());
        if(v2.size()!=0){
            sort(v2.begin(),v2.end());
        for(int i=0;i<v2.size()/2;i++){
            int temp=v2[i];
            v2[i]=v2[v2.size()-i-1];
            v2[v2.size()-i-1]=temp;
        }
        }
        ofs<<"Case #"<<iii+1<<": ";
        if(v1.size()==0){
            ofs<<v2[0];
            for(int i=1;i<v2.size();i++){
                ofs<<" "<<v2[i];
            }
            ofs<<endl;
        }
        else if(v2.size()==0){
            ofs<<v1[0];
            for(int i=1;i<v1.size();i++){
                ofs<<" "<<v1[i];
            }
            ofs<<endl;
        }
        else{
        int ii1=0,ii2=0;
        if(i1[ii1]<i2[ii2]){
            ofs<<v1[ii1];
            ii1++;
        }
        else{
            ofs<<v2[ii2];
            ii2++;
        }
        while(ii1<i1.size()&&ii2<i2.size()){
            if(i1[ii1]<i2[ii2]){
                ofs<<" "<<v1[ii1];
                ii1++;
            }
            else{
                ofs<<" "<<v2[ii2];
                ii2++;
            }
        }
        while(ii1<i1.size()){
            ofs<<" "<<v1[ii1];
            ii1++;
        }
        while(ii2<i2.size()){
            ofs<<" "<<v2[ii2];
            ii2++;
        }
        ofs<<endl;
        }
    }
    ifs.close();
    ofs.close();
}
View Code

Cross the maze

#include<iostream>
#include<fstream>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<algorithm>
#include<sstream>
#include<queue>
#include<sstream>
//#define _USE_MATH_DEFINES
#include<math.h>
using namespace std;
char GetChar(int t){
    switch(t){
    case 0:
        return 'N';
        break;
    case 1:
        return 'E';
        break;
    case 2:
        return 'S';
        break;
    case 3:
        return 'W';
        break;
    default:
        break;
    }
    return 0;
}
bool GetAt(vector<string>&mat,int cx,int cy){
    if(cx<0||cx>=mat.size()||cy<0||cy>=mat[0].length()){
        return false;
    }
    else return mat[cy][cx]=='.';
}
bool GetAt(vector<string>&mat,int cx,int cy,int t){
    switch(t){
    case 0:
        return GetAt(mat,cx,cy-1);
        break;
    case 1:
        return GetAt(mat,cx+1,cy);
        break;
    case 2:
        return GetAt(mat,cx,cy+1);
        break;
    case 3:
        return GetAt(mat,cx-1,cy);
        break;
    default:
        break;
    }
}
void Update(int& cx,int& cy,int t){
    switch(t){
    case 0:
        cy--;
        break;
    case 1:
        cx++;
        break;
    case 2:
        cy++;
        break;
    case 3:
        cx--;
        break;
    default:
        break;
    }
}
void Update2(int& t,int& left, bool clockwise){
    if(clockwise){
        left=t;
        t=(t+1)%4;
    }
    else{
        t=left;
        left=left-1;
        if(left<0)left+=4;
    }
}
int main(){
    ifstream ifs("H:/Test/D-large-practice.in");
    ofstream ofs("H:/Test/D-large-practice.out");
    stringstream ss;
    int t,N,sx,sy,ex,ey;
    vector<string> mat;
    vector<char> Path;
    ifs>>t;
    for(int iii=0;iii<t;iii++){
        ifs>>N;
        mat.resize(N);
        for(int i=0;i<N;i++){
            ifs>>mat[i];
        }
        Path.clear();
        ifs>>sy>>sx>>ey>>ex;
        sx--;
        sy--;
        ex--;
        ey--;
        ofs<<"Case #"<<iii+1<<": ";
        int cx=sx,cy=sy;
        bool find=true;
        Path.clear();
        int t;
        if(cx==0){
            if(cy==0)t=1;
            else t=3;
        }
        else{
            if(cy==0)t=2;
            else t=0;
        }
        int left=t-1;
        if(left<0)left+=4;
        while(cx!=ex||cy!=ey){
            if(Path.size()>=10000){
                find=false;
                break;
            }
            if(GetAt(mat,cx,cy,left)){
                Update2(t,left,false);
                Update(cx,cy,t);
                Path.push_back(GetChar(t));
            }
            else{
                Update2(t,left,true);
                if(GetAt(mat,cx,cy,left)){
                    Update2(t,left,false);
                    Update(cx,cy,t);
                    Path.push_back(GetChar(t));
                }
                else{
                    Update2(t,left,true);
                    if(GetAt(mat,cx,cy,left)){
                        Update2(t,left,false);
                        Update(cx,cy,t);
                        Path.push_back(GetChar(t));
                    }
                    else{
                        Update2(t,left,true);
                        if(GetAt(mat,cx,cy,left)){
                            Update2(t,left,false);
                            Update(cx,cy,t);
                            Path.push_back(GetChar(t));
                        }
                        else{
                            find=false;
                            break;
                        }
                    }
                }
            }
        }
        if(find){
            ofs<<Path.size()<<endl;
            for(int i=0;i<Path.size();i++){
                ofs<<Path[i];
            }
            ofs<<endl;
        }
        else{
            ofs<<"Edison ran out of energy."<<endl;
        }
    }
    ifs.close();
    ofs.close();
}
View Code

Spaceship Defence

#include<iostream>
#include<fstream>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<algorithm>
#include<sstream>
#include<queue>
//#define _USE_MATH_DEFINES
#include<math.h>
using namespace std;
#define SIZE 1332
int cost[SIZE];
bool visited[SIZE];
vector<vector<pair<int,int>> >edges[SIZE];
int StringToInt(string& a){
    if(a.length()==1){
        if(a[0]>='0'&&a[0]<='9')return a[0]-'0';
        else return a[0]-'a'+10;
    }
    else{
        int a1,a2;
        if(a[0]>='0'&&a[0]<='9')a1=a[0]-'0';
        else a1=a[0]-'a'+10;
        if(a[1]>='0'&&a[1]<='9')a2=a[1]-'0';
        else a2=a[1]-'a'+10;
        return (a1+1)*36+a2;
    }

}
int FindMin(){
    int cm=-1;
    int ret;
    for(int i=0;i<SIZE;i++){
        if(!visited[i]){
            if(cm==-1){
                cm=cost[i];
                ret=i;
            }
            else{
                if(cost[i]!=-1&&cost[i]<cm){
                    cm=cost[i];
                    ret=i;
                }
            }
        }
    }
    if(cm==-1)return-1;
    return ret;
}
int main(){

    ifstream ifs("H:/Test/E-large-practice.in");
    ofstream ofs("H:/Test/E-large-practice.out");
    int t;
    ifs>>t;
    int N,M,S;
    vector<string>color;
    for(int iii=0;iii<t;iii++){
        
        ofs<<"Case #"<<iii+1<<": "<<endl;
        ifs>>N;
        color.resize(N);
        for(int i=0;i<SIZE;i++){
            edges[i].clear();
        }
        for(int i=0;i<N;i++)
        {
            ifs>>color[i];
        }
        ifs>>M;
        int ai,bi,ti;
        for(int i=0;i<M;i++){
            ifs>>ai>>bi>>ti;
            ai--;
            bi--;
            int i1=StringToInt(color[ai]);
            int i2=StringToInt(color[bi]);
            edges[i1].push_back(pair<int,int>(i2,ti));
        }
        ifs>>S;
        cout<<iii<<endl;
        for(int ii=0;ii<S;ii++){
            for(int i=0;i<SIZE;i++){
                cost[i]=-1;
                visited[i]=false;
            }
            int p,q;
            ifs>>p>>q;
            p--;
            q--;
            int mi=StringToInt(color[p]);
            cost[mi]=0;
            while(mi!=-1){
                vector<pair<int,int>>& vec=edges[mi];
                for(int i=0;i<vec.size();i++){
                    if(cost[vec[i].first]==-1||cost[vec[i].first]>cost[mi]+vec[i].second){
                        cost[vec[i].first]=cost[mi]+vec[i].second;
                    }
                }
                visited[mi]=true;
                mi=FindMin();
            }
            ofs<<cost[StringToInt(color[q])]<<endl;
        }
    }
    ifs.close();
    ofs.close();
}
View Code

 

posted @ 2013-09-23 21:16  懒猫欣  阅读(314)  评论(0编辑  收藏  举报