첫 줄에 문자열 text, 둘째 줄에 찾을 말 pattern이 주어진다. text 안에 pattern이 몇 번 나오는지 출력하라. 나온 자리끼리 겹쳐도 따로 센다. 예를 들어 "aaaa"에는 "aa"가 0번째, 1번째, 2번째 자리에서 시작해 모두 3번 나온다. pattern이 text보다 길면 0이다.
입력
text는 영어 소문자, 길이 1 ~ 5,000. pattern은 영어 소문자, 길이 1 ~ 10
출력
나온 횟수
예시 1 · stdin
aaaa
aa
stdout
3
예시 2 · stdin
abababa
aba
stdout
3
main.py
오류 목록00F8 ↓ · ⇧F8 ↑
입력(stdin)
실행하면 이 줄들이 차례로 input()·표준 입력으로 들어가요(실행 중에 묻지 않아요).
$ 준비됐어요
코드를 입력해 풀려면 PC에서 열어 주세요. 모바일에서는 문제와 풀이를 볼 수 있어요.
다른 언어로는?언어별 풀이 보기 · 8개 언어 풀이
"몇 번 나오나"를 한 번에 세 주는 함수는 겹치지 않게 세요: Python text.count("aa")는 "aaaa"에서 2예요. 겹쳐 세려면 찾은 자리 바로 다음 칸(+1)부터 다시 찾아요: Python text.find(p, i + 1), JS·TS·Java indexOf(p, i + 1), C# IndexOf(p, i + 1, StringComparison.Ordinal), C++ find(p, i + 1), C strstr(q + 1, p)(찾은 곳의 포인터를 줘요). 크레이콘의 자리찾기는 시작 자리를 받지 않아서, 자리마다 text.부분문자열(i, m) == pattern으로 비교해요. 함정: 찾은 뒤 패턴 길이만큼(+m) 건너뛰면 기본 count와 똑같이 겹친 것을 놓쳐요.
Pythonmain.py · 8 줄
text = input()pattern = input()count = 0i = text.find(pattern)while i != -1: count += 1 i = text.find(pattern, i + 1)print(count)
JavaScriptmain.js · 4 줄
const [text, pattern] = require("fs").readFileSync(0, "utf8").split("\n").map((s) => s.trim());let count = 0;for (let i = text.indexOf(pattern); i !== -1; i = text.indexOf(pattern, i + 1)) count++;console.log(count);
TypeScriptmain.ts · 5 줄
import { readFileSync } from "node:fs";const [text, pattern]: string[] = readFileSync(0, "utf8").split("\n").map((s) => s.trim());let count = 0;for (let i = text.indexOf(pattern); i !== -1; i = text.indexOf(pattern, i + 1)) count++;console.log(count);
JavaMain.java · 13 줄
import java.io.BufferedReader;import java.io.InputStreamReader;public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String text = br.readLine().trim(); String pattern = br.readLine().trim(); int count = 0; for (int i = text.indexOf(pattern); i != -1; i = text.indexOf(pattern, i + 1)) count++; System.out.println(count); }}
C#Main.cs · 15 줄
using System;class Program{ static void Main() { string text = Console.ReadLine().Trim(); string pattern = Console.ReadLine().Trim(); int count = 0; for (int i = text.IndexOf(pattern, StringComparison.Ordinal); i != -1; i = text.IndexOf(pattern, i + 1, StringComparison.Ordinal)) count++; Console.WriteLine(count); }}
C++main.cpp · 10 줄
#include <iostream>#include <string>int main() { std::string text, pattern; std::cin >> text >> pattern; int count = 0; for (size_t i = text.find(pattern); i != std::string::npos; i = text.find(pattern, i + 1)) count++; std::cout << count << "\n";}
Cmain.c · 13 줄
#include <stdio.h>#include <string.h>static char text[5005];int main(void) { char pattern[16]; if (scanf("%5000s %10s", text, pattern) != 2) return 0; int count = 0; for (char* p = strstr(text, pattern); p != NULL; p = strstr(p + 1, pattern)) count++; printf("%d\n", count); return 0;}
Craykonmain.cray · 12 줄
text = 콘솔.줄읽기().공백제거();pattern = 콘솔.줄읽기().공백제거();m = pattern.길이;count = 0;반복 (i = 0; i + m <= text.길이; i++){ 만약 (text.부분문자열(i, m) == pattern) { count++; }}콘솔.쓰기(count);