[백준 29945] Loetamatu tekst (C#, C++) - soo:bak
작성일 :
문제 링크
설명
N개의 후보 주제와 패턴 문자열이 주어집니다. 패턴은 알파벳 소문자와 별표로 이루어지며, 별표는 정확히 한 글자를 대체합니다.
패턴과 길이가 같고 별표가 아닌 위치의 문자가 모두 일치하는 후보를 찾아 입력 순서대로 모두 출력하는 문제입니다.
접근법
패턴과 길이가 다른 후보는 제외한 후, 길이가 같은 후보에 대해 각 위치를 비교합니다.
패턴의 문자가 별표이면 통과하고, 별표가 아니면 해당 위치의 문자가 일치해야 합니다.
이후 조건을 만족하는 후보를 모두 모아 개수와 함께 출력합니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System.Collections.Generic;
class Program {
static bool Match(string s, string pat) {
if (s.Length != pat.Length)
return false;
for (int i = 0; i < s.Length; i++) {
if (pat[i] == '*')
continue;
if (pat[i] != s[i])
return false;
}
return true;
}
static void Main() {
var n = int.Parse(Console.ReadLine()!);
var words = new List<string>(n);
for (var i = 0; i < n; i++)
words.Add(Console.ReadLine()!);
var pat = Console.ReadLine()!;
var ans = new List<string>();
foreach (var w in words) {
if (Match(w, pat))
ans.Add(w);
}
Console.WriteLine(ans.Count);
foreach (var w in ans)
Console.WriteLine(w);
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <bits/stdc++.h>
using namespace std;
typedef vector<string> vs;
bool match(const string& s, const string& pat) {
if (s.size() != pat.size())
return false;
for (size_t i = 0; i < s.size(); i++) {
if (pat[i] == '*')
continue;
if (pat[i] != s[i])
return false;
}
return true;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vs words(n);
for (int i = 0; i < n; i++)
cin >> words[i];
string pat;
cin >> pat;
vs ans;
for (auto& w : words) {
if (match(w, pat))
ans.push_back(w);
}
cout << ans.size() << "\n";
for (auto& w : ans)
cout << w << "\n";
return 0;
}