-replace KFC->BBQ변환하기
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void replace(string& arr, string before, string after)
{
int bn = before.length();
int an = after.length();
int a = 0;
int b;
while (1)
{
a = arr.find(before, a);
if (a == -1)break;
arr.erase(a, bn);
arr.insert(a, after);
a += an;
}
}
int main()
{
string arr = "BBKFCAAAAAKFCAA";
// KFC 문자열을 BBQ로 바꾼 후 문자열 전체 출력~
// BBBBQAAAAABBQAA
replace(arr, "KFC", "BBQ");
return 0;
}
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
string arr = "ASDFASDF";
string before = "FA";
string changed = "KKK";
arr.replace(arr.find(before), before.length(), changed);
// 바꾸고 싶은 문자열 // 바꾸고 싶은 문자열의 길이 // 바꿀 문자열
return 0;
} // replace 함수 사용법
- split빼고 출력하기
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
string arr = "KFC#MC#MST#BBQ"; //
int a, b;
a = 0;
vector<string>result;
while (1)
{
b = arr.find('#', a);
if (b == -1)
{
b = arr.length();
string temp = arr.substr(a, b - a);
result.push_back(temp);
break;
}
string temp = arr.substr(a, b - a);
result.push_back(temp);
a = b + 1;
}
int t = result.size();
for (int x = 0; x < t; x++)
{
cout << result[x]<<'\n';
}
// KFC
// MC
// MST
// BBQ
return 0;
}
- kkk가 몇개 있는지 출력
#include<iostream>
#include<string>
using namespace std;
int main()
{
string arr = "sd9KKK89hKKK9d8d9f8KKKs9d8f9s"; //abs
string target = "KKK";
int n = target.length();
int a, b;
int cnt = 0;
a = 0;
while (1)
{
a = arr.find(target, a);
if (a == -1)break;
cnt++;
a += n;
}
cout << cnt;
return 0;
}
- 문자열 숫자로 바꾼 후 7 더해서 출력
#include<iostream>
#include<string>
using namespace std;
int findnum(string& arr, int index)
{
int n = arr.length();
for (int x = index; x < n; x++)
{
if (arr[x] >= '0' && arr[x] <= '9')
{
return x;
}
}
return -1;
}
int findchar(string& arr, int index)
{
int n = arr.length();
for (int x = index; x < n; x++)
{
if (!(arr[x] >= '0' && arr[x] <= '9'))
{
return x;
}
}
return -1;
}
int main()
{
string arr = "ABT1031BT1335Aab";
// 0에서 9사이의 문자를 숫자로 바꾼 후
// 7 더해서 출력하기
int a, b;
a = 0;
while (1)
{
a = findnum(arr, a);
if (a == -1)break;
b = findchar(arr, a + 1);
if (b == -1)break;
string result = arr.substr(a, b - a);
cout << stoi(result) + 7 << endl;
a = b + 1;
}
return 0;
}
- 문자열 split
#include<iostream>
using namespace std;
int main()
{
string arr = "AB[426][5T[[[ATS]]]H]A[[K]";
int a, b;
a = 0;
while (1)
{
a = arr.find('[', a);
if (a == -1)break;
b = arr.find(']', a+1);
if (b == -1)break;
while (1)
{
int c = arr.find('[', a + 1);
if (c == -1)break;
if (c < b)a = c;
else break;
}
string result = arr.substr(a + 1, b - a - 1);
cout << result << endl;
a = b + 1;
}
return 0;
}
- 프로그래머스 프린터
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;
int solution(vector<int> priorities, int location) {
int answer = 0;
struct node {
int prio;
int index;
};
queue<node>q;
int t = priorities.size();
for (int x = 0; x < t; x++) // 묶어서 큐 등록
{
q.push({ priorities[x],x });
}
while (!q.empty())
{
int Max = -21e8;
for (int x = 0; x < q.size(); x++) { // 우선순위가 가장 높은것 찾기
if (Max < q.front().prio)
{
Max = q.front().prio;
}
q.push(q.front());
q.pop();
}
if (Max > q.front().prio) { //큐에 프런트가 맥스보다 작으면 뒤로 가시오
q.push(q.front());
q.pop();
}
else // 큐에 프런트가 맥스라면
{
if (location == q.front().index) { // 입력받은 인덱스가 큐에 프런트의 인덱스와 같다면
answer++;
return answer; // 몇번째로 출력되는지 리턴하고
}
else { // 입력받은 인덱스가 큐에 프런트인덱스와 다르다면
q.pop(); // 출력하고
answer++; // 몇번째 증가
}
}
}
return answer;
}
int main()
{
vector<int> priorities = {1,1,9,1,1,1};
int location = 0;
int ret=solution(priorities, location);
cout << ret;
return 0;
}
- 프로그래머스 송전탑
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
vector<int>solu(vector<int> heights)
{
vector<int>answer;
// 뒤에서 부터 반복문 시작
for (int y = heights.size() - 1; y >= 0; y--)
{
int index = 0;
for (int x = y-1; x >= 0; x--) {
if (heights[x] > heights[y]) // 앞에 타워랑 비교 후
{
index = x + 1;
break;
}
}
answer.insert(answer.begin(), index);
}
return answer;
}
int main()
{
vector<int>heights = { 6,9,5,7,4 };
vector<int>result = solu(heights);
int size = result.size();
for (int x = 0; x < size; x++)
{
cout << result[x]<<" ";
}
}
'Algorithm > C++' 카테고리의 다른 글
[21.08.23] BFS/ 슬라이딩윈도우 기본 (0) | 2021.08.23 |
---|---|
[21.08.06]DFS, BFS + 문제풀이 (0) | 2021.08.06 |
[21.08.04]큐/스택 활용하기 (0) | 2021.08.04 |
[21.08.03] dat활용 counting sort와 vector사용법 (0) | 2021.08.03 |
[21.08.02] 재귀함수2/ 백트래킹 (0) | 2021.08.02 |