poj 1451:模拟发短信的字典树
来源:互联网 发布:网络信息安全三级等保 编辑:程序博客网 时间:2024/05/18 16:58
#include <stdio.h>
#include <string.h>
using namespace std;
char best[105];
int Max;
struct Node
{
int p;
Node* next[26];
Node(int prob)
{
p=prob;
for(int i=0;i<26;i++)
{
next[i]=NULL;
}
}
};
void AddNode(Node* root, char* p, int prob)
{
//printf("%s\n", p);
int index;
Node* np=root;
for(;*p!='\0';p++)
{
index=*p-'a';
if(np->next[index]!=NULL)
{
np->next[index]->p+=prob;
}
else
{
Node* newp=new Node(index);
np->next[index]=newp;
np->next[index]->p=prob;
}
np=np->next[index];
}
}
int num2char(int n, int &cnt)
{
switch(n)
{
case 2: cnt=3; return 'a'-'a';
case 3: cnt=3; return 'd'-'a';
case 4: cnt=3; return 'g'-'a';
case 5: cnt=3; return 'j'-'a';
case 6: cnt=3; return 'm'-'a';
case 7: cnt=4; return 'p'-'a';
case 8: cnt=3; return 't'-'a';
case 9: cnt=4; return 'w'-'a';
}
}
void dfs(char* text, int len, int dep, Node* np, int prob, char cur[105], bool &flag)
{
int i,cnt,num;
int ch;
if(dep==len)
{
if(prob > Max)
{
Max=prob;
strcpy(best, cur);
flag = true;
}
return;
}
num=text[dep]-'0';
if(num==0 || num==1)
{
return;
}
ch=num2char(num, cnt);
for(i=ch;i<ch+cnt;i++)
{
if(np->next[i]!=NULL)
{
for(int j=0;;j++)
{
if(cur[j]=='\0')
{
cur[j]=i+'a';
cur[j+1]='\0';
break;
}
}
dfs(text, len, dep+1, np->next[i], np->next[i]->p, cur, flag );
for(int j=0;;j++)
{
if(cur[j]=='\0' && j>0)
{
cur[j-1]='\0';
break;
}
}
}
}
return;
}
void Parse(Node* root, char *p, int len)
{
bool flag=false;
int i,num,cnt;
char ch, text[105],cur[105];
cur[0]='\0';
for(i=0;i<len-1;i++)
{
strncpy(text,p,i+1);
flag=false;
Max=0;
dfs(text, i+1, 0, root, 0, cur, flag);
if( flag )
{
printf("%s\n", best);
}
else
{
printf("MANUALLY\n");
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
int T,i,m,n,k,prob;
char word[105],oper[105];
scanf("%d", &T);
for(k=1;k<=T;k++)
{
printf("Scenario #%d:\n", k);
Node* root=new Node(0);
scanf("%d", &n);
for(i=0;i<n;i++)
{
scanf("%s %d", word, &prob);
AddNode(root, word, prob);
}
char cur[105];
scanf("%d", &m);
for(i=0;i<m;i++)
{
cur[0]='\0';
scanf("%s", oper);
Parse(root, oper, strlen(oper));
printf("\n");
}
printf("\n");
}
//while(1);
}
#include <string.h>
using namespace std;
char best[105];
int Max;
struct Node
{
int p;
Node* next[26];
Node(int prob)
{
p=prob;
for(int i=0;i<26;i++)
{
next[i]=NULL;
}
}
};
void AddNode(Node* root, char* p, int prob)
{
//printf("%s\n", p);
int index;
Node* np=root;
for(;*p!='\0';p++)
{
index=*p-'a';
if(np->next[index]!=NULL)
{
np->next[index]->p+=prob;
}
else
{
Node* newp=new Node(index);
np->next[index]=newp;
np->next[index]->p=prob;
}
np=np->next[index];
}
}
int num2char(int n, int &cnt)
{
switch(n)
{
case 2: cnt=3; return 'a'-'a';
case 3: cnt=3; return 'd'-'a';
case 4: cnt=3; return 'g'-'a';
case 5: cnt=3; return 'j'-'a';
case 6: cnt=3; return 'm'-'a';
case 7: cnt=4; return 'p'-'a';
case 8: cnt=3; return 't'-'a';
case 9: cnt=4; return 'w'-'a';
}
}
void dfs(char* text, int len, int dep, Node* np, int prob, char cur[105], bool &flag)
{
int i,cnt,num;
int ch;
if(dep==len)
{
if(prob > Max)
{
Max=prob;
strcpy(best, cur);
flag = true;
}
return;
}
num=text[dep]-'0';
if(num==0 || num==1)
{
return;
}
ch=num2char(num, cnt);
for(i=ch;i<ch+cnt;i++)
{
if(np->next[i]!=NULL)
{
for(int j=0;;j++)
{
if(cur[j]=='\0')
{
cur[j]=i+'a';
cur[j+1]='\0';
break;
}
}
dfs(text, len, dep+1, np->next[i], np->next[i]->p, cur, flag );
for(int j=0;;j++)
{
if(cur[j]=='\0' && j>0)
{
cur[j-1]='\0';
break;
}
}
}
}
return;
}
void Parse(Node* root, char *p, int len)
{
bool flag=false;
int i,num,cnt;
char ch, text[105],cur[105];
cur[0]='\0';
for(i=0;i<len-1;i++)
{
strncpy(text,p,i+1);
flag=false;
Max=0;
dfs(text, i+1, 0, root, 0, cur, flag);
if( flag )
{
printf("%s\n", best);
}
else
{
printf("MANUALLY\n");
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
int T,i,m,n,k,prob;
char word[105],oper[105];
scanf("%d", &T);
for(k=1;k<=T;k++)
{
printf("Scenario #%d:\n", k);
Node* root=new Node(0);
scanf("%d", &n);
for(i=0;i<n;i++)
{
scanf("%s %d", word, &prob);
AddNode(root, word, prob);
}
char cur[105];
scanf("%d", &m);
for(i=0;i<m;i++)
{
cur[0]='\0';
scanf("%s", oper);
Parse(root, oper, strlen(oper));
printf("\n");
}
printf("\n");
}
//while(1);
}
- poj 1451:模拟发短信的字典树
- 模拟android发短信
- 模拟发短信
- NYOJ 643 发短信(模拟)
- js模拟发短信version1
- 两个android模拟器实现打电话发短信的模拟状态
- 两个android模拟器实现打电话发短信的模拟状态
- android 模拟器 实现 打电话 发短信的模拟状态
- 两个android模拟器实现打电话发短信的模拟状态
- JS+CSS做一个模拟发短信的页面!
- poj 1451 T9 【字典树 + DFS 模拟智能英语输入法】
- Android模拟器模拟打电话,发短信
- POJ 1451 字典树
- 发错短信的结果
- PhoneGap发短信的Plugin
- android发短信的代码
- POJ 2001 Shortest Prefixes (字典树模拟切割)
- [字典树]poj 1451 T9
- 亲爱的,我的眼泪不多,请不要我再为你流泪好吗?
- [erlang]ets结合record的增删改查操作
- MySQL正则表达式
- 面向对象基础
- 共产党圈定的国民党57名战犯名单
- poj 1451:模拟发短信的字典树
- uva_644_Immediate Decodability
- 直方图用法示例
- 缘系天涯,一枕残梦千里殇岁月沉积着忧伤:伤感日志
- jQuery Autocomplete
- INSERT ALL和INSERT FIRST
- HighGUI参考手册
- C#程序设计(十四)----计算数列的极限值
- 关于Image Handler 2.0模块的使用讲解