阿里笔试8.26场第一题
贴个纯暴力
没参与笔试
不知道能A多少
测试用例:
input:
4
1 z a
1 a z
2 az bb
3 bbb bbb
1 z a
1 a z
2 az bb
3 bbb bbb
output:
0
24
1
0
0
24
1
0
import java.util.Scanner;
public class Main {
/**
* 比较字典序
* 如果s1<s2 返回true
* 如果s1>s2 返回false
* */
public static boolean cmp(String s1,String s2){
char[] nums1 = s1.toCharArray();
char[] nums2 = s2.toCharArray();
boolean ans = false;
for (int i = 0;i<nums1.length;i++){
if (nums1[i] == nums2[i]){
continue;
}
else if (nums1[i]<nums2[i]){
ans = true;
break;
}
else if (nums1[i]>nums2[i]){
break;
}
}
return ans;
}
public static class Template{
int n;
String s1;
String s2;
public Template(int n, String s1, String s2) {
this.n = n;
this.s1 = s1;
this.s2 = s2;
}
}
//字符串自增1
public static String add(String s){
char[] nums = s.toCharArray();
char first_ch = nums[0];
int len = nums.length;
int ext = 1;
for (int i = len-1;i>=0;i--){
int now = nums[i]+ext;
if (now > 122){
ext = 1;
nums[i] = (char) ((now%122)+97-1);
}
else if (now == 122){
ext = 0;
nums[i] = 'z';
}
else if (now < 122){
ext = 0;
nums[i] = (char) now;
}
}
StringBuffer sb = new StringBuffer();
for (int i = 0;i<len;i++){
if (i == 0 && first_ch == 'z'){
sb.append(first_ch);
continue;
}
sb.append(nums[i]);
}
return sb.toString();
}
public static int count_words(Template template){
String A = template.s1;
String B = template.s2;
if (A.equals(B) || !cmp(A,B)) return 0;
int count = 0;
while (true){
A = add(A);
if (cmp(A,B)){
count++;
}
if (A.equals(B))
break;
}
return count;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
Template[] templates = new Template[t];
for (int i = 0;i<t;i++){
templates[i] = new Template(sc.nextInt(),sc.next(),sc.next());
}
for (Template template:templates){
System.out.println(count_words(template));
}
}
}
查看9道真题和解析