2019年6月多校联训b层Day 2 test 问题 A: AC
大水题,不过考试时我想到的是KMP
#include<bits/stdc++.h> using namespace std; const int maxn=1e6+7; char s1[maxn],s2[maxn]; bool flag1,flag2; int fail[maxn]; int fail1[maxn]; int len1,len2; char test[maxn]; char test1[maxn]; int suma[maxn]; int sumc[maxn]; int sum1a[maxn]; int sum1c[maxn]; int ans1,ans2; void pre() { fail[1]=0; fail[2]=0; fail1[1]=0; fail1[2]=0; } void kmp1() { int ans=0,j=0; int l1=strlen(test+1); for(int i=1;i<=len1;i++) { while(j>0&&test[j+1]!=s1[i]) j=fail[j]; if(test[j+1]==s1[i]) j++; if(j==l1) { ans++; suma[ans]=i-l1+1; } } ans1=ans; } void kmp2() { int ans=0,j=0; int l2=strlen(test1+1); for(int i=1;i<=len1;i++) { while(j>0&&test1[j+1]!=s1[i]) j=fail1[j]; if(test1[j+1]==s1[i]) j++; if(j==l2) { ans++; sum1c[ans]=i-l2+1; } } ans2=ans; } bool check() { for(int i=1;i<=ans1;i++) { for(int j=1;j<=ans2;j++) { if(abs(suma[i]-sum1c[j])>1){ return true; } } } return false; } int main() { while(scanf("%s",s1+1)!=EOF) { memset(fail1,0,sizeof(fail1)); memset(fail,0,sizeof(fail)); len1=strlen(s1+1); test[1]=\'A\'; test[2]=\'C\'; test1[1]=\'C\'; test1[2]=\'A\'; pre(); kmp1(); kmp2(); if(check()) printf("YES\n"); else printf("NO\n"); } return 0; }