日期:2025年12月25日
ZeroJudge 題目連結:a221. 11734 - Big Number of Teams will Solve This
解題想法
自訂檢查兩個字串 s、t 是否完全相同的函式 check,如果 s、t 完全相同,印出 Yes 並離開函式;接下來將 s、t 的空格移除並存成另外兩個字串 sp、tp,如果 s、t 的長度不相等、但是 sp、tp 相同,印出 Output Format Error 並離開函式;如果 sp、tp 不相同,印出 Wrong Answer 並離開函式。
Python 程式碼
使用時間約為 19 ms,記憶體約為 3.3 MB,通過測試。
def check(s, t):
if s == t: # 完全相同
print("Yes") # 印出 Yes,離開函式
return
sp = s.replace(" ", "") # 移除空格
tp = t.replace(" ", "")
n, m = len(s), len(t) # 移除空格前的字串長度
if sp == tp and n != m: # 如果 sp、tp 相同,但是原始長度不同
print("Output Format Error") # 印出 Output Format Error,離開函式
return
if sp != tp: # 如果 sp、tp 不相同
print("Wrong Answer") # 印出 Wrong Answer,離開函式
return
t = int(input())
for i in range(1, t+1):
print(f"Case {i:d}:", end=" ")
check(input().rstrip(), input().rstrip()) # 刪掉結尾的空格
C++ 程式碼
使用時間約為 2 ms,記憶體約為 328 kB,通過測試。
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
string rstrip(string s) {
int n = (int)s.size(), end = -1;
for(int i=n-1; i>=0; i--) {
if (!isspace(s[i])) {
end = i;
break;
}
}
return s.substr(0, end+1);
}
string rmspace(string s) {
string t = "";
for(char c : s) {
if (!isspace(c)) t += c;
}
return t;
}
void check(string s, string t) {
if (s == t) { // 完全相同
cout << "Yes\n"; // 印出 Yes,離開函式
return;
}
string sp = rmspace(s), tp = rmspace(t); // 移除空格
int n = (int)s.size(), m = (int)t.size(); // 移除空格前的字串長度
if (sp == tp && n != m) { // 如果 sp、tp 相同,但是原始長度不同
cout << "Output Format Error\n"; // 印出 Output Format Error,離開函式
return;
}
if (sp != tp) { // 如果 sp、tp 不相同
cout << "Wrong Answer\n"; // 印出 Wrong Answer,離開函式
return;
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int T; cin >> T; cin.ignore();
for(int i=1; i<=T; i++) {
cout << "Case " << i << ": ";
string s, t;
getline(cin, s);
getline(cin, t);
s = rstrip(s);
t = rstrip(t);
check(s, t);
}
return 0;
}
沒有留言:
張貼留言