When working with files on a computer, wildcards (?) are often used to identify common patterns or to handle multiple files efficiently. In this problem, you need to determine a common pattern among multiple file names.
You will be given N file names as input, and all file names have the same length. For each position in the file names, if all characters at that position are identical, you should output that character. Otherwise, you should replace it with a ?.
The goal of this problem is to derive a pattern based on the given N file names. By comparing the characters at each position across all file names, you can determine whether to keep the character or replace it with ?.
For example, given the following file names:
config.sys
config.inf
configures
The output should be:
config????
Your task is to implement a program that generates this pattern based on the given file names.
This problem requires writing a program that processes strings. Due to the nature of string manipulation, it can be challenging if you are not familiar with arrays, pointers, and the characteristics of C strings.
There isn’t much of an algorithm to devise. My approach was to store the first word and then compare it with incoming strings. If there is a difference in any character, I marked that position with a ?. If a position is already marked with ?, you can skip checking it, but it’s not necessary. It wouldn’t cause any major issues either way.
Below is the source code I wrote. Please use it as a reference.
//----------------------------------------------------------
// baekjoon #1032 - Command Prompt
// - by Aubrey Choi
// - created at 2019-09-13
//----------------------------------------------------------
#include <stdio.h>
int main()
{
int n;
char p[52], w[52];
scanf("%d%s",&n,p);
while(--n) { scanf("%s", w); for(int i=0;p[i];i++) if(p[i]!='?'&&p[i]!=w[i]) p[i]='?'; }
puts(p);
}
'Programming > BOJ' 카테고리의 다른 글
[C/C++] BOJ #1026 - Treasure (0) | 2025.02.04 |
---|---|
[C/C++] BOJ #1024 - Sum of Number Sequences. (0) | 2025.02.02 |
[C/C++] BOJ #1021 - Rotating Queues (0) | 2025.01.29 |
[C/C++] BOJ #1019 - Book Page (0) | 2025.01.26 |
[C/C++] BOJ #1018 - Paint Chess Board Again (0) | 2025.01.18 |