할껀하고놀자

[SWEA] 7272번 안경이 없어! 본문

[IT]/SWEA

[SWEA] 7272번 안경이 없어!

working_hard 2019. 6. 3. 17:33
728x90
#include<iostream>
#include<string>
using namespace std;

char arr[] = { 'A','D','O','P','Q','R' };

int main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		string n1, n2, n3, n4;
		cin >> n1 >> n2;
		if (n1.size() != n2.size()) {
			cout << "#" << i << " " << "DIFF" << endl;
			continue;
		}
		for (int j = 0; j < n1.length(); j++) {
			char c = n1.at(j);
			if (c == 'B') {
				n3.append("2");
			}
			else {
				for (int k = 0; k < 6; k++) {
					if (c == arr[k]) {
						n3.append("1");
						break;
					}
				}
				n3.append("0");
			}
			c = n2.at(j);
			if (c == 'B') {
				n4.append("2");
			}
			else {
				for (int k = 0; k < 6; k++) {
					if (c == arr[k]) {
						n4.append("1");
						break;
					}
				}
				n4.append("0");
			}
		}
		if (n3 == n4) {
			cout << "#" << i << " " << "SAME" << endl;
		}
		else {
			cout << "#" << i << " " << "DIFF" << endl;
		}
	}

	return 0;
}

포인트 : 문제 잘 읽기. 문자열 size가 다른 경우도 있다. 

 

문자열 size 알기. str.size()

문자열 더해주기 : str.append();

문자열에서 하나씩 알고 싶을 때 : char c = str.at(1);

 

다른 방식으로 풀이한 코드입니다.

#include<iostream>
#include<string>
using namespace std;
bool is_equal(string s1, string s2)
{
    if(s1.length()!=s2.length())return false;
 
    int a[26]={0,};
    a['B'-'A']=2;
    a['A'-'A']=1;
    a['D'-'A']=1;
    a['O'-'A']=1;
    a['P'-'A']=1;
    a['Q'-'A']=1;
    a['R'-'A']=1;
 
    for(int i=0;i<s1.length();i++)
        if(a[s1[i]-'A']!=a[s2[i]-'A']) return false;
    return true;
}
int main()
{
    cin.tie(NULL);
    cin.sync_with_stdio(false);
    int T;
    cin>>T;
    for(int z=1;z<=T;z++)
    {
        string s1,s2;
        cin>>s1>>s2;
        cout<<"#"<<z<<(is_equal(s1,s2)?" SAME\n":" DIFF\n");
    }
}

 

'[IT] > SWEA' 카테고리의 다른 글

[SWEA] 6853번 직사각형과 점  (0) 2019.06.03
Comments