문자열을 앞에서부터 탐색하며, 주어진 부분 문자열이 존재하는지 확인한다.
존재하는 경우 카운트 해주면된다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #include <stdio.h> #include <string.h> int main(void) { int T; setbuf(stdout, NULL); scanf("%d\n", &T); for (int test_case = 1; test_case <= T; test_case++) { char str1[10002] = { 0, }, str2[102] = { 0, }; scanf("%s %s", str1,str2); int len1 = strlen(str1), len2 = strlen(str2); int cnt = 0; int temp_cnt = 0; for (int i = 0; i < len1; i++) { int check = 1; for (int j = 0; j < len2; j++) { if (str1[i+j] != str2[j]) { check = 0; break; } } if (check == 0) cnt++; else { cnt++; i += len2 - 1; } } printf("#%d %d\n", test_case, cnt); } return 0; } | cs |