코테연습일지/기타 알고리즘 & 자료구조
연결 리스트
jemin0619
2024. 1. 24. 23:15
https://www.youtube.com/watch?v=C6MX5u7r72E
https://www.acmicpc.net/problem/1406
1406번: 에디터
첫째 줄에는 초기에 편집기에 입력되어 있는 문자열이 주어진다. 이 문자열은 길이가 N이고, 영어 소문자로만 이루어져 있으며, 길이는 100,000을 넘지 않는다. 둘째 줄에는 입력할 명령어의 개수
www.acmicpc.net
#include <bits/stdc++.h>
using namespace std;
int main(){
string text;
int n;
cin>>text>>n;
list<char> L;
for(auto c : text) L.push_back(c);
auto cursor = L.end();
while(n--){
char op;
cin>>op;
if(op=='P'){
char add;
cin>>add;
L.insert(cursor,add);
}
if(op=='L'){
if(cursor!=L.begin()) cursor--;
}
if(op=='D'){
if(cursor!=L.end()) cursor++;
}
if(op=='B'){
if(cursor!=L.begin()){
cursor--;
cursor = L.erase(cursor);
}
}
}
for(auto c : L) cout<<c;
return 0;
}
https://www.acmicpc.net/problem/5397
5397번: 키로거
첫째 줄에 테스트 케이스의 개수가 주어진다. 각 테스트 케이스는 한줄로 이루어져 있고, 강산이가 입력한 순서대로 길이가 L인 문자열이 주어진다. (1 ≤ L ≤ 1,000,000) 강산이가 백스페이스를 입
www.acmicpc.net
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
while(n--){
string text;
cin>>text;
list<char> L;
auto cursor = L.end();
for(char c : text){
if(c=='<'){
if(cursor!=L.begin()) cursor--;
}
else if(c=='>'){
if(cursor!=L.end()) cursor++;
}
else if(c=='-'){
if(cursor!=L.begin()){
cursor--;
cursor=L.erase(cursor);
}
}
else{ //문자가 들어오면
L.insert(cursor,c);
}
}
for(auto c : L) cout<<c;
cout<<"\n";
}
return 0;
}
https://www.acmicpc.net/problem/1158
1158번: 요세푸스 문제
첫째 줄에 N과 K가 빈 칸을 사이에 두고 순서대로 주어진다. (1 ≤ K ≤ N ≤ 5,000)
www.acmicpc.net
//https://velog.io/@jh991012/백준C-1158번-요세푸스-문제
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
list<int> L;
auto cursor = L.begin();
cout << '<';
for (int i = 1; i <= n; i++) L.push_back(i);
while (!L.empty()){
for (int i = 0; i < k; i++){
cursor++;
if (cursor == L.end())
cursor = L.begin();
}
cout << *cursor;
cursor = L.erase(cursor);
cursor--;
if (L.empty()) cout << '>';
else cout << ", ";
}
return 0;
}
연결 리스트 문제는 강의에서도 말씀하신 것처럼 크게 변형이 있지 않는 것 같다고 느꼈다.