我的PAT-BASIC代码仓:https://github.com/617076674/PAT-BASIC
我的PAT-ADVANCED代码仓:https://github.com/617076674/PAT-ADVANCED
原题链接:
PAT-BASIC1014:https://pintia.cn/problem-sets/994805260223102976/problems/994805308755394560
PAT-ADVANCED1061:https://pintia.cn/problem-sets/994805342720868352/problems/994805411985604608
题目描述:
PAT-BASIC1014:
PAT-ADVANCED1061:
知识点:字符串
思路:根据题给规则一一破解
本题有两个坑点:
(1)“第 1 对相同的大写英文字母(大小写有区分)是第 4 个字母 D
”这句话隐含的条件是第一个大写的英文字母是A~G。
(2)“第 2 对相同的字符是 E
”,这里第2对相同的字符,是指“第 1 对相同的大写英文字母(大小写有区分)”之后的第一对相同的字符,而且限定该相同的字符必须是0~9或A~N。
时间复杂度为O(n),其中n为输入字符串中的最长长度。空间复杂度是O(1)。
本题提供的JAVA代码能够获得通过,但有时会超时。
C++代码:
#include<iostream>
#include<string>
using namespace std;
int main() {
string s1;
string s2;
string s3;
string s4;
string weeks[] = { "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN" };
cin >> s1 >> s2 >> s3 >> s4;
int index = -1;
for (int i = 0; i < s1.length(); i++) {
if (i >= s2.length()) {
break;
}
if (s1[i] >= 'A' && s1[i] <= 'G' && s1[i] == s2[i]) {
index = i;
cout << weeks[s1[i] - 'A'] << " ";
break;
}
}
for (int i = index + 1; i < s1.length(); i++) {
if (i >= s2.length()) {
break;
}
if (((s1[i] >= '0' && s1[i] <= '9') || (s1[i] >= 'A' && s1[i] <= 'N')) && s1[i] == s2[i]){
if ((s1[i] >= '0' && s1[i] <= '9')) {
cout << "0" << s1[i] << ":";
break;
} else {
int hour = s1[i] - 'A' + 10;
cout << hour << ":";
}
break;
}
}
for (int i = 0; i < s3.length(); i++) {
if (i >= s4.length()) {
break;
}
if (((s3[i] >= 'a' && s3[i] <= 'z') || (s3[i] >= 'A' && s3[i] <= 'Z')) && s3[i] == s4[i]) {
if (i <= 9) {
cout << "0" << i;
} else {
cout << i;
}
break;
}
}
}
C++解题报告:
JAVA代码:
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String string1 = scanner.nextLine();
String string2 = scanner.nextLine();
String string3 = scanner.nextLine();
String string4 = scanner.nextLine();
String[] weeks = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
int count = 0;
int index = 0;
for (int i = 0; i < string1.length(); i++) {
if(string1.charAt(i) >= 'A' && string1.charAt(i) <= 'G' && i < string2.length() && string1.charAt(i) == string2.charAt(i)){
count++;
if(count == 1){
index = i;
System.out.print(weeks[string1.charAt(i) - 'A'] + " ");
break;
}
}
}
for (int i = index + 1; i < string1.length(); i++) {
if(i < string2.length() && string1.charAt(i) == string2.charAt(i)){
if(string1.charAt(i) >= 'A' && string1.charAt(i) <= 'N'){
int hour = string1.charAt(i) - 'A' + 10;
System.out.print(hour + ":");
break;
}else if(string1.charAt(i) >= '0' && string1.charAt(i) <= '9') {
System.out.print("0" + string1.charAt(i) + ":");
break;
}
}
}
for (int i = 0; i < string3.length(); i++) {
if(((string3.charAt(i) >= 'a' && string3.charAt(i) <= 'z') || (string3.charAt(i) >= 'A' && string3.charAt(i) <= 'Z'))
&& i < string4.length() && string3.charAt(i) == string4.charAt(i)){
if(i <= 9){
System.out.print("0" + i);
}else{
System.out.print(i);
}
break;
}
}
}
}
JAVA解题报告: