搜索字符串
Description
搜索字符串
Input
输入两个字符串a,b(字符串长度不超过1000)
Output
输出在a中出现b的次数(每个结果占一行)
Sample Input
abcdefsdabcbacbbc abc aabbaabbaabbaa abbaa
Sample Output
2 3
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 #define MAX 1010 6 7 int main() 8 { 9 char a[MAX],b[MAX]; 10 while(gets(a)!=NULL) 11 { 12 gets(b); 13 int sum=0,i=0,j=0; 14 int lena,lenb; 15 lena=strlen(a); 16 lenb=strlen(b); 17 while(i<lena) 18 { 19 if(a[i]==b[j]) 20 { 21 i++; 22 j++; 23 } 24 else 25 { 26 i=i-j+1; 27 j=0; 28 } 29 if(j>=lenb) 30 { 31 sum++; 32 i=i-j+1; 33 j=0; 34 } 35 } 36 printf("%d\n",sum); 37 } 38 }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步