「何回現れるか」を一度で数える関数は重ならないように数えます: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)(見つけた所のポインタを返します)。 Craykon の 位置検索 は開始位置を受け取らないので、位置ごとに 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);