ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [ 6996 ] 애너그램 - array, vector
    알고리즘/BOJ 2021. 7. 27. 16:20

    < array, vector > - < solve등급 >

     

    [ 6996 ]

    애너그램

    두 단어 A와 B가 주어졌을 때, A에 속하는 알파벳의 순서를 바꾸어서 B를 만들 수 있다면, A와 B를 애너그램이라고 한다.

    두 단어가 애너그램인지 아닌지 구하는 프로그램을 작성하시오.

     

    :: 입력

    첫째 줄에 테스트 케이스의 개수(<100)가 주어진다. 각 테스트 케이스는 한 줄로 이루어져 있고, 길이가 100을 넘지 않는 단어가 공백으로 구분되어서 주어진다. 단어는 알파벳 소문자로만 이루어져 있다.

    :: 출력

    각 테스트 케이스마다 애너그램인지 아닌지를 예체 출력과 같은 형식으로 출력한다. 


     

    - try 1 

    : break 쓸 자리에 continue 써서 틀림

     

      try1  코드  

    #include <string>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    
    using namespace std;
    
    #define endl "\n"
    
    int main() {
    	int n = 0;
    	
    	cin >> n;
    	
    	string a, b;
    
    	while (n--) {
    
    		int alpa1[26] = { 0, };
    		int alpa2[26] = { 0, };
    
    		cin >> a >> b;
    
    		if (a.length() != b.length()) {
    			cout << a << " & " << b << " are NOT anagrams." << endl;
    			continue;
    		}
    		else {
    			for (int i = 0; i < a.length(); i++) {
    				alpa1[a[i] - 97]++;
    				alpa2[b[i] - 97]++;
    			}
    		}
    
    		for (int i = 0; i < 26; i++) {
    			if (alpa1[i] != alpa2[i]) {
    				cout << a << " & " << b << " are NOT anagrams." << endl;
    				continue; ////////// 틀린 부분 !!!!!!!
    			}
    		}
    
    		cout << a << " & " << b << " are anagrams." << endl;
    		continue;
    	}
    }

     

    - try 2 

    : continue를 break으로 고쳤는데도 틀림 ,, 

     

      try 2  코드  

    위의 코드에서 틀린 부분 고쳐서 제출

     

    - try 3

    : 연산이 복잡해서 틀렸나 싶었는데 그게 아니고 

    입쳑 1 출력 1 입력 1 출력 1 이런 방식으로 했는데

    이게 틀렸나 싶어서

    입력으로 들어오는 단어를 다 저장하고 결과도 다 저장해서

    입력이 모두 끝난후에 순서대로 출력해주니까 맞음 !!

     

    벡터를 너무 남발하는 경향이 있나 싶음 ,, 편해서 자꾸 쓰게 됨

     

     

      정답  코드  

    #include <string>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    
    using namespace std;
    
    #define endl "\n"
    
    int main() {
    	int n = 0;
    	
    	cin >> n;
    
    	string word_a, word_b;
    	vector<int> result;
    	vector<string> word_;
    	vector<string> word__;
    
    	while (n--) {
    		int a[26] = { 0, };
    		int b[26] = { 0, };
    
    		cin >> word_a >> word_b;
    
    		word_.push_back(word_a);
    		word__.push_back(word_b);
    
    		if (word_a.length() != word_b.length()) { // 길이 다른 경우
    			result.push_back(0);
    			continue;
    		}
    
    		for (int i = 0; i < word_a.length(); i++) { // 알파벳 갯수 저장
    			a[word_a[i] - 97] += 1;
    			b[word_b[i] - 97] += 1;
    		}
    
    		bool isAn = true;
    
    		for (int i = 0; i < 26; i++) {
    			if (a[i] != b[i]) {
    				isAn = false;
    				break;
    			}
    		}
    
    		if (isAn) { result.push_back(1); }
    		else result.push_back(0);
    	}
    
    	for (int i = 0; i < result.size(); i++) {
    		if (result[i] == 1) {
    			cout << word_[i] << " & " << word__[i] << " are anagrams.\n";
    		}else cout << word_[i] << " & " << word__[i] << " are NOT anagrams.\n";
    	}
    }

     

      정답  코드  

    #include <cstdio>
    #include <cstring>
    
    int main(void)
    {
    	int T;
    	scanf("%d", &T);
    	for (int i = 0; i < T; i++)
    	{
    		int A[26] = { 0 }, B[26] = { 0 };
    		char a[101], b[101];
    		scanf("%s %s", a, b);
    		if (strlen(a) != strlen(b))
    		{
    			printf("%s & %s are NOT anagrams.\n", a, b);
    			continue;
    		}
    
    		int aa = strlen(a);
    		for (int i = 0; i < aa; i++)
    		{
    			A[a[i] - 'a']++;
    			B[b[i] - 'a']++;
    		}
    
    		bool same = true;
    		for (int i = 0; i < 26; i++)
    			if (A[i] != B[i])
    			{
    				same = false;
    				break;
    			}
    		
    		if(same)
    			printf("%s & %s are anagrams.\n", a, b);
    		else
    			printf("%s & %s are NOT anagrams.\n", a, b);
    	}
    	
    	return 0;
    }

     

      아이디어  

    ** 엥 나랑 똑같은 방식으로 입력 1 출력 1 입력 1 출력 1 이렇게 했는데 이 사람은 맞음 뭐가 틀려 나랑

    **** 길이 비교후에 바로 출력하는 건 괜찮은데

    알파벳 비교하는 와중에 출력하는 부분이 잘못 되었던 듯 !!

    for (int i = 0; i < 26; i++) {
    			if (alpa1[i] != alpa2[i]) {
    				cout << a << " & " << b << " are NOT anagrams." << endl;
    				break;
    			}
    		}
    
    		cout << a << " & " << b << " are anagrams." << endl;
    		continue;

    이렇게 하게 되면 NOT anagrams 문장을 출력하고 또 are anagrams 문장을 출력하게 됨 ㅜㅜ 바보

    저 부분만 bool을 사용하는 방식으로 고쳐주면 이 코드도 맞을 듯 

준생e