-
[Algorithm] stringstream tokenizerAlgorithm 2020. 4. 3. 22:47
stl의 stringstream을 사용할 때
delimiter가 공백 문자(" ")일 경우엔 사용이 쉬웠지만 다른 문자일 경우 이렇게 사용하였다는것을 잊지 않으려고 포스팅.
문제 : 프로그래머스 방금그곡
https://programmers.co.kr/learn/courses/30/lessons/17683
#include <stdio.h> #include <string.h> #include <string> #include <vector> #include <algorithm> #include <iostream> #include <map> #include <set> #include <math.h> #include <stack> #include <queue> #include <sstream> using namespace std; map<string, string> played; map<string, int> order; string solution(string m, vector<string> musicinfos) { string answer = ""; for(int i = 0; i < musicinfos.size(); i++){ stringstream ss(musicinfos[i]); string tmp, start, end, title, note; vector<string> v; while(getline(ss, tmp, ',')){ v.push_back(tmp); } start = v[0]; end = v[1]; title = v[2]; note = v[3]; order.insert(pair<string, int>(title, i)); v.clear(); ss.clear(); ss.str(start); while(getline(ss, tmp, ':')){ v.push_back(tmp); } int stime = stoi(v[0])*60 + stoi(v[1]); v.clear(); ss.clear(); ss.str(end); while(getline(ss, tmp, ':')){ v.push_back(tmp); } int etime = stoi(v[0])*60 + stoi(v[1]); int ttime = etime - stime; tmp = ""; int idx = 0; for(int i = 0; i < ttime; i++){ if(idx >= note.size()) idx = 0; if(note[idx+1] == '#'){ if(note[idx] == 'C'){ tmp += 'O'; idx += 2; } else if(note[idx] == 'D'){ tmp += 'P'; idx += 2; } else if(note[idx] == 'F'){ tmp += 'Q'; idx += 2; } else if(note[idx] == 'G'){ tmp += 'R'; idx += 2; } else if(note[idx] == 'A'){ tmp += 'S'; idx += 2; } else if(note[idx] == 'E'){ tmp += 'T'; idx += 2; } } else tmp += note[idx++]; } played.insert(pair<string, string>(title, tmp)); cout << title << " " << tmp << endl; } string nm = ""; for(int i = 0; i < m.size(); i++){ if(m[i+1] == '#'){ if(m[i] == 'C'){ nm += 'O'; i += 1; } else if(m[i] == 'D'){ nm += 'P'; i += 1; } else if(m[i] == 'F'){ nm += 'Q'; i += 1; } else if(m[i] == 'G'){ nm += 'R'; i += 1; } else if(m[i] == 'A'){ nm += 'S'; i += 1; } else if(m[i] == 'E'){ nm += 'E'; i += 1; } } else nm += m[i]; } cout << "nm : " << nm << endl; answer = "(None)"; int MAX = -2e9; int minOrder = 2e9; for(auto it : played){ string cur = it.second; int pos = cur.find(nm); if(pos == -1) continue; // cout << cur << endl; // cout << cur[pos+m.size()] << endl; if((int)cur.size() > MAX){ answer = it.first; MAX = cur.size(); minOrder = order[it.first]; } else if((int)cur.size() == MAX && order[it.first] < minOrder){ minOrder = order[it.first]; answer = it.first; } } return answer; }
'Algorithm' 카테고리의 다른 글
[Algorithm] 프로그래머스 레벨3 N으로 표현 (0) 2020.04.06 [Algorithm]문자열 해싱 (0) 2020.03.01