Algorithm/기타(기업등)
[SAP/C++] Remove Space
IagreeBUT
2021. 4. 21. 22:32
728x90
난이도 하
스페이스 제거문제
k개의 연속 알파벳 제거를 스택으로 해결했던 것을 응용해서
큐로 구현해서 스페이스가 아닌건 버리는 방식으로 해봄
#include<bits/stdc++.h>
using namespace std;
class Solution
{
queue <char>q;
string output ="";
public:
string modify (string s)
{
for(int i=0; i<s.size();i++){
if(s[i]!=' '){
q.push(s[i]);
}
}
while (!q.empty()) { // 스택에 쌓여있는 것을 그대로 빼서 붙이기
output += q.front();
q.pop();
}
return output;
}
};
int main()
{
int t;
cin >> t;
cin.ignore();
while(t--)
{
string s;
getline(cin,s);
Solution ob;
cout<<ob.modify(s)<<endl;
}
return 0;
}
728x90