标签:oid printf namespace false long set max ios har
题目网址:http://icpc.njust.edu.cn/Problem/Hdu/2087/
代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef unsigned int ui; 4 typedef long long ll; 5 typedef unsigned long long ull; 6 #define pf printf 7 #define mem(a,b) memset(a,b,sizeof(a)) 8 #define prime1 1e9+7 9 #define prime2 1e9+9 10 #define pi 3.14159265 11 #define lson l,mid,rt<<1 12 #define rson mid+1,r,rt<<1|1 13 #define scand(x) scanf("%llf",&x) 14 #define f(i,a,b) for(int i=a;i<=b;i++) 15 #define scan(a) scanf("%d",&a) 16 #define dbg(args) cout<<#args<<":"<<args<<endl; 17 #define inf 0x3f3f3f3f 18 #define maxn 100010 19 int n,m,t; 20 char s[maxn],p[maxn],nxt[maxn]; 21 void getnxt() 22 { 23 int plen=strlen(p); 24 int i=-1,j=0; 25 nxt[0]=-1; 26 while(j<plen) 27 { 28 if(i==-1||p[i]==p[j]) 29 { 30 i++,j++; 31 if(p[i]==p[j])nxt[j]=nxt[i]; 32 else nxt[j]=i; 33 } 34 else i=nxt[i]; 35 } 36 } 37 int kmp() 38 { 39 int plen=strlen(p); 40 int slen=strlen(s); 41 int j=0,i=0; 42 int cnt=0; 43 while(i<slen) 44 { 45 if(j==-1||s[i]==p[j])i++,j++; 46 else j=nxt[j]; 47 if(j==plen) 48 { 49 j=0,cnt++; 50 } 51 } 52 return cnt; 53 } 54 int main() 55 { 56 //freopen("input.txt","r",stdin); 57 //freopen("output.txt","w",stdout); 58 std::ios::sync_with_stdio(false); 59 while(scanf("%s",s)) 60 { 61 if(!strcmp(s,"#"))break; 62 scanf(" %s",p); 63 getnxt(); 64 pf("%d\n",kmp()); 65 } 66 }
标签:oid printf namespace false long set max ios har
原文地址:https://www.cnblogs.com/randy-lo/p/12516699.html