文章目录
- 1. 题目链接
- 2. 题目代码
- 正确代码
- 3. 题目总结
- 学习
1. 题目链接
Gift Carpet
2. 题目代码
正确代码
#include<iostream>
#include<vector>
using namespace std;
char letterTable[21][21];
int main(){
int testCase;
cin >> testCase;
int row;
int column;
string resultOfCheck;
while(testCase --){
cin >> row >> column;
for(int subscriptOfRow = 1; subscriptOfRow <= row; subscriptOfRow ++){
for(int subscriptOfColumn = 1; subscriptOfColumn <= column; subscriptOfColumn ++){
cin >> letterTable[subscriptOfRow][subscriptOfColumn];
}
}
for(int subscriptOfColumn = 1; subscriptOfColumn <= column; subscriptOfColumn ++){
for(int subscriptOfRow = 1; subscriptOfRow <= row; subscriptOfRow ++){
if(letterTable[subscriptOfRow][subscriptOfColumn] == 'v' && resultOfCheck.find('v') == string::npos){
resultOfCheck = resultOfCheck + 'v';
break;
}else if(letterTable[subscriptOfRow][subscriptOfColumn] == 'i' && resultOfCheck.find('i') == string::npos && resultOfCheck.find('v') != string::npos){
resultOfCheck = resultOfCheck + 'i';
break;
}else if(letterTable[subscriptOfRow][subscriptOfColumn] == 'k'&& resultOfCheck.find('k') == string::npos && resultOfCheck.find('i') != string::npos && resultOfCheck.find('v') != string::npos){
resultOfCheck = resultOfCheck + 'k';
break;
}else if(letterTable[subscriptOfRow][subscriptOfColumn] == 'a'&& resultOfCheck.find('a') == string::npos && resultOfCheck.find('k') != string::npos && resultOfCheck.find('i') != string::npos && resultOfCheck.find('v') != string::npos){
resultOfCheck = resultOfCheck + 'a';
break;
}
}
}
//cout << resultOfCheck << endl;
if(resultOfCheck == "vika"){
cout << "YES" << endl;
}else{
cout << "NO" << endl;
}
resultOfCheck = "";
}
return 0;
}
3. 题目总结
解题时长:1h30min左右
保存"vika"相关字符:
① 最初的思路:使用vector容器,解决去重问题有点麻烦(想过使用find函数,但是没有解决)。
② 中间的思路:使用set容器,弊端在于set容器内的字符不是按插入顺序保存的,难以判断字符是否是按"vika"的顺序
③ 最终的思路:使用字符串保存
注意:一列只能出一个字符
学习
#include <bits/stdc++.h>
#define int long long
using namespace std;
int t, n, m, b, cnt;
string str[21], a = "vika";
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> t;
while (t--) {
cnt = b = 0;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> str[i];
}
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (str[j][i] == a[b]) {
b++, cnt++;
break;
}
if (cnt == 4) cout << "yes\n";
else cout << "no\n";
}
return 0;
}