1260:串刺しを探します


テーマ
Description
原子串と目標子串を与えて、目標の子串が原子串の中で出現する回数を求めます。
Input
複数のグループのテストデータは、各グループのテストデータの最初の行は原子列で、2番目の行は目標のサブストリングの長さは100を超えないです。Output
出力対象のサブストリングが原子列に出現する回数。
Sample Input
abc 123 abc abc CdeAbcAbcdecde Sample Output
2
コードブロック
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner cn = new Scanner(System.in);
        while (cn.hasNext()) {
            String str = cn.next();
            String str1 = cn.next();
            int length = str.length();
            int length1 = str1.length();
            int count = 0;
            int t = str.indexOf(str1);
            while (t >= 0 && length >= length1) {
                t = str.indexOf(str1);
                str = str.substring(t + length1 - 1);
                count++;
            }
            System.out.println(count - 1);
        }
    }
}