1. 程式人生 > 其它 >poj 1409(水題)

poj 1409(水題)

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
struct node{
    char result[305][55];
    int len;
};
node r[305];
char a[105][55],sequence[305],seqCh[305][55];
int rCnt,cnt,n,d[] = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9};
bool flag;
int cmp(node ra,node rb){
    
int len = min(ra.len,rb.len); for(int i=0;i<len;i++){ if(strcmp(ra.result[i],rb.result[i])<0)return true; if(strcmp(ra.result[i],rb.result[i])>0)return false; } } void dfs(int p,int q){ if(p==q){ r[rCnt].len = cnt; for(int i=0;i<cnt;i++){ strcpy(r[rCnt].result[i],seqCh[i]); } rCnt
++; } for(int i=0;i<n;i++){ flag = true; for(int j=0;j<strlen(a[i]);j++){ if(sequence[p+j]!=d[a[i][j]-'a']+'0'){ flag = false; break; } } if(flag){ strcpy(seqCh[cnt++],a[i]); dfs(p
+strlen(a[i]),q); cnt--; } } } int main(){ while(scanf("%d",&n)==1&&n){ for(int i=0;i<n;i++){ scanf("%s",a[i]); } scanf("%s",sequence); // for(int i=0;i<n;i++){ // for(int j=0;j<strlen(a[i]);j++){ // cout<<data[i][j]; // } // cout<<endl; // } rCnt = 0; dfs(0,strlen(sequence)); sort(r,r+rCnt,cmp); for(int i=0;i<rCnt;i++){ for(int j=0;j<r[i].len-1;j++){ cout<<r[i].result[j]<<" "; } cout<<r[i].result[r[i].len-1]<<"."<<endl; } cout<<"--"<<endl; } return 0; }