1일1알

백준 1411번 비슷한 단어 C++ 본문

알고리즘

백준 1411번 비슷한 단어 C++

영춘권의달인 2023. 3. 5. 13:28

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

 

1411번: 비슷한 단어

첫째 줄에 단어의 개수 N이 주어진다. 둘째 줄부터 N개의 줄에 한 줄에 하나씩 단어가 주어진다. 단어의 길이는 최대 50이고, N은 100보다 작거나 같은 자연수이다. 모든 단어의 길이는 같고, 중복

www.acmicpc.net

 

문자열끼리 하나씩 전부 검사한다.

 

#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>

using namespace std;
using int64 = long long;

int n;
vector<string> v;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> n;
    v = vector<string>(n);
    int ans = 0;
    for (int i = 0; i < n; i++) cin >> v[i];
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            map<char, char> mp;
            set<char> st;
            string str1 = v[i];
            string str2 = v[j];
            bool sim = true;
            for (int k = 0; k < str1.length(); k++) {
                auto itm = mp.find(str1[k]);
                auto its = st.find(str2[k]);
                if (itm == mp.end()) {
                    if (its != st.end()) {
                        sim = false;
                    }
                    mp[str1[k]] = str2[k];
                    st.insert(str2[k]);
                }
                else {
                    if (mp[str1[k]] == str2[k]) continue;
                    sim = false;
                }
            }
            if (sim) {
                ans++;
            }
        }
    }
    cout << ans;
}