[SWEA] String

문제 보러가기

제한사항

한 문장에서 검색하는 문자열의 길이는 최대 10을 넘지 않는다.

한 문장에서는 하나의 문자열만 검색한다.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.Scanner;

public class Solution {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		for(int T=1;T<=10;T++) {
			int tc = sc.nextInt();
			sc.nextLine();
			String target = sc.nextLine();
			String str = sc.nextLine();
			str = str.replaceAll(target, "+");
			int count = 0;
			for(int i=0; i<str.length(); i++) {
				if ( str.charAt(i) == '+' ) { 
					count++;
				}
			}
			System.out.println("#"+tc+" "+count);
		}
	}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.Scanner;

public class Solution {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		for(int tc=1; tc<=10; tc++) {
			int T = s.nextInt();
			String f = s.next();
			int len = f.length();
			String str = s.next();
			int count =0;
			
			for(int i=0; i<str.length()-len+1; i++) {
				//System.out.println(str.substring(i,i+len));
				if(f.equals(str.substring(i, i+len))) {
					count++;
				}
			}
			
			System.out.println("#"+T+" "+count);
		}
	}
}