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

poj 1732 Phone numbers

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

poj 1732 Phone numbers

#include<cstdio>#include<cstring>#include<map>#include<string>using namespace std;const int inf=1000000;int ma[300],lm[50020];map<string,int> md;char word[50020][70];int dp[120],pre[120];int main(){    ma['i']=ma['j']='1';    ma['a']=ma['b']=ma['c']='2';    ma['d']=ma['e']=ma['f']='3';    ma['g']=ma['h']='4';    ma['k']=ma['l']='5';    ma['m']=ma['n']='6';    ma['p']=ma['r']=ma['s']='7';    ma['t']=ma['u']=ma['v']='8';    ma['w']=ma['x']=ma['y']='9';    ma['o']=ma['q']=ma['z']='0';    char s[120];    int n;    while(scanf("%s",s+1)!=EOF)    {        md.clear();        scanf("%d",&n);        int i,j,k;        for(i=1; i<=n; i++)        { scanf("%s",word[i]); char tp[70]; for(j=0;word[i][j];j++)     tp[j]=ma[word[i][j]]; tp[lm[i]=j]=''; md[tp]=i;        }        memset(pre,-1,sizeof(pre));        for(i=110;i>0;i--) dp[i]=inf;        dp[0]=0;        for(i=1;s[i]!='';i++)        { if(dp[i-1]==inf)     continue; if(md.find(s+i)!=md.end())     {         k=md[s+i];         if(dp[i-1+lm[k]]>dp[i-1]+1)         {  dp[i-1+lm[k]]=dp[i-1]+1;  pre[i-1+lm[k]]=k;         }     } for(j=i+1;s[j]!='';j++) {     char ch=s[j];     s[j]='';     if(md.find(s+i)!=md.end())     {         k=md[s+i];         if(dp[i-1+lm[k]]>dp[i-1]+1)         {  dp[i-1+lm[k]]=dp[i-1]+1;  pre[i-1+lm[k]]=k;         }     }     s[j]=ch; }        }        if(dp[--i]==inf) printf("No solution.n");        else        { int wos[200],top=0; for(; i!=0; i-=lm[pre[i]])     wos[top++]=pre[i]; for(int i=top-1; i>0; i--)     printf("%s ",word[wos[i]]); printf("%sn",word[wos[0]]);        }    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/380585.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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