본문 바로가기

백준

2607 비슷한 단어

ㅇㄴㅠ 이게 몇 번째 실수야

cin.tie 쓸거면 scanf 쓰지 말라고!!!! 

이 문제 때문에 계속 틀렸었다.

 

암튼 경우를 구해보면 

- 한 단어에서 한 문자를 더하거나

- 한 단어에서 한 문자를 빼거나

- 하나의 문자를 다른 문자로 바꾸어 나머지 한 단어와 같은 구성을 갖게 되는 경우

 

그래서 한 문자를 더하거나 빼는 경우를 먼저 확인해주고 그다음으로 문자를 바꾸는 경우를 확인해줬다.

간단한 것 같은데 왜이리 애먹냐

 

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cmath>
#include <cstring>
#include <bitset>

#define xx first
#define yy second
#define all(x) (x).begin(), (x).end()

using namespace std;
using i64 = long long int;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;
using iii = tuple<int, int, int>;

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

	int n;
	cin >> n;

	vector<string> v(n);
	for (int i = 0; i < n; i++)
	{
		cin >> v[i];
	}

	vector<int> word1(26);
	for (int i = 0; i < v[0].size(); i++)
		word1[v[0][i] - 'A']++;

	int ans = 0;
	for (int i = 1; i < n; i++)
	{
		vector<int> word2(26);
		for (int j = 0; j < v[i].size(); j++)
			word2[v[i][j] - 'A']++;

		int diff = 0;
		for (int j = 0; j < word1.size(); j++)
			diff += abs(word1[j] - word2[j]);
		if (diff < 2 && abs((int)v[0].size() - (int)v[i].size()) <= 1)
		{
			ans++;
			continue;
		}

		diff = 0;
		for (int j = 0; j < word1.size(); j++)
		{
			if (abs(word1[j] - word2[j]) >= 2)
				diff++;
			diff += abs(word1[j] - word2[j]);
		}
		if (diff == 2 && v[0].size() == v[i].size())
			ans++;
	}

	cout << ans << endl;

	return 0;
}

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

15970 화살표 그리기  (0) 2020.12.05
1927 최소 힙  (0) 2020.12.05
11660 구간 합 구하기 5  (0) 2020.11.24
20127 Y-수열  (0) 2020.11.24
10816 숫자 카드 2  (0) 2020.11.10