20291:파일 정리

computer_log·2023년 9월 8일
0

백준

  1. erase 활용하기
  2. 결과 배열 만들어주기 전에, 정렬하기
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int T;
struct Node {
	int cnt;
	string str;
};
vector<string>v;
vector<Node>result;
string temp;
int cnt,N = 0;
int main() {
	freopen_s(new FILE*, "input.txt", "r", stdin);
	cin >> T;
	while (T--) {
		string str;
		cin >> str;
		int a = str.find('.');
		str = str.erase(0, a + 1);
		v.push_back(str);
	}

	sort(v.begin(), v.end());
	//정렬을 했기때문에,!!!!!!-->이부분 중요!!!
	temp = v[0];//초기값 설정
	for (int i = 0; i < v.size(); i++) {
		//1.만약에 같다 그럼 cnt추가해준다.
		if (temp == v[i]) {
			cnt++;
		}
		else if (temp != v[i]) {
			//2. 만약에 다르다, 그럼일단 result에 넣어준다.
			result.push_back({ cnt,temp });
			temp = v[i];
			cnt = 1;
		}
	}
	result.push_back({ cnt,temp });
	for (int i = 0; i < result.size(); i++) {
		cout << result[i].str << " " << result[i].cnt << "\n";
	}


	return 0;
}
profile
computer_log

0개의 댓글