백준 2607

HR·2022년 6월 2일
0

알고리즘 문제풀이

목록 보기
36/50

백준 2607 : 비슷한 단어

  1. 기준 문자열의 알파벳을 앞에서부터 차례대로
  2. 입력받은 문자열에서 찾아서 하나씩 지워간다.
  3. 입력받은 문자열에 남은 알파벳이 1개면 비슷한 단어!

정답 코드

#include <iostream>
#include <algorithm>

using namespace std;

int n;
string base;
int ans;

int main() {	
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
	cin>>n>>base;
	int len = base.size();
	
	for(int i=0; i<n-1; i++) {
		string comp;
		cin>>comp;
		int compLen = comp.size();
		
		int cnt=0;
		for(int j=0; j<len; j++) {
			for(int k=0; k<compLen; k++) {
				 if(base[j] == comp[k]) {
				 	cnt++;
				 	comp[k] = ' ';
				 	break;
				 }
			}
		}
		
		if(compLen-cnt<=1 && len-cnt<=1) {
			ans++;
		}
	}	
	
	
	cout<<ans<<'\n';
	
	
	return 0;
}

처음에 compLen-cnt<=1 조건만 넣었다가 틀렸다. 조건을 꼼꼼하게 비교하자!

0개의 댓글