Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- undirected graphical model
- MySQL
- vector미분
- Perceptron Convergence theorem
- 인공지능
- 델타 rule
- 알고리즘대회
- 근구하기
- SCPC
- Numerical optimization
- 선형판별분석
- 이것이 MySQL이다
- 스터디
- directed graphical model
- 1차예선
- bisection
- 5397번
- 개발순서
- chapter02
- 2018
- secant
- CH01
- 로지스틱 회귀
- 선형분류
- Fisher discriminant analysis
- 알고리즘
- chapter01
- falsePosition
- graphical models
- 자바ORM표준JPA프로그래밍
Archives
- Today
- Total
computer_study
[탐색] BAEKJOON '1543'번 '문서 검색'문제 (C++/python) 본문
문제 : www.acmicpc.net/problem/1543
-
문제 해결 아이디어
1. 처음부터 일치하는 단어를 찾기위해 순차적으로 탐색하고, 찾았다면 찾은 단어 다음부터 다시 탐색한다.
-
python
document = input()
key = input()
start = 0
result = 0
while len(document) - start >= len(key):
if document[start:start + len(key)] == key:
result += 1
start += len(key)
else:
start += 1
print(result)
-
c++
#include "iostream"
#include "string"
using namespace std;
int result=0;
string input, key;
// size가 넘어간다면 -1
// 해당 부분의 문자열과 단어가 일치한다면 1
// 그렇지 않은 경우는 0
int check_same(int start){
int count =0;
if(start + key.size() > input.size())
return -1;
for(int i=0 ; i< key.size() ; i++){
if(input.at(start+i) == key.at(i))
count++;
}
if(count == key.size())
return 1;
return 0;
}
// 해당 단어를 찾은 경우에, 그 단어가 끝난 다음을 다시 시작으로
// 찾지 못한 경우엔, 다음 단어를 시작으로 탐색한다.
void search_key(int start){
int check = check_same(start);
if(check == -1){
;
}
else if(check == 1){
result++;
search_key(start + key.size());
}
else if(check == 0){
search_key(start+1);
}
}
int main(){
getline(cin,input); // 공백을 포함하여 입력받기 위해 getline사용
getline(cin,key);
search_key(0);
printf("%d\n",result);
return 0;
}
※ getline
'알고리즘 > 문제풀이' 카테고리의 다른 글
[탐색] BAEKJOON '1302'번 '베스트셀러'문제 (C++/python) (0) | 2020.08.11 |
---|---|
[탐색] BAEKJOON '1568'번 '새'문제 (C++/python) (0) | 2020.08.11 |
[재귀함수] BAEKJOON ' 7490 '번 '0 만들기 '문제 (C++/python) (0) | 2020.08.11 |
[재귀함수] BAEKJOON '1074'번 'Z'문제 (C++/python) (0) | 2020.08.07 |
[재귀함수/DP] BAEKJOON '2747'번 '피보나치 수 '문제 (C++/python) (0) | 2020.08.07 |
Comments