반응형

정말 쉬운 문자열 처리문제입니다. 

푸는방법도 다양합니다.

 

저는 가장 긴 문자열 기준으로 값을 비교하면서 다른값이거나 문자열이 없다면, ? 

모두 같은 문자라면 해당문자를 추가하는식으로 결과값을 도출했습니다. 

 

 

 

#include <iostream>
#include <string>
using namespace std;

int n = 0;
string arr[50];
string result; 

int main() {
		
	int len = 0,len_index=-1;
	// input 
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
		if (arr[i].size() > len) len = arr[i].size(), len_index = i;
	}

	for (int i = 0; i < len; i++) {
		char word = arr[len_index][i];
		bool jud = false;
		for (int j = 0; j < n; j++) {
			if (i > arr[j].size() - 1) {
				result += '?';
				jud = true;
				break;
			}
			else if(word != arr[j][i]) {
				result += '?';
				jud = true;
				break;
			}
				
		}
		if (jud == false)
			result += word;

	}
	
	cout << result << endl;
}

 

반응형

'알고리즘 > 문자열 처리' 카테고리의 다른 글

[백준] 9935-문자열 폭발(C++)  (0) 2020.02.25
[백준] 2789-유학 금지(C++)  (0) 2020.02.25
[백준] 4949-균형잡힌 세상  (0) 2020.02.24
[백준] 1152-단어의 개수  (0) 2020.02.24

+ Recent posts