码迷,mamicode.com
首页 > 其他好文 > 详细

【CQOI2014】通配符匹配

时间:2019-07-31 23:38:29      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:http   name   cqoi2014   void   通配符   stream   匹配   https   namespace   

题面

https://www.luogu.org/problem/P3167

题解

#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#define N 100500
#define ri register int
#define uLL unsigned long long

const uLL p=107;
using namespace std;
uLL pp[N];
struct hash {
  uLL sum[N];
  inline void getsum(char *s) {
    sum[0]=0;
    int l=strlen(s+1);
    for (ri i=1;i<=l;i++) sum[i]=sum[i-1]*p+(s[i]-a);
  }
  inline uLL getval(int l,int r) {
    //if (l>r) return 0;
    return sum[r]-sum[l-1]*pp[r-l+1];
  }
} H1,H2;

bool f[15][N];
char s1[N],s2[N];
int L[15],loc[15];

int main(){
  pp[0]=1;
  for (ri i=1;i<N;i++) pp[i]=pp[i-1]*p;
  scanf("%s",s1+1);
  int l=strlen(s1+1);
  s1[l+2]=s1[l+1]; s1[l+1]=?;
  int cnt=0,pre=0;
  loc[0]=0;
  for (ri i=1;i<=l+1;i++) if (s1[i]==?||s1[i]==*) {
    loc[++cnt]=i;
    L[cnt]=i-pre-1;
    pre=i;
  }
  H1.getsum(s1);
  int n;
  scanf("%d",&n);
  while (n--) {
    scanf("%s",s2+1);
    int l=strlen(s2+1);
    s2[l+2]=s2[l+1]; s2[l+1]=#;
    H2.getsum(s2);
    memset(f,0,sizeof(f));
    f[0][0]=1;
    for (ri i=1;i<=cnt;i++) 
      for (ri j=0;j<=l+1;j++) {
        if (s1[loc[i]]==*) {
          if (H1.getval(loc[i-1]+1,loc[i]-1)==H2.getval(j-L[i]+1,j)&&f[i-1][j-L[i]]) f[i][j]=1;
          if (H1.getval(loc[i-1]+1,loc[i]-1)==H2.getval(j-L[i],j-1)&&f[i-1][j-L[i]-1]) f[i][j]=1;
          f[i][j]|=f[i][j-1];
        }
        else {
          if (H1.getval(loc[i-1]+1,loc[i]-1)==H2.getval(j-L[i],j-1)&&f[i-1][j-L[i]-1]) f[i][j]=1;
        }
      }
    if (f[cnt][l+1]==1) puts("YES"); else puts("NO");
  }
}

 

【CQOI2014】通配符匹配

标签:http   name   cqoi2014   void   通配符   stream   匹配   https   namespace   

原文地址:https://www.cnblogs.com/shxnb666/p/11279812.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!