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

poj 1072 Puzzle Out

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

poj 1072 Puzzle Out

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;struct node{   int right,child;   char ch;   bool end;}tree[350010];int index = 1,cnt = 0,mat[100],tl[100];char answer[128],key[125],list[100][30];bool used[128],findans,matched[100];void insert(int p,char *word){   if (tree[p].ch){       if (tree[p].ch == *word){if (!*(word + 1)) tree[p].end = 1;else{    if(!tree[p].child) tree[p].child = ++index;    insert(tree[p].child,word + 1);}       }       else{if (tree[p].right) insert(tree[p].right,word);else {    tree[p].right = ++ index;    insert(tree[p].right,word);}       }   }   else{       tree[p].ch = *word;       if (!*(word + 1)) tree[p].end = 1;       else{tree[p].child = ++ index;insert(tree[p].child,word + 1);       }   }}int findwordmat(){   for (int i = 0;i < cnt;i ++){       mat[i] = 0;       for (int j = 0;j < tl[i];j ++)if (key[list[i][j]])    ++ mat[i];   }}int subsearch(int cur,int wi,int num,int tnum);int search(int cur){   if (cur == cnt){       if (findans) return 2;       findans = 1;       for (int i = 'A';i <= 'Z';i ++) answer[key[i]] = i;       return 1;   }   findwordmat();   int m = -1,wi;   for (int i = 0;i < cnt;i ++)       if (!matched[i] && mat[i] > m){m = mat[i];wi = i;       }   return subsearch(cur,wi,0,1);}int subsearch(int cur,int wi,int num,int tnum){   if (num == tl[wi]){       matched[wi] = true;       int tmp = search(cur + 1);       matched[wi] = false;       return tmp;   }   if (!tnum) return 0;   char cc = list[wi][num];   if (key[cc]){       for (;tnum;tnum = tree[tnum].right)if (tree[tnum].ch == key[cc]){    if (num + 1 == tl[wi] && !tree[tnum].end) return 0;    return subsearch(cur,wi,num + 1,tree[tnum].child);}       return 0;   }else{       int find = 0;       for (;tnum;tnum = tree[tnum].right)if (!used[tree[tnum].ch]){    if (num + 1 == tl[wi] && !tree[tnum].end) continue;    used[tree[tnum].ch] = true;    key[cc] = tree[tnum].ch;    int tmp = subsearch(cur,wi,num + 1,tree[tnum].child);    key[cc] = 0;    used[tree[tnum].ch] = false;    if (tmp == 2) return tmp;    if (tmp == 1) find = tmp;}       return find;   }}int main(){   char word[130];   int w,t,tmp,pd,T;   scanf("%d",&w);   for (int i = 1;i <= w;i ++){       scanf("%s",word);       insert(1,word);   }   scanf("%d",&T);   while (T --){       memset(list,0,sizeof list);       cnt = 0,tmp = 0,pd = 0;       findans = 0;       memset(key,0,sizeof key);       memset(answer,0,sizeof answer);       char c;       while (c = getchar(),c < 'A' || c > 'z');       for (;;){if (c >= 'A' && c <= 'Z'){    pd = false; list[cnt][tmp ++] = c;}else if (c == ' '){    pd = false;    if (tmp){        tl[cnt] = tmp;list[cnt ++][tmp] = 0;        tmp = 0;    }}else if (c == 'n'){    if (tmp){        tl[cnt] = tmp;list[cnt ++][tmp] = 0;        tmp = 0;    }    if (pd) break;    pd = 1;}if (scanf("%c",&c) == EOF){    if (tmp){        tl[cnt] = tmp;list[cnt ++][tmp] = 0;        tmp = 0;break;    }}       }       int tmp = search(0);       if (tmp == 0) puts("#No solution#");       else if (tmp == 1){for (int i = 'A';i <= 'Z';i ++){    if (answer[i]) printf("%c",answer[i]);    else printf("*");}printf("n");       }else{puts("#More than one solution#");       }}   return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/375025.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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