알고리즘/백준

[백준/c++] 16171번: 나는 친구가 적다(Small)

녕이 2022. 7. 18. 13:50
728x90

 

 

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

 

16171번: 나는 친구가 적다 (Small)

첫 번째 줄에는 알파벳 소문자, 대문자, 숫자로 이루어진 문자열 S가 주어진다. (1 ≤ |S| ≤ 100) 두 번째 줄에는 성민이가 찾고자 하는 알파벳 소문자, 대문자로만 이루어진 키워드 문자열 K가 주

www.acmicpc.net

 

 

std::string find함수를 사용하면 문자열에서 특정 문자열을 찾을 수 있다. 

find함수는 찾고 싶은 문자열의 시작 위치를 반환한다. (size_t형)

찾고자 하는 문자열이 있는지 확인하려면 아래와 같이 하면 된다.

 

std::string text = "hello world!";
size_t nPos = text.find("world!");
if(nPos != string::npos) { //해당 문자열이 있는지 확인 (있다면)
    //...
}

 

 

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

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    string s1, s2, s3;
    cin >> s1 >> s2;
    
    for(int i=0; i<s1.size(); i++){
        if(s1[i] >= '0' && s1[i] <= '9') continue;
        s3 += s1[i];
    }
    
    size_t nPos = s3.find(s2);
    if(string::npos != nPos) cout << 1 << '\n';
    else                     cout << 0 << '\n';
    
    return 0;
}

 

 

 

 

728x90