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 | 29 | 30 | 31 |
Tags
- Numerical optimization
- undirected graphical model
- falsePosition
- bisection
- 개발순서
- MySQL
- 2018
- 알고리즘대회
- 알고리즘
- Perceptron Convergence theorem
- graphical models
- 델타 rule
- SCPC
- directed graphical model
- 선형판별분석
- chapter01
- 1차예선
- secant
- CH01
- Fisher discriminant analysis
- 스터디
- 로지스틱 회귀
- 선형분류
- 자바ORM표준JPA프로그래밍
- vector미분
- chapter02
- 5397번
- 인공지능
- 근구하기
- 이것이 MySQL이다
Archives
- Today
- Total
computer_study
[탐색] BAEKJOON '1543'번 '문서 검색'문제 (C++/python) 본문
문제 : www.acmicpc.net/problem/1543
1543번: 문서 검색
세준이는 영어로만 이루어진 어떤 문서를 검색하는 함수를 만들려고 한다. 이 함수는 어떤 단어가 총 몇 번 등장하는지 세려고 한다. 그러나, 세준이의 함수는 중복되어 세는 것은 빼고 세야 한�
www.acmicpc.net
-
문제 해결 아이디어
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