티스토리 뷰

출처 : https://www.hackerrank.com/challenges/the-grid-search/problem


#include <bits/stdc++.h>


using namespace std;


vector<string> split_string(string);


string gridSearch(vector<string> G, vector<string> P, int R, int r, int C, int c) {

for (int i = 0; i <= R - r; i++) {

for (int j = 0; j <= C - c; j++) {

bool chk = false;

for (int n = 0; n < r; n++) {

for (int m = 0; m < c; m++) {

if (G[i + n][j + m] != P[n][m]) {

chk = true; break;

}

}

if (chk) break;

}

if (!chk) return "YES";

}

}

return "NO";

}


int main()

{

ofstream fout(getenv("OUTPUT_PATH"));


int t;

cin >> t;

cin.ignore(numeric_limits<streamsize>::max(), '\n');


for (int t_itr = 0; t_itr < t; t_itr++) {

string RC_temp;

getline(cin, RC_temp);


vector<string> RC = split_string(RC_temp);


int R = stoi(RC[0]);


int C = stoi(RC[1]);


vector<string> G(R);


for (int i = 0; i < R; i++) {

string G_item;

getline(cin, G_item);


G[i] = G_item;

}


string rc_temp;

getline(cin, rc_temp);


vector<string> rc = split_string(rc_temp);


int r = stoi(rc[0]);


int c = stoi(rc[1]);


vector<string> P(r);


for (int i = 0; i < r; i++) {

string P_item;

getline(cin, P_item);


P[i] = P_item;

}


string result = gridSearch(G, P,R,r,C,c);


fout << result << "\n";

}


fout.close();


return 0;

}


vector<string> split_string(string input_string) {

string::iterator new_end = unique(input_string.begin(), input_string.end(), [](const char &x, const char &y) {

return x == y and x == ' ';

});


input_string.erase(new_end, input_string.end());


while (input_string[input_string.length() - 1] == ' ') {

input_string.pop_back();

}


vector<string> splits;

char delimiter = ' ';


size_t i = 0;

size_t pos = input_string.find(delimiter);


while (pos != string::npos) {

splits.push_back(input_string.substr(i, pos - i));


i = pos + 1;

pos = input_string.find(delimiter, i);

}


splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));


return splits;

}



'알고리즘 > 기타' 카테고리의 다른 글

[HackerRank] Connected Cells in a Grid  (0) 2018.10.02
[HackerRank] Lily's Homework  (0) 2018.10.02
[HackerRank] The Time in Words  (0) 2018.10.01
[HackerRank] Bigger is Greater  (0) 2018.10.01
[HackerRank] Organizing Containers of Balls  (0) 2018.10.01
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함