본문 바로가기

백준

1411 비슷한 단어

아니 숌스럽다가 대체 뭐야~?~?

 

문제 이해 못해서 다른 블로그 글 보고 이해했다.

 

두 개의 다른 알파벳을 하나의 알파벳으로 바꿀 수 없지만, 자기 자신으로 바꾸는 것은 가능하다.

 

이 문장 떄문에 애먹었음

 

적당히 해석해보니 aa -> ab 바뀌는 경우를 보면

a를 a로 바꾸는 규칙을 세웠는데 두번째 알파벳은 a를 b로 바꾸고 있으니 규칙에 맞지 않는다.

 

이렇게 생각하고 구현하니 문제가 생겼다.

ab -> aa로 바꾸는 경우는 맞다고 판단해버린다.

 

그래서 배열을 두 개를 둬서 확인하도록 구현했다. 

 

 

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define xx first
#define yy second
#define all(x) (x).begin(), (x).end()
#pragma warning(disable:4996)
 
using namespace std;
using i64 = long long;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;
 
int main() {
    
    int n;
    scanf("%d", &n);
    
    vector<string> str(n);
    for (int i = 0; i < n; i++)
        cin >> str[i];
    
    int ans = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
            string str1 = str[i];
            string str2 = str[j];
            
            if (str1.size() != str2.size())
                continue;
            
            vector<int> alpha1(26, -1);
            vector<int> alpha2(26, -1);
            
            bool is_shong = true;
            for (int k = 0; k < str1.size(); k++)
            {
                if (alpha1[str1[k] - 'a'] == -1 && alpha2[str2[k] - 'a'] == -1)
                {
                    alpha1[str1[k] - 'a'] = str2[k] - 'a';
                    alpha2[str2[k] - 'a'] = str1[k] - 'a';
                    continue;
                }
                
                if (alpha1[str1[k] - 'a'] != str2[k] - 'a' ||
                    alpha2[str2[k] - 'a'] != str1[k] - 'a')
                {
                    is_shong = false;
                    break;
                }
            }
            
            if (is_shong)
                ans++;
        }
    }
    
    cout << ans << endl;
    
    return 0;
}

'백준' 카테고리의 다른 글

2608 로마 숫자  (0) 2020.09.06
15989 1, 2, 3더하기 4  (0) 2020.09.06
3036 링  (0) 2020.09.03
14717 앉았다  (2) 2020.09.02
1064 평행사변형  (0) 2020.08.31