반응형
[ 문제 ]
총 N개의 문자열로 이루어진 집합 S가 주어진다.
입력으로 주어지는 M개의 문자열 중에서 집합 S에 포함되어 있는 것이 총 몇 개인지 구하는 프로그램을 작성하시오.
[ 코드 ]
1. 내 코드
StreamReader sr = new StreamReader(Console.OpenStandardInput());
StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
string[] s = sr.ReadLine().Split();
int n = int.Parse(s[0]);
int m = int.Parse(s[1]);
string[] arrS = new string[n];
Dictionary<string, int> arrM = new Dictionary<string, int>();
int count = 0;
for (int i = 0; i < n; i++)
{
arrS[i] = sr.ReadLine();
}
for (int i = 0; i < m; i++)
{
string input = sr.ReadLine();
if (arrM.ContainsKey(input))
{
arrM[input]++;
}
else
{
arrM[input] = 1;
}
}
foreach(var item in arrS)
{
if (arrM.ContainsKey(item))
{
count += arrM[item];
}
}
sw.Write(count);
sw.Flush();
sw.Close();
sr.Close();
2. 다른 사람 참고 코드
StreamReader sr = new StreamReader(Console.OpenStandardInput());
StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
string[] s = sr.ReadLine().Split();
int n = int.Parse(s[0]);
int m = int.Parse(s[1]);
int count = 0;
HashSet<string> set = new HashSet<string>();
while(n-- > 0)
{
set.Add(sr.ReadLine());
}
while(m-- > 0)
{
if (set.Contains(sr.ReadLine()))
{
count++;
}
}
sw.Write(count);
sw.Flush();
sw.Close();
sr.Close();
[ 실행화면 ]
문제링크: https://www.acmicpc.net/problem/14425
14425번: 문자열 집합
첫째 줄에 문자열의 개수 N과 M (1 ≤ N ≤ 10,000, 1 ≤ M ≤ 10,000)이 주어진다. 다음 N개의 줄에는 집합 S에 포함되어 있는 문자열들이 주어진다. 다음 M개의 줄에는 검사해야 하는 문자열들이 주어
www.acmicpc.net
반응형
'Language > C#' 카테고리의 다른 글
[C#] 백준 1620번 나는야 포켓몬 마스터 이다솜 (0) | 2024.03.24 |
---|---|
[C#] 백준 7785번 회사에 있는 사람 (0) | 2024.03.24 |
[C#] 백준 17103번 골드바흐 파티션 (0) | 2024.03.23 |
[C#] 18870번 좌표 압축 (0) | 2024.03.22 |
[C#] 백준 7568번 덩치 (0) | 2024.03.21 |