2015微软实习在线笔试题 - Professor Q's Software

时间限制:10000ms

单点时限:1000ms

内存限制:256MB

描述

Professor Q develops a new software. The software consists of N modules which are numbered from 1 to N. The i-th module will be started up by signal Si. If signal Si is generated multiple times, the i-th module will also be started multiple times. Two different modules may be started up by the same signal. During its lifecircle, the i-th module will generate Ki signals: E1, E2, …, EKi. These signals may start up other modules and so on. Fortunately the software is so carefully designed that there is no loop in the starting chain of modules, which means eventually all the modules will be stoped. Professor Q generates some initial signals and want to know how many times each module is started.

输入

The first line contains an integer T, the number of test cases. T test cases follows.

For each test case, the first line contains contains two numbers N and M, indicating the number of modules and number of signals that Professor Q generates initially.

The second line contains M integers, indicating the signals that Professor Q generates initially.

Line 3~N + 2, each line describes an module, following the format S, K, E1, E2, … , EK. S represents the signal that start up this module. K represents the total amount of signals that are generated during the lifecircle of this module. And E1 … EK are these signals.

For 20% data, all N, M <= 10

For 40% data, all N, M <= 103

For 100% data, all 1 <= T <= 5, N, M <= 105, 0 <= K <= 3, 0 <= S, E <= 105.

Hint: HUGE input in this problem. Fast IO such as scanf and BufferedReader are recommended.

输出

For each test case, output a line with N numbers Ans1, Ans2, … , AnsN. Ansi is the number of times that the i-th module is started. In case the answers may be too large, output the answers modulo 142857 (the remainder of division by 142857).

样例输入

3

3 2

123 256

123 2 456 256

456 3 666 111 256

256 1 90

3 1

100

100 2 200 200

200 1 300

200 0

5 1

1

1 2 2 3

2 2 3 4

3 2 4 5

4 2 5 6

5 2 6 7

样例输出

1 1 3

1 2 2

1 1 2 3 5

  • 方法一:使用栈进行处理,该方法逻辑简单,容易写代码
#include <vector>
#include <stack>
#include <iostream>

using namespace std;

void find (int** a, int len, int b, vector<int>& next);

int main()
{
    int T = 0;
    int N = 0, M = 0;
    cin >> T;

    for(int i = 0; i < T; ++i)
    {
        // N: module num; M: init num
        cin >> N >> M;

        // get the init
        int* init = new int[M]();
        for (int j = 0; j < M; ++j)
            cin >> init[j];

        // get the modules
        int** mods = new int*[N];
        for (int j = 0; j < N; ++j)
        {
            int modId = 0, childNum = 0;
            cin >> modId >> childNum;

            mods[j] = new int[childNum + 2]();
            mods[j][0] = modId;
            mods[j][1] = childNum;
            for (int k = 0; k < childNum; ++k)
                cin >> mods[j][k+2];
        }

        int* cnt = new int[N]();
        stack<int> s;
        vector<int> next;
        for (int j = 0; j < M; ++j)
            s.push(init[j]);
        while (!s.empty())
        {
            next.clear();
            find(mods, N, s.top(), next);
            s.pop();
            for (vector<int>:: iterator iter = next.begin(); iter != next.end(); ++iter)
            {
                cnt[*iter]++;
                for (int j = 0; j < mods[*iter][1]; ++j)
                    s.push(mods[*iter][2+j]);
            }
        }

        for (int j = 0; j < N; ++j)
            cout << cnt[j] << " ";
        cout << endl;

        // release memory
        delete[] init;
        for (int j = 0; j < N; ++j)
            delete[] mods[j];
        delete[] cnt;
    }

}

void find (int** a, int len, int b, vector<int>& next)
{
    for (int i = 0; i < len; ++i)
    {
        if (b == a[i][0])
            next.push_back(i);
    }
}
  • 方法二:最简单直接的查找方法,这个要求写代码时要留意别处理出错。
#include <vector>
#include <iostream>

using namespace std;

void get (int** a, int len, int line, int* cnt);
void find (int** a, int len, int b, vector<int>& next);

int main()
{
    int T = 0;
    int N = 0, M = 0;
    cin >> T;

    for(int i = 0; i < T; ++i)
    {
        // N: module num; M: init num
        cin >> N >> M;

        // get the init
        int* init = new int[M]();
        for (int j = 0; j < M; ++j)
            cin >> init[j];

        // get the modules
        int** mods = new int*[N];
        for (int j = 0; j < N; ++j)
        {
            int modId = 0, childNum = 0;
            cin >> modId >> childNum;

            mods[j] = new int[childNum + 2]();
            mods[j][0] = modId;
            mods[j][1] = childNum;
            for (int k = 0; k < childNum; ++k)
                cin >> mods[j][k+2];
        }

        int* cnt = new int[N]();
        for (int j = 0; j < M; ++j)
        {
            vector<int> next;
            find(mods, N, init[j], next);
            for (vector<int>::iterator iter = next.begin(); iter != next.end(); ++iter)
            {
                    cnt[*iter]++;
                    get (mods, N, *iter, cnt);
            }
        }

        for (int j = 0; j < N; ++j)
            cout << cnt[j] << " ";
        cout << endl;
    }

}

void get (int** a, int len, int line, int* cnt)
{
    for (int i = 0; i < a[line][1]; ++i)
    {
        vector<int> next;
        find(a, len, a[line][i+2], next);
        for (vector<int>::iterator iter = next.begin(); iter != next.end(); ++iter)
        {
            cnt[*iter]++;
            get(a, len, *iter, cnt);
        }
    }
}

void find (int** a, int len, int b, vector<int>& next)
{
    for (int i = 0; i < len; ++i)
    {
        if (b == a[i][0])
            next.push_back(i);
    }
}

2015微软实习在线笔试题 - Professor Q's Software

时间: 2024-08-02 11:00:34

2015微软实习在线笔试题 - Professor Q's Software的相关文章

2015年阿里研发工程师实习在线笔试题

投递了阿里的C/C++方向研发,昨天晚上参加在线笔试时完全懵了,各种数学题.智力题,以及各种看似风马牛不相及的题目在一起,各位感受下. 题目中涉及到排列组合 基本数学问题等.下面关于以上部分题目给出自己的意见,如果有不对的地方,希望各位指正. 关于数N!有多少个零 思路:给定一个整数N,那么N的阶乘N!末尾有多少个0的问题可以转换为N!乘式中可以分解出多少个5的问题.因为5和其前面的任何一个偶数相乘都会产生0,所以只需求出在由1到N的数中共可以分解出多少个5.例如25!,可以分解出5(1×5).

百度实习在线笔试题【逆序问题】

自己写的代码,未经测试 ////baidu实习岗在线测评 ////一组01的二进制字符串,要求不为逆序,需要交换几次位置. // #include <iostream> #include <vector> using namespace std; typedef struct{ unsigned int num; vector<char*> data; }SInput; void pInputData(SInput &input) { int i; cin>

2015搜狐在线笔试题(内存泄露问题)(转)

以下操作中,可能的解决java内存泄露问题的手段有:[多选]( ) 在程序中调用System.gc(); 关掉不再使用的网络/数据库连接: 在程序中调用finalize(); 清理集合类中的无用对象: 在程序中调用Runtime.getRuntime().runFinalization(): 答案应该为BD首先内存泄露是由于某些无用对象无法回收,对象回收的条件是根据搜素算法不可达,那么哪些可以作为根呢?1.方法区中类静态变量引用的对象2.虚拟机栈帧中引用的对象3.本地方法栈帧中引用的对象4.方法

2015阿里巴巴前端实习生在线笔试题

Summary 大公司开始招实习生了,我也变成过来人了,品味到之前的酸甜苦辣,除了加油好像也没法说那么多. 因为是你在奋斗,心态这件事是你们在掌握的.但是我们唯一能提供的是我们topview实验室新鲜出炉的面经和笔试. (其实我在想有没应届生春招 - -!) Where 2015阿里巴巴前端实习生在线笔试题

2015年阿里巴巴校招研发工程师在线笔试题汇总

在线笔试题汇总 卷一: 1.下面的函数中哪个是系统调用而不是库函数______? printf scanf fgetc read print_s scan_s 2.某足球队有四名外援,分别来自巴西.荷兰.意大利和美国.他们分别擅长前锋.后卫或守门,其中: ① 美国外援单独擅长守门: ② 意大利外援不擅长前锋: ③ 巴西外援和另外某个外援擅长相同的位置: ④ 荷兰外援擅长的位置和巴西外援不同. 以上条件可以推出巴西外援擅长的位置是______. 前锋 守门 后卫 前锋或守门 后卫或守门 前锋或后卫

2015.8.29某高级企业的在线笔试题

收集了今年阿里的在线笔试题,贴出来供需要的朋友参考. 1.下面的函数中哪个是系统调用而不是库函数______?printfscanffgetcreadprint_sscan_s 2.某足球队有四名外援,分别来自巴西.荷兰.意大利和美国.他们分别擅长前锋.后卫或守门,其中:① 美国外援单独擅长守门:② 意大利外援不擅长前锋:③ 巴西外援和另外某个外援擅长相同的位置:④ 荷兰外援擅长的位置和巴西外援不同.以上条件可以推出巴西外援擅长的位置是______.前锋守门后卫前锋或守门后卫或守门前锋或后卫 3

48行代码解一道亚马逊的在线笔试题

这题是我从这里看到的一道亚马逊的在线笔试题,具体规则请前往该文章查看,下面贴出我的解题代码: 其中11,12,13,14分别代表J,Q,K,A; class CardCompare { private int[] cards = new int[] { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }; public bool CompareCards(int[] cards1, int[] cards2) { return cardsScore(card

微软线上笔试-2015-4-3(1,2题) Magic Box &amp;&amp; Professor Q&#39;s Software

写在前面: http://blog.csdn.net/michael_kong_nju/article/details/44872519 关于4.3号的微软线上挑战赛,感觉自己还是刷题刷少了,表现在几个方面:1. 编程经验不足.2. 算法的使用不灵活.所以下面还是要加强OJ的训练, 把Leetcode上的题多做做.后面又把4道题仔细的编写调试了一下,希望和我情况类似的同学也能加紧代码的训练. 1. 第一题的原题是: The circus clown Sunny has a magic box.

2014阿里巴巴WEB前端实习生在线笔试题

2014年3月31日晚,我怀着略微忐忑的心情(第一次在线笔试^_^!!)进行了笔试,阿里巴巴的笔试题共有10道,几乎包含了Web前端开发的各个方面,有程序题.有叙述题,时间非常紧张,只完成了大概6道题.下面把遇到的题目跟大家分享一下! 1. <pre name="code" class="html"><!doctype html> <html> <head> <style type="text/css&