此题有大坑
-
坑一:由于字符串不知道多长,因此不能用数组存储(亲自试验,3000不够用,就算够用,开辟一个大数组的耗时也会导致超时)
-
坑二 :由于字符串过长,使用Scanner读字符串也会导致超时
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Scanner;
public class 损坏的RAID5{
static int N = 1010;
static String data[] = new String[N];
static int len[] = new int[N];
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String[] split = br.readLine().split(" ");
int n = Integer.parseInt(split[0]);//磁盘
int s = Integer.parseInt(split[1]);//条带
int l = Integer.parseInt(split[2]);//现有磁盘
for(int i = 0 ; i < l ; i ++) {
String nextLine[] = br.readLine().split(" ");
int diskNum = Integer.parseInt(nextLine[0]);
data[diskNum] = nextLine[1];
len[diskNum] = data[diskNum].length();
}
int m = Integer.parseInt(br.readLine());
while(m-->0) {
int x = Integer.parseInt(br.readLine());
int i = x / ((n - 1) * s);// x在第i行(编号)
int j = x % ((n - 1) * s);// 多了j个
int h = j % s + i * s;// x的高度
int c = (n - 1 - i % n + j / s + 1) % n;// m所在磁盘号
String res = find(h,c,n);
bw.write(res+"\n");
}
bw.flush();
}
private static String find(int h, int c, int n) {
if(len[c]/8-1<h) {//读取不到
return Eor(h,c,n);
}
return data[c].substring(8*h,8*h+8);//直接读
}
private static String Eor(int h, int c,int n) {
int res[] = new int[4];
for(int i = 0 ; i < n ;i ++) {//除了c列,若再出现错误列,返回“-”
if(i==c) continue;
if(len[i]/8-1<h) return "-";
String ss = data[i].substring(8*h,8*h+8);//求异或值
for(int j = 0 ; j < 4; j ++) {
int a = Integer.valueOf(ss.substring(j*2,j*2+2),16);
res[j] ^= a;
}
}
String ans = "";
for(int i = 0 ; i < 4 ; i ++) {
ans+= String.format("%02X", res[i]);
}
return ans;
}
}
网友评论