Welcome, Guest! Sign Up RSS

Clever Space

Friday, 11.22.2024
Main » 2013 » October » 21 » [NOIP2012模拟赛No.19]DNA病毒
3:01 AM
[NOIP2012模拟赛No.19]DNA病毒

本来只想测试一下人品如何,没想到。。。

RK的强大让我无力吐槽,

这道题O(nlogn)的人品算法竟然比O(n)还快。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<set>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<iostream>
#include<string>
#include<cmath>
#define PRIME 13331
#define N 8010
#define FOR(i,a,b) for(i=(a);i<=(b);i++)
#define ROF(i,a,b) for(i=(a);i>=(b);i--)
typedef unsigned long long ULL;
using namespace std;
ULL p[N];char s1[N],s2[N];ULL c[N];
void Main()
{
  scanf("%s",s1+1);scanf("%s",s2+1);
  int n=strlen(s1+1),m=strlen(s2+1),i;
  if (n<m) {printf("NO\n");return;}
  ULL tmp=0;
  FOR(i,1,m) {tmp*=PRIME;tmp+=s2[i]-'a';}
  int len=1;c[1]=tmp;
  FOR(i,1,m) {tmp-=(s2[i]-'a')*p[m-1];tmp*=PRIME;tmp+=s2[i]-'a';c[++len]=tmp;}
  sort(c+1,c+1+len);
  tmp=0;
  FOR(i,1,n)
  {
    if (i<=m) {tmp*=PRIME;tmp+=s1[i]-'a';}
    else {tmp-=(s1[i-m]-'a')*p[m-1];tmp*=PRIME;tmp+=s1[i]-'a';}
    if (i>=m)
    {
      if (binary_search(c+1,c+1+len,tmp)) {printf("YES\n");return;}
    }
  }
  printf("NO\n");
}
int main()
{
  int T,i;
  scanf("%d",&T);
  p[0]=1;
  FOR(i,1,8000) p[i]=p[i-1]*PRIME;
  while (T--) Main();
}

Views: 338 | Added by: dhy0077 | Rating: 0.0/0
Total comments: 0
Only registered users can add comments.
[ Sign Up | Login ]