문제) 백준 - 문자열 - 부분 문자열
https://www.acmicpc.net/problem/6550
6550번: 부분 문자열
입력은 여러 개의 테스트 케이스로 이루어져 있다. 각 테스트 케이스는 한 줄로 이루어져 있으며, 문자열 s 와 t가 빈칸을 사이에 두고 들어온다. s와 t의 길이는 10만을 넘지 않는다.
www.acmicpc.net
문자열 s가 문자열 t의 부분 문자열로 존재하는지 확인하는 문제였습니다. t의 문자열을 처음부터 탐색하면서 s와 같을 경우 idx를 1 증가시킵니다. 만약 idx가 s 문자열의 길이와 같을 경우 true를 return 합니다.
C++ 소스코드)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
#define ll long long | |
#define INF 987654321 | |
#define MAX 4002 | |
using namespace std; | |
typedef pair<int, double> p; | |
bool solve(string s, string t) | |
{ | |
int idx = 0; | |
for(int i = 0; i <t.size(); ++i){ | |
if(idx == s.size()) | |
return true; | |
if(t[i] == s[idx]) | |
idx++; | |
} | |
if(idx == s.size()) | |
return true; | |
return false; | |
} | |
int main() | |
{ | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); | |
cout.tie(0); | |
string s, t; | |
while (cin >> s>>t) | |
{ | |
if (solve(s, t)) | |
cout << "Yes" << endl; | |
else | |
cout << "No" << endl; | |
} | |
return 0; | |
} |
반응형
'PS(Problem Solving) > 백준_BOJ' 카테고리의 다른 글
[백준] 2304번 - 창고 다각형 (C++) 문제 및 풀이 (0) | 2022.02.09 |
---|---|
[백준] 22869번 - 징검다리 건너기 (small) (C++) 문제 및 풀이 (0) | 2022.02.08 |
[백준] 2602번 - 돌다리 건너기 (C++) 문제 및 풀이 (0) | 2022.02.04 |
[백준] 18113번 - 그르다 김가놈 (C++) 문제 및 풀이 (0) | 2022.02.04 |
[백준] 18513번 - 샘터 (C++) 문제 및 풀이 (0) | 2022.02.03 |
댓글