标签:esc ace 表示 BMI href == asc 也有 rip
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 28917 Accepted Submission(s): 17714
#include<cstdio> #include<iostream> #include<cstring> #include<memory> using namespace std; char moban[1005],wenben[1005]; int next1[1005]; int sum; void getnext(char* s,int* next1,int m) { next1[0]=0; next1[1]=0; for(int i=1;i<m;i++) { int j=next1[i]; while(j&&s[i]!=s[j]) j=next1[j]; if(s[i]==s[j]) next1[i+1]=j+1; else next1[i+1]=0; } } void kmp(char* ss,char* s,int* next1,int n,int m) { int ans=0; getnext(s,next1,m); int j=0; for(int i=0;i<n;i++) { while(j&&s[j]!=ss[i]) j=next1[j]; if(s[j]==ss[i]) j++; if(j==m) { ans++; j=0; } } printf("%d\n",ans); } int main() { while(~scanf("%s",wenben)) { if(wenben[0]==‘#‘) break; scanf("%s",moban); int n=strlen(wenben); int m=strlen(moban); kmp(wenben,moban,next1,n,m); } return 0; }
暴力:
#include<stdio.h> #include<string.h> using namespace std; #define max_v 1005 char s1[max_v],s2[max_v]; int main() { while(~scanf("%s",s1)) { if(s1[0]==‘#‘) break; scanf("%s",s2); int l1=strlen(s1); int l2=strlen(s2); int sum=0; int j=0; for(int i=0; i<l1; i++) { if(s1[i]==s2[j]) { j++; } else { j=0; } if(j==l2) { sum++; j=0; } } printf("%d\n",sum); } }
标签:esc ace 表示 BMI href == asc 也有 rip
原文地址:https://www.cnblogs.com/yinbiao/p/9457482.html