Open Hong-JinSuk opened 1 year ago
문제를 읽고 이해한 뒤, 아래 코드를 실행해보시면 전체적으로 이해가 될겁니다. 여기서 두번째 조건인 R~T>2 에서 RNFAT가 삭제되는데, 왜 삭제가 되는걸까요... 거리는 3이 나오고 2보다 커서 삭제될일이 없을거 같은데요... ㅠㅠ
#include <string> #include <vector> #include<iostream> #include<sstream> #include<string.h> #include<cmath> using namespace std; vector<char> Kakao = { 'A','C','F','J','M','N','R','T'}; vector<string> All = {"RNFAT", "NFRTA", "RNAFT"}; // 예시로 해둔거에용 int solution(int n, vector<string> data) { for (int i = 0; i < n; i++) { // 조건만큼 반복 for (int j = 0; j < All.size(); j++) { cout << endl; int f_i = All[j].find(data[i][0]); // ex) 첫번째 예시에서는 N -> 1 int s_i = All[j].find(data[i][2]); // ex) 첫번째 예시에서는 F -> 2 int Abs = abs(s_i - f_i) - 1; // 두 캐릭터 사이의 거리 cout << "첫번째 캐릭터 위치, 두번째 캐릭터 위치 = " << f_i << ", " << s_i << ", 두 캐릭터 사이의 거리 = " << Abs << endl; cout << "현재 검사하는 배치 : " << All[j] << ", 조건 : " << data[i] << endl; if (data[i][3] == '=') // i번째 조건의 부호 { cout << "="; if (Abs == 0) cout << "문제없음" << endl; if (Abs != 0) { // 붙어있지 않으면 cout << "erase, "; cout << All[j] << endl; All.erase(All.begin() + j); j--; // 지워주고 j를 하나 빼준다. why? 삭제되면 size가 작아져서 } } else if (data[i][3] == '>') { cout << ">"; if (Abs > data[i][4]) cout << "문제없음" << endl; if (Abs <= data[i][4]) { // 거리가 가까우면 삭제 cout << "erase, "; cout << All[j] << endl; All.erase(All.begin() + j); j--; } } else if (data[i][3] == '<') { cout << "<"; if (Abs < data[i][4]) cout << "문제없음" << endl; if (Abs >= data[i][4]) { // 거리가 멀면 삭제 cout << "erase, "; cout << All[j] << endl; All.erase(All.begin() + j); j--; } } } } int answer = All.size(); cout << answer; return answer; } int main() { vector<string> Data = { "N~F=0", "R~T>2" }; int n = 2; cout << All.size() << endl; // 모든 문자열을 만들었다 치자 // 그게 All임. solution(n, Data); return 0; }
data[i][0] 의 자료형이 int 형이아니라 char이네요 현재 코드에서는 아스키코드값과 정수를 비교하고있어서 해당 문제가 발생합니다.
문제를 읽고 이해한 뒤, 아래 코드를 실행해보시면 전체적으로 이해가 될겁니다. 여기서 두번째 조건인 R~T>2 에서 RNFAT가 삭제되는데, 왜 삭제가 되는걸까요... 거리는 3이 나오고 2보다 커서 삭제될일이 없을거 같은데요... ㅠㅠ