Vijos:字符串还原
java代码:
import java.util.Scanner;
//字符串还原
public class Main {
//输入项
static int n;
//输出结果
static String strFinal;
static final String arrIndex = "abcdefghijklmnopqrstuvwxyz";
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
n = in.nextInt();
final String str1 = in.next();
final String str2 = in.next();
final String str3 = in.next();
String s0;
String s1;
String s2;
String s3;
String s4;
String s5;
//判断
for(int i=0;i<=6;i++) {
//赋值
s0 = getChanged(str1, i);
s1 = getChanged2(str1, i);
s2 = getChanged(str2, i);
s3 = getChanged2(str2, i);
s4 = getChanged(str3, i);
s5 = getChanged2(str3, i);
//检测
if(s0.equals(s3) || s0.equals(s5)) {
strFinal = s0;
break;
}else if(s1.equals(s2) || s1.equals(s4)) {
strFinal = s1;
break;
}else if(s2.equals(s5)) {
strFinal = s2;
break;
}else if(s3.equals(s4)) {
strFinal = s3;
break;
}
}
//输出最终结果
System.out.println(strFinal);
}
//向前取值
public static String getChanged(String str,int i) {
char arr[] = str.toCharArray();
for(int j=0;j<arr.length;j++) {
int m = (int)arr[j]-96-i;
if(m<1) {
arr[j] = arrIndex.toCharArray()[arrIndex.length()-1+m];
}else {
arr[j] = arrIndex.toCharArray()[m-1];
}
}
return String.valueOf(arr);
}
//向后取值
public static String getChanged2(String str,int i) {
char arr2[] = str.toCharArray();
for(int j=0;j<arr2.length;j++) {
int m = (int)arr2[j]-96+i;
if(m>26) {
arr2[j] = arrIndex.toCharArray()[m-27];
}else {
arr2[j] = arrIndex.toCharArray()[m-1];
}
}
return String.valueOf(arr2);
}
}
在这里我把向前向后两种方法分开了,当然你写在一个函数也是比较清晰,不影响答案。