1일1알

백준 14426번 접두사 찾기 C++ 본문

알고리즘

백준 14426번 접두사 찾기 C++

영춘권의달인 2022. 10. 25. 10:10

https://www.acmicpc.net/problem/14426

 

14426번: 접두사 찾기

문자열 S의 접두사란 S의 가장 앞에서부터 부분 문자열을 의미한다. 예를 들어, S = "codeplus"의 접두사는 "code", "co", "codepl", "codeplus"가 있고, "plus", "s", "cude", "crud"는 접두사가 아니다. 총 N개의 문자

www.acmicpc.net

 

unordered_set 이용

 

#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>

using namespace std;
using int64 = long long;

int n, m;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
    unordered_set<string> st;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        string str;
        cin >> str;
        string prefix = "";
        for (int j = 0; j < str.length(); j++) {
            prefix += str[j];
            st.insert(prefix);
        }
    }
    int ans = 0;
    for (int i = 0; i < m; i++) {
        string str;
        cin >> str;
        auto it = st.find(str);
        if (it != st.end()) ans++;
    }
    cout << ans;
}