Try Again

Codefroces 852 G. Bathroom terminal

G. Bathroom terminal

Smith wakes up at the side of a dirty, disused bathroom, his ankle chained to pipes. Next to him is tape-player with a hand-written message "Play Me". He finds a tape in his own back pocket. After putting the tape in the tape-player, he sees a key hanging from a ceiling, chained to some kind of a machine, which is connected to the terminal next to him. After pressing a Play button a rough voice starts playing from the tape:

"Listen up Smith. As you can see, you are in pretty tough situation and in order to escape, you have to solve a puzzle.

You are given N strings which represent words. Each word is of the maximum length L and consists of characters 'a'-'e'. You are also given M strings which represent patterns. Pattern is a string of length  ≤  L and consists of characters 'a'-'e' as well as the maximum 3 characters '?'. Character '?' is an unknown character, meaning it can be equal to any character 'a'-'e', or even an empty character. For each pattern find the number of words that matches with the given pattern. After solving it and typing the result in the terminal, the key will drop from the ceiling and you may escape. Let the game begin."

Help Smith escape.

Input

The first line of input contains two integers N and M (1 ≤ N ≤  100 000, 1 ≤ M ≤  5000), representing the number of words and patterns respectively.

The next N lines represent each word, and after those N lines, following M lines represent each pattern. Each word and each pattern has a maximum length L (1 ≤ L ≤ 50). Each pattern has no more that three characters '?'. All other characters in words and patters are lowercase English letters from 'a' to 'e'.

Output

Output contains M lines and each line consists of one integer, representing the number of words that match the corresponding pattern.

Example
Input
3 1
abc
aec
ac
a?c
Output
3
Note

If we switch '?' with 'b', 'e' and with empty character, we get 'abc', 'aec' and 'ac' respectively.

 匹配查询

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define ios() ios::sync_with_stdio(true)
#define INF 1044266558
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
int n,m,ans,a[5],len,k;
char s[110];
string mp;
set<string>v;
struct trie
{
    int cnt;
    struct trie *next[26];
    trie()
    {
        cnt=0;
        for(int i=0;i<26;i++)
            next[i]=NULL;
    }
};
trie *root;
void insert(char *s)
{
    trie *p=root,*tmp;
    for(int i=0;s[i]!='\0';i++)
    {
        if(p->next[s[i]-'a']==NULL)
        {
            tmp=new trie();
            p->next[s[i]-'a']=tmp;
        }
        p=p->next[s[i]-'a'];
    }
    p->cnt++;
}
int find(string s)
{
    int x=s.size();
    trie *p=root;
    for(int i=0;i<x;i++)
    {
        if(p->next[s[i]-'a']==NULL) return 0;
        p=p->next[s[i]-'a'];
    }
    return p->cnt;
}
void solve(int x)
{
    if(x==k)
    {
        mp="";
        for(int i=0;i<len;i++)
        {
            if(s[i]=='`') continue;
            mp+=s[i];
        }
        if(!v.count(mp))
        {
            ans+=find(mp);
            v.insert(mp);
        }
        return ;
    }
    for(int i=-1;i<5;i++)
    {
        s[a[x]]=(char)('a'+i);
        solve(x+1);
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    root=new trie();
    for(int i=0;i<n;i++)
    {
        scanf("%s",s);
        insert(s);
    }
    while(m--)
    {
        ans=0;k=0;
        scanf("%s",s);
        len=strlen(s);
        v.clear();
        for(int i=0;i<len;i++)
            if(s[i]=='?') a[k++]=i;
        solve(0);
        printf("%d\n",ans);
    }
    return 0;
}

map也可以,效率有点低

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define ios() ios::sync_with_stdio(false)
#define INF 1044266558
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
set<string>s;
map<string,int>mp;
string v;
char ch[75];
int a[5],n,m,ans,k,len;
void solve(int x)
{
    if(x==k)
    {
        v="";
        for(int i=0;i<len;i++)
        {
            if(ch[i]=='`') continue;
            v+=ch[i];
        }
        if(!s.count(v))
        {
            ans+=mp[v];
            s.insert(v);
        }
        return ;
    }
    for(int i=-1;i<5;i++)
    {
        ch[a[x]]=(char)('a'+i);
        solve(x+1);
    }
}
int main()
{
    ios::sync_with_stdio(true);
    cin>>n>>m;
    for(int i=0;i<n;i++)
    {
        cin>>v;
        mp[v]++;
    }
    while(m--)
    {
        ans=0;k=0;
        scanf("%s",ch);
        len=strlen(ch);
        s.clear();
        for(int i=0;i<len;i++)
        {
            if(ch[i]=='?') a[k++]=i;
        }
        solve(0);
        printf("%d\n",ans);
    }
    return 0;
}

 

posted @ 2017-09-05 22:11  十年换你一句好久不见  阅读(319)  评论(0编辑  收藏  举报