CCF-损坏的RAID5-201903-3

import java.io.*; 
import java.util.StringTokenizer; 

public class Main {
      
	private static String base = "0123456789ABCDEF";
	private static char toHex(int i) {
     
		return base.charAt(i);
	}

	private static int toDec(char c) {
     
		if (c >= '0' && c <= '9') {
     
			return c - '0';
		}
		return c - 'A' + 10;
	}

    public static void main(String[] args) throws IOException {
      
        Scanner sc = new Scanner(System.in); 
        PrintWriter out = new PrintWriter(System.out);
        int n = sc.nextInt(); // 硬盘数目
        int s = sc.nextInt(); // 条带大小
        int l = sc.nextInt(); // 现存硬盘数目
        int size = 0;
        String[] disk = new String[n];
        for (int i = 0; i < l; i++) {
     
        	int id = sc.nextInt();
        	disk[id] = sc.next();
        	size = disk[id].length() / 8;
        }
        // 读取操作的数目
        int m = sc.nextInt();
        while (m-- > 0) {
     
        	int b = sc.nextInt(); // 要读取的块号
        	// 指定的块超出阵列总长度
        	if ((n - 1) * size <= b) {
     
        		out.println("-");
        		continue;
        	}
        	int x = b / (s * (n - 1)); // 从上往下第x个条带
        	int i = x * s + b % s; // 第i行
        	int j = (b / s) % n; // 第j列
        	// System.out.println(i + " " + j);
        	// 阵列不完整,且所在硬盘缺失,且无法推算出来
        	if (disk[j] != null) {
     
        		for (int index = i * 8; index < (i + 1) * 8; index++) {
     
	    			out.print(disk[j].charAt(index));
	    		}
	    		out.println();
        	}
        	else if (n - l > 1) {
     
        		out.println("-");
        	}
        	else {
     
        	// 硬盘缺失,将其推算出来
        		for (int index = i * 8; index < (i + 1) * 8; index++) {
     
        			int ans = 0;
	        		for (int k = 0; k < n; k++) {
     
	        			// 求XOR
	        			if (k != j) {
     
	        				ans ^= toDec(disk[k].charAt(index));
	        			}
	        		}
	    			out.print(toHex(ans));
	    		}
	    		out.println();
        		continue;
        	}
        }
		out.flush();
    }
} 

class Scanner {
     
	private BufferedReader reader;
	private StringTokenizer st;

	public Scanner(InputStream stream) {
     
		reader = new BufferedReader(new InputStreamReader(stream));
		st = null;
	}

	public String next() {
     
		while(st == null || !st.hasMoreTokens()){
     
			try {
     
				String line  = reader.readLine();
				if (line == null) return null;
				st =  new StringTokenizer(line);
			} catch (Exception e) {
     
				throw (new RuntimeException());
			}
		}
		return st.nextToken();
	}

	public int nextInt() {
     
		return Integer.parseInt(next());
	}

	public long nextLong() {
     
		return Long.parseLong(next());
	}
}

你可能感兴趣的:(CCF-损坏的RAID5-201903-3)