Ural 1002 Phone number
来源:互联网 发布:软件行业就业方向 编辑:程序博客网 时间:2024/04/30 17:43
dp[i]表示数字串第i位之前最少有多少单词,以数字串长度为阶段,每个阶段共有n个状态,类似01背包..不同的是,单词可以放不止一次...
转移方程dp[i]=min(dp[i],dp[i-word[j].length()]+1);关键是记录,dp[i]每更新一次状态,记录下j,最后一个j肯定用单词最短的那个单词,接着向前遍历.....
#include<iostream>#include<cstring>#include<string>using namespace std;#define MAX 50000string word[MAX+2];int res[MAX+2][52];int dp[102]; string num;int a[102];int ans[102];int w[102];const int f[27]={2,2,2,3,3,3,4,4,1,1,5,5,6,6,0,7,0,7,7,8,8,8,9,9,9,0};bool fun(int pos,int st){ int j=0; for(int i=pos-word[st].length();i<pos;i++) if(a[i]!=res[st][j++]) return false; return true;}int main(){ int n,nlen,i,j,t; while(cin>>num&&num[0]!='-') { nlen=num.length(); for(i=0;i<nlen;i++) a[i]=num[i]-'0'; cin>>n; for(i=0;i<n;i++) { cin>>word[i]; t=word[i].length(); for(j=0;j<t;j++) res[i][j]=f[word[i][j]-'a']; } for(i=0;i<=nlen;i++) {dp[i]=1<<30;w[i]=0;} dp[0]=0; for(i=1;i<=nlen;i++) { for(j=0;j<n;j++) if(i>=word[j].length()&&fun(i,j)&&dp[i-word[j].length()]+1<dp[i]) {dp[i]=dp[i-word[j].length()]+1; w[i]=j; } } if(dp[nlen]>2000) printf("No solution.\n"); else { i=nlen;int s=0; while(i>0) { ans[s++]=w[i]; i-=word[w[i]].length(); } for(i=s-1;i>0;i--) cout<<word[ans[i]]<<" "; cout<<word[ans[0]]<<endl; } }}
- Ural 1002 Phone number
- Ural 1002 Phone Number (字典树 + dp)
- ural 1002 Phone numbers
- ural 1002 phone numbers
- URAL 1002 Phone Numbers
- ural 1002 Phone Numbers 字典树+dp
- ural 1002 Phone Numbers 字典树+dp
- URAL 1002|Phone Numbers|动态规划
- URAL 1002. Phone Numbers
- Phone Number
- Phone Number
- Phone Number
- URAL 1385 Interesting Number
- URAL 1385 Interesting Number
- Encoded Phone Number help
- oz phone number regex
- sdut2151 Phone Number
- Phone Number Divide
- CMD输入路径包含空格
- c语言基础—运算符和语句
- Hibernate关联关系映射-----双向一对多/多对一映射配置
- oracle的(+)意思
- c语言基础—指针与数组
- Ural 1002 Phone number
- NDK(ngx_devel_kit)模块学习
- 使用 ACE 库框架在 UNIX 中开发高性能并发应用
- 我的ABAP学习历程
- OLTP与OLAP理解
- IT行业事件
- Windows Server 2003 x64环境下安装Oracle10.2 与 toad10.6
- StringBuffer类
- /etc/ld.so.conf文件