zoukankan      html  css  js  c++  java
  • POJ 1816

    题目链接:

    http://poj.org/problem?id=1816

    http://bailian.openjudge.cn/practice/1816?lang=en_US

    Time Limit: 2000MS  Memory Limit: 65536K

    Description

    A word is a string of lowercases. A word pattern is a string of lowercases, '?'s and '*'s. In a pattern, a '?' matches any single lowercase, and a '*' matches none or more lowercases.

    There are many word patterns and some words in your hand. For each word, your task is to tell which patterns match it.

    Input

    The first line of input contains two integers N (0 < N <= 100000) and M (0 < M <=100), representing the number of word patterns and the number of words. Each of the following N lines contains a word pattern, assuming all the patterns are numbered from 0 to N-1. After those, each of the last M lines contains a word.

    You can assume that the length of patterns will not exceed 6, and the length of words will not exceed 20.

    Output

    For each word, print a line contains the numbers of matched patterns by increasing order. Each number is followed by a single blank. If there is no pattern that can match the word, print "Not match".

    Sample Input

    5 4
    t*
    ?h*s
    ??e*
    *s
    ?*e
    this
    the
    an
    is

    Sample Output

    0 1 3
    0 2 4
    Not match
    3

    题意:

    给出 $n$ 个模式串(只包含小写字母,"?" 和 "*"),$m$ 个字符串(只包含小写字母),

    模式串中 "?" 代表能匹配任意一个字母,"*" 代表能匹配任意多个字母(可以是 $0$ 个)。

    现在对于 $m$ 个字符串,查询编号为 $0 sim n-1$ 这 $n$ 个模式串中有多少个是可以匹配的上的。

    题解:

    对 $n$ 个模式串建立字典树;对于 $m$ 个字符串的查询,每个字符串都在字典树上进行DFS匹配。

    放几组数据:

    3 1
    t
    t*
    t**
    t
    1 1
    *j*j
    jjj
    2 1
    abc
    abc
    abc

    AC代码:

    #include<cstdio>
    #include<string>
    #include<vector>
    #include<algorithm>
    using namespace std;
    const int maxn=100000+5;
    int n,m;
    
    namespace Trie
    {
        const int SIZE=maxn;
        int sz;
        struct TrieNode{
            vector<int> pos;
            int nxt[28];
        }trie[SIZE];
        void init(){sz=1;}
        int idx(const char& c)
        {
            if(c=='?') return 26;
            if(c=='*') return 27;
            return c-'a';
        }
        void insert(const string& s,int id)
        {
            int p=1;
            for(int i=0;i<s.size();i++)
            {
                int ch=idx(s[i]);
                if(!trie[p].nxt[ch]) trie[p].nxt[ch]=++sz;
                p=trie[p].nxt[ch];
            }
            trie[p].pos.push_back(id);
        }
    
        void dfs(vector<int>& ans,int p,const string& s,int k)
        {
            if(trie[p].nxt[27]) {
                for(int i=0;k+i<=s.size();i++) dfs(ans,trie[p].nxt[27],s,k+i);
            }
            if(k>=s.size())
            {
                for(int i=0;i<trie[p].pos.size();i++) ans.push_back(trie[p].pos[i]);
                return;
            }
            int ch=idx(s[k]);
            if(trie[p].nxt[26]) dfs(ans,trie[p].nxt[26],s,k+1);
            if(trie[p].nxt[ch]) dfs(ans,trie[p].nxt[ch],s,k+1);
        }
    };
    
    int main()
    {
        scanf("%d%d",&n,&m);
        Trie::init();
        char s[23];
        for(int i=0;i<n;i++)
        {
            scanf("%s",&s);
            Trie::insert(s,i);
        }
    
        vector<int> ans;
        for(int i=1;i<=m;i++)
        {
            scanf("%s",&s);
            ans.clear();
            Trie::dfs(ans,1,s,0);
            if(ans.empty()) printf("Not match
    ");
            else
            {
                sort(ans.begin(),ans.end());
                ans.erase(unique(ans.begin(),ans.end()),ans.end());
                for(int k=0;k<ans.size();k++) {
                    printf("%d%c",ans[k],(k==ans.size()-1)?'
    ':' ');
                }
            }
        }
    }
  • 相关阅读:
    2019.9.18 Unity3D与Android相互传递消息 & unity与ios相互传递消息
    2019.9.10 IEnumerable 详解C# 迭代器
    Windows Live Writer 之 代码快速插入插件
    目标管理剖析与实践– 献给追梦的人 (转)
    转:简历写法
    Linux下 输入 env 而得到的环境变量解读
    how to install tweepy
    全分布式环境下,DataNode不启动的问题解决
    几个因为hadoop配置文件不当造成的错误
    Hadoop: HDFS 格式化时,出现 “ERROR namenode.NameNode: java.io.IOException: Cannot create directory /usr/hadoop/tmp/dfs/name/current”
  • 原文地址:https://www.cnblogs.com/dilthey/p/9973362.html
Copyright © 2011-2022 走看看