栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 1816 Wild Words

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 1816 Wild Words

#include<cstdio>#include<cstring>#include<algorithm>#include<set>#include<vector>using namespace std;const int N=100005;struct node{    char ch;    int next[30];    void init(char _ch)    {        ch=_ch;        memset(next,-1,sizeof(next));    }}tr[N*10];int cnt,head[N],nc;bool mark[N];struct Edge{    int to,next;}edge[N*2];void add(int a,int b){    edge[nc].to=b;edge[nc].next=head[a];head[a]=nc++;}inline int change(char x){    if(x=='?')        return 26;    else if(x=='*')        return 27;    else        return x-'a';}void insert(char s[],int id){    int i,p;    for(i=0,p=0;s[i]!='';i++)    {        int pt=change(s[i]);        if(tr[p].next[pt]==-1)        { tr[p].next[pt]=cnt; p=cnt++; tr[p].init(s[i]);        }        else p=tr[p].next[pt];    }    add(p,id);}bool flag;void dfs(char s[],int p){    if(s[0]=='')    {        for(int i=head[p];i!=-1;i=edge[i].next) flag=mark[edge[i].to]=true;        if(tr[p].next[27]!=-1) dfs(s,tr[p].next[27]);        return;    }    int pt=change(s[0]);    if(tr[p].next[pt]!=-1)        dfs(s+1,tr[p].next[pt]);    if(tr[p].next[26]!=-1)        dfs(s+1,tr[p].next[26]);    if(tr[p].next[27]!=-1)        dfs(s+1,tr[p].next[27]),dfs(s,tr[p].next[27]);    if(tr[p].ch=='*')        dfs(s+1,p);}int main(){    int n,m;    while(scanf("%d%d",&n,&m)!=EOF)    {        tr[0].init('$');        cnt=1;        memset(head,-1,sizeof(head));        nc=0;        for(int i=0;i<n;i++)        { char s[30]; scanf("%s",s); insert(s,i);        }        while(m--)        { char s[30]; scanf("%s",s); flag=false; memset(mark,false,sizeof(mark)); dfs(s,0); if(!flag) {     printf("Not matchn");     continue; } for(int i=0;i<n;i++) {     if(mark[i])     {         if(flag)  flag=false;         else  printf(" ");         printf("%d",i);     } } printf("n");        }    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/377233.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号