#C7401. Count Non-overlapping Substring Occurrences
Count Non-overlapping Substring Occurrences
Count Non-overlapping Substring Occurrences
Given two strings s and sub, your task is to calculate the number of non-overlapping occurrences of sub in s.
The occurrence is non-overlapping, meaning that once an occurrence is found, the search continues from the end of this occurrence.
Mathematically, if we denote the function as \( f(s, sub) \) and the length of sub as \( |sub| \), then the occurrences are counted as follows:
\[ f(s, sub) = \text{number of indices } i \text{ such that } s[i:i+|sub|] = sub \text{ and these intervals do not overlap.} \]For example, for s = "testtesttest"
and sub = "test"
, the result is 3
.
inputFormat
The input consists of two lines from standard input. The first line contains the string s, and the second line contains the substring sub.
Both s and sub are non-empty strings.
outputFormat
The output is a single integer printed to standard output, representing the number of non-overlapping occurrences of the substring sub in s.
## sampletesttesttest
test
3