zoukankan      html  css  js  c++  java
  • Google Round A China New Grad Test

    Problem A Read Phone Number

    Do you know how to read the phone numbers in English? Now let me tell you.

    For example, In China, the phone numbers are 11 digits, like: 15012233444. Someone divides the numbers into 3-4-4 format, i.e. 150 1223 3444. While someone divides the numbers into 3-3-5 format, i.e. 150 122 33444. Different formats lead to different ways to read these numbers:

    150 1223 3444 reads one five zero one double two three three triple four.

    150 122 33444 reads one five zero one double two double three triple four.

    Here comes the problem:

    Given a list of phone numbers and the dividing formats, output the right ways to read these numbers.

    Rules:

    Single numbers just read them separately.

    2 successive numbers use double.

    3 successive numbers use triple.

    4 successive numbers use quadruple.

    5 successive numbers use quintuple.

    6 successive numbers use sextuple.

    7 successive numbers use septuple.

    8 successive numbers use octuple.

    9 successive numbers use nonuple.

    10 successive numbers use decuple.

    More than 10 successive numbers read them all separately.

    Input

    The first line of the input gives the number of test cases, TT lines|test cases follow. Each line contains a phone number N and the dividing format F, one or more positive integers separated by dashes (-), without leading zeros and whose sum always equals the number of digits in the phone number.

    Output

    For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the reading sentence in English whose words are separated by a space.

    Limits

    1 ≤ T ≤ 100.

    Small dataset

    1 ≤ length of N ≤ 10.

    Large dataset

    1 ≤ length of N ≤ 100.

    Sample


    Input 
     

      

    3

    15012233444 3-4-4

    15012233444 3-3-5

    12223 2-3

     

     

     

    Output 
     

     

    Case #1: one five zero one double two three three triple four

    Case #2: one five zero one double two double three triple four

    Case #3: one two double two three

     

     

    总结

    1. 做这类题目, 就要多写几个函数, 每进入一个状态等同于进入一个函数

    2. 状态函数的参数包含 int &i

    3. 外部的循环变量最好用 while, 即使用 for, 也不要使用 i++ 操作

     

     

    /*

    *    简单自动机, google 几乎每年都考

    *    这道题的难点在于 offset 的使用, 下面的代码写的并不理想, offset 添加的位置太过 tricky

    *    应该有更好的解决办法

    *    100 行代码, 写了半小时, 出现 bug 的地方就是 offset 忘了加上

    *    

    */

     

     

    #include <iostream>

    #include <stdio.h>

    #include <memory.h>

    #include <algorithm>

    #include <vector>

    #include <map>

    #include <set>

    #include <string>

    using namespace std;

     

    char phoneNumber[500];

    char cuts[500];

     

    string itos[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

    string dup[12] = {"", "", "double", "triple", "quadruple", "quintuple", "sextuple",

        "septuple", "octuple", "nonuple", "decuple"};

    /*

    *    not sure if 0-1-10 this kind of data exists

    */

    vector<int> handleCut(char *s) {

        int len = strlen(s);

        vector<int> retVec;

     

        int leftOver = 0;

        for(int i = 0; i < len; i ++) {

            if(s[i] == '-') {

                retVec.push_back(leftOver);

                leftOver = 0;

            }else {

                leftOver *= 10;

                leftOver += (s[i]-'0');

            }

        }

        if(leftOver != 0) {

            retVec.push_back(leftOver);

        }

        return retVec;

    }

     

    void handleDuplication(const char* s, const char ch, int &i, const int offset, const int cnt) {

        int dupTimes = 0;

        int st = i;

     

        while(i < cnt && s[i+offset] == ch) {

            i ++;

            dupTimes ++;

        }

     

        if(dupTimes <= 1 || dupTimes >= 10) {

            for(int j = 1; j <= dupTimes; j ++) {

                printf("%s ", itos[s[st+offset]-'0'].c_str());

            }

            return;

        }

     

        printf("%s %s ", dup[dupTimes].c_str(), itos[s[st+offset]-'0'].c_str());

     

    }

     

    void handleParty(const char* s, const int offset, const int cnt) {

        int i = 0;

        while(i < cnt-1) {

            if(s[i+offset] == s[i+1+offset]) { /* handle duplication */

                handleDuplication(s, s[i+offset], i, offset, cnt);

            }else {

                printf("%s ", itos[s[i+offset]-'0'].c_str() );

                i ++;

            }

        }

        if(i < cnt) {

            printf("%s ", itos[s[i+offset]-'0'].c_str());

        }

    }

     

    int main() {

        freopen("C:\Users\vincent\Dropbox\workplacce\joj\test.txt", "r", stdin);

     

        int T, iCase = 0;

        scanf("%d", &T);

        while(T--) {

            iCase ++;

            printf("Case #%d: ", iCase);

     

            scanf("%s%s", phoneNumber, cuts);

     

            vector<int> nums = handleCut(cuts);

     

            int offset = 0;

            for(int i = 0; i < nums.size(); i ++) {

                handleParty(phoneNumber, offset, nums[i]);

                offset += nums[i];

            }

            printf(" ");

        }

        return 0;

    }

     

     

    Problem B Rational Number Tree

    Consider an infinite complete binary tree where the root node is 1/1 and left and right childs of node p/q are p/(p+q) and (p+q)/q, respectively. This tree looks like:

    1/1

    ______|______

    | |

    1/2 2/1

    ___|___ ___|___

    | | | |

    1/3 3/2 2/3 3/1

    ...

    It is known that every positive rational number appears exactly once in this tree. A level-order traversal of the tree results in the following array:

    1/1, 1/2, 2/1, 1/3, 3/2, 2/3, 3/1, ...

    Please solve the following two questions:

    1. Find the n-th element of the array, where n starts from 1. For example, for the input 2, the correct output is 1/2.
    2. Given p/q, find its position in the array. As an example, the input 1/2 results in the output 2.

    Input

    The first line of the input gives the number of test cases, T. T test cases follow. Each test case consists of one line. The line contains a problem id (1 or 2) and one or two additional integers:

    1. If the problem id is 1, then only one integer n is given, and you are expected to find the n-th element of the array.
    2. If the problem id is 2, then two integers p and q are given, and you are expected to find the position of p/q in the array.

    Output

    For each test case:

    1. If the problem id is 1, then output one line containing "Case #x: p q", where x is the case number (starting from 1), and p, q are numerator and denominator of the asked array element, respectively.
    2. If the problem id is 2, then output one line containing "Case #x: n", where x is the case number (starting from 1), and n is the position of the given number.

    Limits

    1 ≤ T ≤ 100; p and q are relatively prime.

    Small dataset

    1 ≤ n, p, q ≤ 216-1; p/q is an element in a tree with level number ≤ 16.

    Large dataset

    1 ≤ n, p, q ≤ 264-1; p/q is an element in a tree with level number ≤ 64.

    Sample


    Input 
     


    Output 
     

    4

    1 2

    2 1 2

    1 5

    2 3 2

    Case #1: 1 2

    Case #2: 2

    Case #3: 3 2

    Case #4: 5

     

    总结

    1. 一开始以为是数学题, 后来用 dfs 强行做的, 巧妙的使用 dfs(int &n) 这种形式的深搜

    2. 有人将 rational number tree 表示成完全二叉树, 不过实现起来, 依然是深搜的原理

     

    #include <iostream>

    #include <stdio.h>

    #include <memory.h>

    #include <algorithm>

    #include <vector>

    #include <map>

    #include <set>

    #include <string>

    using namespace std;

     

    void getPQ(int n, int &p, int &q) {

        if(n == 1) {

            p = q = 1;

            return ;

        }

     

        getPQ(n/2, p, q);

        

        int newp, newq;

        if(n & 1) { /* 右孩子 */

            newp = p + q;

            newq = q;

     

        }else {

            newp = p;

            newq = p + q;

        }

        p = newp;

        q = newq;

    }

     

    void getN(int &n, int p, int q) {

        if(p == 1 && q == 1) {

            n = 1;

            return;

        }

     

        int newp, newq;

        if(p > q) { /* 右孩子 */

            newp = p - q;

            newq = q;

            getN(n, newp, newq);

            n = n*2 + 1;

        }else {

            newp = p;

            newq = q-p;

            getN(n, newp, newq);

            n = n*2;

        }

    }

     

    int main() {

        freopen("C:\Users\vincent\Dropbox\workplacce\joj\test.txt", "r", stdin);

     

        int T, iCase = 0;

        scanf("%d", &T);

        while(T --) {

            iCase ++;

            

            int cmd;

            scanf("%d", &cmd);

     

            int n, p, q;

            if(cmd == 1) {

                scanf("%d", &n);

                getPQ(n, p, q);

                printf("Case #%d: %d %d ", iCase, p, q);

            }else {

                scanf("%d%d", &p, &q);

                getN(n, p, q);

                printf("Case #%d: %d ", iCase, n);

            }

            

        }

        return 0;

    }

     

    Problem C Sorting

    Alex and Bob are brothers and they both enjoy reading very much. They have widely different tastes on books so they keep their own books separately. However, their father thinks it is good to promote exchanges if they can put their books together. Thus he has bought an one-row bookshelf for them today and put all his sons' books on it in random order. He labeled each position of the bookshelf the owner of the corresponding book ('Alex' or 'Bob').

    Unfortunately, Alex and Bob went outside and didn't know what their father did. When they were back, they came to realize the problem: they usually arranged their books in their own orders, but the books seem to be in a great mess on the bookshelf now. They have to sort them right now!!

    Each book has its own worth, which is represented by an integer. Books with odd values of worth belong to Alex and the books with even values of worth belong to Bob. Alex has a habit of sorting his books from the left to the right in an increasing order of worths, while Bob prefers to sort his books from the left to the right in a decreasing order of worths.

    At the same time, they do not want to change the positions of the labels, so that after they have finished sorting the books according their rules, each book's owner's name should match with the label in its position.

    Here comes the problem. A sequence of N values s0, s1, ..., sN-1 is given, which indicates the worths of the books from the left to the right on the bookshelf currently. Please help the brothers to find out the sequence of worths after sorting such that it satisfies the above description.

    Input

    The first line of input contains a single integer T, the number of test cases. Each test case starts with a line containing an integer N, the number of books on the bookshelf. The next line contains N integers separated by spaces, representing s0, s1, ..., sN-1, which are the worths of the books.

    Output

    For each test case, output one line containing "Case #X: ", followed by t0, t1, ..., tN-1 in order, and separated by spaces. X is the test case number (starting from 1) and t0, t1, ...,tN-1 forms the resulting sequence of worths of the books from the left to the right.

    Limits

    1 ≤ T ≤ 30.

    Small dataset

    1 ≤ N ≤ 100
    -100 ≤ si ≤ 100

    Large dataset

    1 ≤ N ≤ 1000
    -1000 ≤ si ≤ 1000

    Sample


    Input 
     


    Output 
     

    2

    5

    5 2 4 3 1

    7

    -5 -12 87 2 88 20 11

    Case #1: 1 4 2 3 5

    Case #2: -5 88 11 20 2 -12 87

     

    总结

    1. 把俩人的坐标和坐标对应的值提取出来, value 排好序后再依据各自坐标放回去

     

    #include <iostream>

    #include <stdio.h>

    #include <memory.h>

    #include <algorithm>

    #include <vector>

    #include <map>

    #include <set>

    #include <string>

    using namespace std;

     

     

    int arr[2000];

     

    int main() {

        freopen("C:\Users\vincent\Dropbox\workplacce\joj\test.txt", "r", stdin);

     

        int T, iCase = 0;

        scanf("%d", &T);

        while(T-- ) {

            iCase ++;

            int n;

            scanf("%d", &n);

     

            for(int i = 0; i < n; i ++) {

                scanf("%d", arr+i);

            }

            vector<int> alexindex, bobindex;

            vector<int> alexval, bobval;

     

            for(int i = 0; i < n; i ++) {

                if(abs(arr[i])%2 == 1) {

                    alexindex.push_back(i);

                    alexval.push_back(arr[i]);

                }else {

                    bobindex.push_back(i);

                    bobval.push_back(arr[i]);

                }

            }

            sort(alexval.begin(), alexval.end());

            sort(bobval.begin(), bobval.end());

     

            for(int i = 0; i < alexindex.size(); i ++) {

                arr[alexindex[i]] = alexval[i];

            }

            for(int i = 0; i < bobindex.size(); i ++) {

                arr[bobindex[i]] = bobval[bobindex.size()-i-1];

            }

            printf("Case #%d: ", iCase);

     

            for(int i = 0; i < n; i ++) {

                printf("%d ", arr[i]);

            }

            printf(" ");

        }

     

        return 0;

    }

     

    Problem E Spaceship Defence

    The enemy has invaded your spaceship, and only superior tactics will allow you to defend it! To travel around your spaceship, your soldiers will use two devices: teleporters andturbolifts.

    Teleporters allow your soldiers to move instantly between rooms. Every room contains a teleporter, and rooms are color-coded: if a soldier is in a room with some color, she can use the teleporter in that room to immediately move to any other room with the same color.

    Turbolifts allow your soldiers to move between rooms more slowly. A turbolift is like an elevator that moves in many directions. Each turbolift moves from one room to one other room, and it takes a certain amount of time to travel. Notes about turbolifts:

    • Turbolifts are not two-way: if a turbolift moves soldiers from room a to room b, the same turbolift cannot move soldiers from room b to room a, although there might be another turbolift that does that.
    • More than one soldier can use the same turbolift, and they do not interfere with each other in any way.

    You will be given the locations and destinations of several soldiers. For each soldier, output the minimum amount of time it could take that soldier to travel from his location to his destination.

    Input

    The first line of the input gives the number of test cases, TT test cases follow.

    For every test case:

    The first line of every test case contains an integer N, which is the number of rooms in your spaceship. The rooms are numbered from 1 to N. The following N lines each contain a string telling the color of the rooms, from room 1 to room N. The strings only contain characters a-z (the lower-case English letters) and 0-9 (the number 0 to 9), and the length of each string will be less than or equal to 2.

    The next line in the test case is an integer M, which indicates the number of turbolifts in your spaceship. The following M lines each contain 3 space-separated integers aibiti, telling us that there is a turbolift that can transport soldiers from room ai to room bi in tiseconds.

    The next line in the test case contains an integer S, which is the number of soldiers at your command. The following S lines each contain two integers: the location and destination of one soldier, pj and qj.

    Output

    For each test case, output one line containing only the string "Case #x:", where x is the number of the test case (starting from 1). On the next S lines, output a single integer: on line j, the smallest number of seconds it could take for a soldier to travel from pj to qj. If there is no path from pj to qj, the integer you output should be -1.

    Limits

    1 ≤ S ≤ 100.
    1 ≤ ai, bi ≤ N.
    0 ≤ ti ≤ 1000.
    1 ≤ pj, qj ≤ N.

    Small dataset

    1 ≤ T ≤ 10.
    1 ≤ N ≤ 1000.
    0 ≤ M ≤ 3000.

    Large dataset

    T = 1.
    1 ≤ N ≤ 80000.
    0 ≤ M ≤ 3000.

    Sample


    Input 
     


    Output 
     

    3

    3

    gl

    t3

    t3

    3

    1 2 217

    3 2 567

    1 1 21

    2

    2 1

    2 3

    4

    ca

    bl

    bl

    8z

    0

    3

    1 2

    2 3

    1 1

    8

    re

    b7

    ye

    gr

    0l

    0l

    ye

    b7

    7

    4 1 19

    2 4 21

    2 5 317

    4 5 34

    4 7 3

    4 8 265

    8 6 71

    3

    4 3

    2 6

    1 4

    Case #1:

    -1

    0

    Case #2:

    -1

    0

    0

    Case #3:

    3

    55

    -1

     

    总结

    1. 根据两种不同的传送装置把图构建出来使用 spfa 即可

    2. 优化. 使用 classic 解法需要构建 graph[80k][8k] 大小的数组, 这是难以接受的. 因此, 我们可以将颜色相同的点聚类到一起, 这样空间复杂度下降到 [1400][1400]

     

    #include <iostream>

    #include <stdio.h>

    #include <memory.h>

    #include <algorithm>

    #include <vector>

    #include <map>

    #include <deque>

    #include <set>

    #include <string>

    using namespace std;

     

    class Node {

    public:

        int dst, cost;

        Node() {

            cost = dst = 0;

        }

        Node(int _dst, int _cost):dst(_dst), cost(_cost) {}

    };

     

     

    vector<Node> graph[100000];

     

    const int INF = 0X3F3F3F3F;

    int dist[100000];

     

    int spfa(int st, int ed) {

        memset(dist, 0x3f, sizeof(dist));

     

        deque<int> queue;

        queue.push_back(st);

        dist[st] = 0;

     

        while(!queue.empty()) {

            int fath = queue.front();

            queue.pop_front();

     

            for(int i = 0; i < graph[fath].size(); i ++) { /* iterator fath child*/

                Node child = graph[fath][i];

                int cIndex = child.dst;

                int cCost = child.cost;

     

                if(dist[fath] + cCost < dist[cIndex]) {

                    dist[cIndex] = dist[fath] + cCost;

                    queue.push_back(cIndex);

                }

            }

        }

     

        if(dist[ed] == INF) {

            return -1;

        }

        return dist[ed];

    }

     

     

    int main() {

        freopen("C:\Users\vincent\Dropbox\workplacce\joj\test.txt", "r", stdin);

        int T, iCase = 0;

        scanf("%d", &T);

        while(T --) {

            iCase ++;

            printf("Case #%d: ", iCase);

     

            

     

     

            /* read room color */

            int n;

            scanf("%d", &n);

            /* init graph */

            for(int i = 1; i <= n; i ++) {

                graph[i].clear();

            }

            

            map<string, vector<int> > tele;

            char color[10];

            for(int i = 1; i <= n; i ++) {

                scanf("%s", color);

                string tmp = color;

                if(tele.count(tmp) == 0) {

                    vector<int> vec;

                    tele[tmp] = vec;

                }

                tele[tmp].push_back(i);

            }

     

            /* building graph using tele information */

            map<string, vector<int> >::iterator it_map;

            for(it_map = tele.begin(); it_map != tele.end(); it_map ++) {

                

                for(int i = 0; i < it_map->second.size(); i ++) {

                    for(int j = i+1; j < it_map->second.size(); j ++) {

                        int p = it_map->second[i];

                        int q = it_map->second[j];

                        

                        graph[p].push_back(Node(q, 0));

                        graph[q].push_back(Node(p, 0));

                    }

                }

            }

     

            /*

             *    read turbo information

             *    regard less duplication for now

             */

     

            int m;

            scanf("%d", &m);

            for(int i = 0; i < m; i ++) {

                int p, q, c;

                scanf("%d%d%d", &p, &q, &c);

                graph[p].push_back(Node(q, c));

            }

     

            /*

             *    read soliders

             */

            int k;

            scanf("%d", &k);

            for(int i = 0; i < k; i ++) {

                int p, q;

                scanf("%d%d", &p, &q);

                int res = spfa(p, q);

                printf("%d ", res);

            }

     

        }

     

        return 0;

    }

     

    Problem D Cross The Maze

    总结

    1. 这道题, 看了半天不知道案例 3 是怎么来的. 姑且认定给定的区域是由一层墙围起来的

    2. 这是一个解法

  • 相关阅读:
    百度地图bd map使用方法
    Nodejs学习笔记2
    Nodejs学习笔记-1
    npm的安装和使用?
    如何使用jqueryUi的datepicker日历控件?
    如何 使用vim的 session和viminfo 恢复上一次工作的环境??
    Yii使用笔记 2
    关于link标签的用法, 不声明rel=stylesheet则无效? 在ff中必须声明rel属性!
    物联网产业链八大环节全景图
    一场改变你投资生涯的讨论:职业德州扑克手看交易
  • 原文地址:https://www.cnblogs.com/zhouzhuo/p/3660163.html
Copyright © 2011-2022 走看看