1406번 : 에디터 - silver 2
#include <iostream>
#include <list>
#include <string>
using namespace std;
int main(void){
string str;
cin>>str;
list<char> l;
for(auto s:str)
l.push_back(s); // list에 문자 순서대로 넣기
auto now = l.end(); // list<int>::iterator cur = l.end()
int q;
cin>>q;
while(q--){ // q만큼 돈다
char c;
cin>>c;
if(c=='P'){ // 왼쪽에 문자 추가
char w;
cin>>w;
l.insert(now, w); // 왼쪽 위치에 w 추가
}else if(c=='L'){
if(now!=l.begin())
now--; // 왼쪽으로 1칸 옮김
}else if(c=='D'){
if(now!=l.end())
now++;//오른쪽으로 1칸 옮김
}else if(c=='B'){ //c=='B' 왼쪽 문자 삭제
if(now!=l.begin()){
now--;
now=l.erase(now);
}
}
}
for(auto itr=l.begin(); itr != l.end(); itr++)
cout << *itr << ' ';
}
1158번 : 요세푸스 문제 - silver 6 // 한번 더 풀기
#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <algorithm>
using namespace std;int main(void){
ios::sync_with_stdio(0);
cin.tie(0);
int n,k;
vector<int>vc,ans;
cin>>n>>k;
for(int i=1;i<=n;i++){
vc.push_back(i);
}
for(int i=0;ans.size()<n;i++){//정답벡터에 n개의 원소가 쌓이면 종료
if(i%k==k-1){
ans.push_back(vc[i]);
}else{
vc.push_back(vc[i]);
}
}
cout <<"<";
for(int i=0;i<n;i++){
if(i==n-1){
cout <<ans[i]; // 마지막 요소는 콤마 없이 출력
}else{
cout<<ans[i]<<", ";
}
}
cout <<">\n";
}
5397번 : 키로거
#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <algorithm>
using namespace std;
int main(void){
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin>>t;
for(int i=0;i<t;i++){
list <char> lst={};
string str;
auto s = lst.begin(); // s는 lst의 첫번째 위치를 가리킴
cin>>str;
for(auto a:str){
if(a=='<'){
if(s!=lst.begin()){ // s가 첫번째가 아닐경우
s--; // <는 왼쪽으로 한칸 이동
}
}
else if(a=='>'){
if(s!=lst.end()){
s++; // >는 오른쪽으로 한칸 이동
}
}else if(a=='-'){
if(s!=lst.begin()){ //커서의 바로 앞에 글자 존재 시 그 글자를 지움
s--;
s=lst.erase(s);
}
}else{
lst.insert(s,a); // s 번째 위치에 a입력
}
}
for(auto l : lst){
cout << l;
}
cout <<"\n";
}
}
'알고리즘 > Algorithm' 카테고리의 다른 글
SWEA 8~9월 풀이 - 1 (0) | 2022.09.18 |
---|---|
baekjoon python 기초 복습.. (0) | 2022.08.09 |
바킹독 알고리즘 문제집 - 배열 (0) | 2022.06.26 |
SW Academy D1 -2 (0) | 2022.05.26 |
SW Academy D1 -1 (0) | 2022.05.25 |