Professor Q's Software Java版本
来源:互联网 发布:如何ping mac地址 编辑:程序博客网 时间:2024/06/08 08:10
题目描述:
#1136 : Professor Q's Software
- 样例输入
33 2123 256123 2 456 256456 3 666 111 256256 1 903 1100100 2 200 200200 1 300200 05 111 2 2 32 2 3 43 2 4 54 2 5 65 2 6 7
- 样例输出
1 1 31 2 21 1 2 3 5
描述
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).
题目分析:
做这道题目时,我首先想到的是深度优先搜索,无奈超时了。改进了一下,将信号数组简化后再次深度遍历,还是超时。我才明白原来方法不对。联系到最近刚看的动态规划,想到本题可以用动态规划来解。总体思想有两个要点:一是填表,以此来避免重复解决子问题;二是自底向上,先去解决最基本的那个问题。总结成一句话就是“自底向上填表法”。
代码如下:
import java.util.*;import java.io.*;public class Main{private static Map<Integer,String> map = new HashMap<Integer,String>();private static String[] active; private static long[] count; private static boolean[] flag; public static void countActive(int num){ count[num]++; String tmpStr = active[num]; if(tmpStr=="") return; String[] tmpArr = tmpStr.split(" "); for(int k=1;k<tmpArr.length;k++){ int tmpNum = Integer.parseInt(tmpArr[k]); count[tmpNum]++; } } public static void depthTravel(int num){ if(flag[num]) return; String tmpStr = active[num]; if(tmpStr==""){ flag[num] = true; return; } String[] tmpArr = tmpStr.split(" ");for(int k=1;k<tmpArr.length;k++){int tmpNum = Integer.parseInt(tmpArr[k]);depthTravel(tmpNum);active[num] = active[num]+active[tmpNum];}flag[num] = true; }public static void main(String[] args){ BufferedReader strin = new BufferedReader(new InputStreamReader(System.in)); try{ String str = strin.readLine();int T = Integer.parseInt(str);for(int i=0;i<T;i++){ String str1 = strin.readLine();String[] strArr1 = str1.split(" ");int N = Integer.parseInt(strArr1[0]);int M = Integer.parseInt(strArr1[1]);String str2 = strin.readLine();String[] strArr2 = str2.split(" ");int[] signals = new int[M];for(int j=0; j<M; j++){signals[j] = Integer.parseInt(strArr2[j]);}map.clear();int[][] module = new int[N][4];for(int j=0; j<N; j++){for(int k=0; k<4; k++){module[j][k] = -1;}String str3 = strin.readLine();String[] strArr3 = str3.split(" ");module[j][0] = Integer.parseInt(strArr3[0]);if(!map.containsKey(module[j][0])){ String tmpStr = ""+j; map.put(module[j][0],tmpStr);}else{ String tmpStr = map.get(module[j][0]); tmpStr = tmpStr+" "+j; map.put(module[j][0], tmpStr);}int len = Integer.parseInt(strArr3[1]);for(int k=1; k<len+1; k++){module[j][k] = Integer.parseInt(strArr3[k+1]);}}active = new String[N];count = new long[N];flag = new boolean[N];for(int j=0;j<N;j++){active[j]="";for(int k=1;k<4;k++){if(module[j][k]==-1)break;if(map.containsKey(module[j][k])){String tmpStr = map.get(module[j][k]);active[j] = active[j]+" "+tmpStr;}}}for(int j=0;j<M;j++){if(map.containsKey(signals[j])){String tmpStr = map.get(signals[j]);String[] tmpArr = tmpStr.split(" ");for(int k=0;k<tmpArr.length;k++){int num = Integer.parseInt(tmpArr[k]);depthTravel(num);countActive(num);}}}if(N > 0){System.out.print(count[0]%142857);}for(int k=1; k<N; k++){System.out.print(" ");System.out.print(count[k]%142857);}System.out.print("\r\n");} }catch(IOException e){ e.printStackTrace(); } }}
代码写的不够简洁,没有优化;但程序是正确的。虽然很早就听说过动态规划,却一直没认真学过。这次算是一次练习和学习吧。还请大家多多指教。
- Professor Q's Software Java版本
- hihocoder #1136 : Professor Q's Software
- 微软笔试-Professor Q's Software
- Professor Q's Software---拓扑排序
- 微软2015实习第二题Professor Q's Software
- 2016微软实习在线笔试题 - Professor Q's Software
- 微软笔试题 HihoCoder#1136: Professor Q's Software 题解
- hihocoder #1136 : Professor Q's Software 数组解法
- 微软2016校园招聘在线笔试 - 第二题 Professor Q's Software
- 微软线上笔试-2015-4-3(1,2题) Magic Box && Professor Q's Software
- Professor Q's Software——邻接表实现拓扑排序
- 题目2 : Professor Q's Software【微软2016校园招聘在线笔试】
- hihoCoder 1136 Professor Q's Software 微软2016校园招聘在线笔试
- 微软2016校园招聘笔试(第一场) Professor Q's Software & Recruitment
- HiHocoder #1136 : Professor Q's Software 微软2016校园招聘在线笔试 【拓扑排序+DP】
- A. Professor GukiZ's Robot
- D-Professor GukiZ's Robot
- CF 70D Professor's task题解
- 天声人語 20150423
- POJ 2051 Argus(优先队列)
- Leetcode16: Number of 1 Bits
- 某ssh后门试用
- Android 5.0 Design 沉浸式状态栏
- Professor Q's Software Java版本
- showModalDialog简单案例及使用
- 欢迎使用CSDN-markdown编辑器
- wamp下 403 Forbidden问题轻松解决
- 一个适配器公用,多个布局文件同用一个适配器
- 最小二乘法建模
- Linux 常用命令
- 日经春秋 20150423
- linux下 c语言多文件编译