본문 바로가기
백준 문제풀이

[백준][문자열] 9375 패션왕 신해빈 c++구현

by 꽁이꽁설꽁돌 2024. 5. 5.
728x90
반응형

목차

    https://www.acmicpc.net/problem/9375

    문제

     

     

    문제 구현 방향

    문자열을 종류만 빼서 map에 string int pair로 넣은 뒤 수학적 계산을 해주면 되는 문제이다.

     

     

    주의할 점

    //넣어주면 틀리게 된다
    ios_base::sync_with_stdio(false);
    	cin.tie(NULL);
    	cout.tie(NULL);

     

    참고

    https://be-senior-developer.tistory.com/52

     

    [코딩 테스트] C++ 코테용 문자열 팁

    목차 c++을 하다보면 문자열이 까다롭게 느껴진다.. 그래서 정리해 보았다. split이 필요한 경우 #include #include #include using namespace std; vectorv; void split(string input, string delimiter) { long long int pos; string tok

    be-senior-developer.tistory.com

     

    코드 구현

    #include <iostream>
    #include<vector>
    #include<math.h>
    #include<map>
    #include<queue>
    #include<algorithm>
    #include<string>
    using namespace std;
    
    map<string, int> m;
    vector<int> v;
    void split(string str, string delimeter) {  //문자열을 잘라주는 함수
    	long long int pos;
    	string token="";
    	while ((pos = str.find(delimeter)) != string::npos) {
    		token = str.substr(0, pos);
    		str.erase(0, pos + delimeter.length());
    	}
    	if (m[str]) {
    		m[str]++;
    	}
    	else {
    		m[str] = 1;
    	}
    }
    
    int main() {
    
    	int T, n, sum=1;
    	string str;
    	cin >> T;
    	if (T == 0) {  //테스트케이스가 0일 경우는 0 출력 후 종료
    		cout << 0;
    		return 0 ;
    	}
    	for (int i = 0; i < T; i++) {
    		cin >> n;
    		getchar();
    		sum = 1;  //초기화 필수
    		m.clear();
    		for (int j = 0; j < n; j++) {
    			getline(cin, str);
    			split(str, " ");
    		}
    		for (auto k = m.begin(); k != m.end(); k++) {
    			sum *= (k->second + 1);  //안입었을 경우를 생각해 +1해서 곱해준다
    		}
    		cout << sum - 1<<"\n";  // 모두 다 안입었을 경우를 빼준다.
    	
    	}
    
    }

     

    반응형