zoukankan      html  css  js  c++  java
  • poj 1816 Wild Words

    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
    

    Source

     
    给出一些pattern,然后给出一些子串,找出与之可以匹配的pattern的下标,问好必须匹配一个字符,星号可以匹配任意个字符(包括0),坑的是可能有重复的pattern,也就是说每个pattern可以对应多个下标。先用字典树记录pattern,然后标记每个结尾,下标指向相应结尾,check的时候,如果满足,就把结尾的ans标记为true,最后判断哪个下标对应的结尾是true,就输出。
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #define MAX 600005
    using namespace std;
    int trie[MAX][28];
    bool flag[MAX];
    int to[100000];
    int pos,n,m;
    bool ans[MAX];
    int Insert(char *s) {
        int i = 0,c = 0;
        while(s[i]) {
            int d = isalpha(s[i]) ? s[i] - 'a' : s[i] == '?' ? 26 : 27;
            if(!trie[c][d]) trie[c][d] = ++ pos;
            c = trie[c][d];
            i ++;
        }
        flag[c] = true;
        return c;
    }
    void check(char *s,int si,int c) {
        if(si == strlen(s) && flag[c]) {
            ans[c] = true;
        }
        int d = s[si] - 'a';
        if(trie[c][d]) {
            check(s,si + 1,trie[c][d]);
        }
        if(trie[c][26]) {
            check(s,si + 1,trie[c][26]);
        }
        if(trie[c][27]) {
            int sj = si;
            while(sj <= strlen(s)) {
                check(s,sj,trie[c][27]);
                sj ++;
            }
        }
    }
    int main() {
        char s[30];
        scanf("%d%d",&n,&m);
        for(int i = 0;i < n;i ++) {
            scanf("%s",s);
            to[i] = Insert(s);
        }
        for(int i = 0;i < m;i ++) {
            scanf("%s",s);
            memset(ans,false,sizeof(ans));
            int match = 0;
            check(s,0,0);
            for(int j = 0;j < n;j ++) {
                if(ans[to[j]]) {
                    printf("%d ",j);
                    match ++;
                }
            }
            if(!match)printf("Not match");
            putchar('
    ');
        }
    }
  • 相关阅读:
    使用注解方式实现 AOP和IoC
    代理工厂生成器和顾问包装通知
    多种方式实现AOP
    Spring面试题
    使用集合方式注入IoC
    Spring代理模式
    Spring AOP的使用及案例
    bzoj 1715: [Usaco2006 Dec]Wormholes 虫洞 -- spfa判断负环
    bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会 -- Tarjan
    bzoj 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 -- 线段树
  • 原文地址:https://www.cnblogs.com/8023spz/p/9621040.html
Copyright © 2011-2022 走看看