본문 바로가기
알고리즘/백준 문제

2607 - 비슷한 단어

by HDobby 2023. 3. 6.

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

 

2607번: 비슷한 단어

첫째 줄에는 단어의 개수가 주어지고 둘째 줄부터는 한 줄에 하나씩 단어가 주어진다. 모든 단어는 영문 알파벳 대문자로 이루어져 있다. 단어의 개수는 100개 이하이며, 각 단어의 길이는 10 이

www.acmicpc.net

생각해 보기

  • 조건을 세세하게 나누면 4가지 입니다.
  • 길이가 같다.
    • 문자 종류가 같은가?
    • 문자가 하나 차이 나는가?
  • 길이가 하나 차이난다.
    • 문자 매핑 카운트가 하나 차이나는가?
  • 길이가 둘 이상 차이나면 무조건 2개 이상의 변화가 필요함으로 넘겨줍시다.

코드

#include<iostream>
#include<map>
#include<cmath>

using namespace std;

int N;
int fcnt[30];
int tcnt[30];
string first;
string tmp;

void input(){
    cin>>N>>first;
    for(int i=0,len=first.length();i<len;++i)
        ++fcnt[first[i]-'A'];
}

bool chkstr(){
    if(abs((int)tmp.length() - (int)first.length()) > 1)
        return false;

    int dcnt=0;
    for(int i=0,len=tmp.length();i<len;++i){
        ++tcnt[tmp[i]-'A'];
    }
    
    for(int i=0;i<='Z'-'A';++i){
        dcnt+=abs(tcnt[i]-fcnt[i]);
    }

    if(dcnt<=1)
        return true;
    
    if(dcnt==2&&tmp.length() == first.length())
        return true;

    return false;
}

void solution(){
    int cnt=0;
    for(int i=0;i<N-1;++i){
        fill(&tcnt[0],&tcnt[30],0);
        cin>>tmp;
        
        if(chkstr())
            ++cnt;
    }
    cout<<cnt<<'\n';
}

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

    input();
    solution();

    return 0;
}

728x90

'알고리즘 > 백준 문제' 카테고리의 다른 글

20310 - 타노스  (0) 2023.03.07
3758 - KCPC  (0) 2023.03.06
17484 - 진우의 달 여행(Small)  (0) 2023.03.03
19441 - 햄버거 분배  (0) 2023.03.03
1515 - 수 이어 쓰기  (0) 2023.03.02

댓글