/*
好久没有写题目了,都怪自己最近太忙了,当了班长忙是有点,但是我不知为何我AC
的时间都没有了,有些事情就少做一点吧,一个团队是需要积极向上有能力的人,而不
是一个没有水平还不努力的人,所以静下心来好好地想想。。。
算法不是很多都不会么??学了数据结构你里面的算法都会了吗??
看过别人的代码只有43行?why??好好想想为何。。。。
*/
#include<iostream>//2524582 2010-06-07 22:30:40 Accepted 1075 250MS 63560K 2549 B C++ 悔惜晟
#include<cstdio>
#include<cstring>
using namespace std;
struct dictree
{
struct dictree *child[26];
char str[20];
};
struct dictree *root;
void insert(char str[], char str1[])
{
struct dictree *P, *N;
P = root;
int len, i, j;
len = strlen(str);
if(len == 0)
return ;
for(i = 0; i < len; i++)
{
if(i != len - 1)
{
if(P->child[str[i] - 'a'] != NULL)
{
P = P->child[str[i] - 'a'];
}
else
{
N = (struct dictree *)malloc(sizeof(struct dictree));
for(j = 0; j < 26; j++)
{
N->child[j] = NULL;
N->str[0] = '/0';
}
P->child[str[i] - 'a'] = N;
P = N;
}
}
else
{
if(P->child[str[i] - 'a'] != NULL)
{
P = P->child[str[i] - 'a'];
strcpy( P->str, str1);
}
else
{
N = (struct dictree *)malloc(sizeof(struct dictree));
for(j = 0; j < 26; j++)
{
N->child[j] = NULL;
N->str[0] = '/0';
}
P->child[str[i] - 'a'] = N;
P = N;
strcpy( P->str, str1);
}
}
}
}
char *find(char str[])
{
int i, len;
len = strlen(str);
struct dictree *P;
P = root;
for(i = 0; i < len; i++)
{
if(i == len - 1 )
{
if(P->child[str[len - 1] - 'a'] != NULL && P->child[str[len - 1] - 'a']->str[0] != '/0') //这里有可能有单词不存在的情况
return P->child[str[len - 1]- 'a']->str;
else
return NULL;
}
if(P->child[str[i] - 'a'] != NULL)
{
P = P->child[str[i] - 'a'];
continue;
}
else
{
return NULL;
}
}
return NULL;
}
int main()
{
char str[3005], str1[3005];
char str2[3005];
int len, i, j;
root = (struct dictree *)malloc(sizeof(struct dictree));
for(j = 0; j < 26; j++)
{
root->child[j] = NULL;
root->str[0] = '/0';
}
scanf("%s", str);
while(scanf("%s", str) != EOF)
{
if(strcmp(str, "END") == 0)
break;
scanf("%s", str1);
insert(str1, str);
}
scanf("%s", str2);
getchar();
while(gets(str2))
{
if(strcmp(str2, "END") == 0)
break;
len = strlen(str2);
j = 0;
str2[len++] = ' ';
str2[len] = '/0';//最好一个有可能是字母锋情况
for(i = 0; i < len; i++)
{
if(!(str2[i] >= 'a' && str2[i] <= 'z'))
{
str[j++] = '/0';
char *s = find(str);
if(!s)
{
printf("%s", str);
}
else
{
printf("%s", s);
}
j = 0;
if(i != len - 1)
printf("%c", str2[i]);
}
else
{
str[j++] = str2[i];
}
}
printf("/n");
}
}
/*
好强大的STL。但是为何效率不高呢?看来字符串完全可以这个hash了
*/
#include<iostream>//2524740 2010-06-07 23:18:42 Accepted 1075 1656MS 37584K 631 B C++ 悔惜晟
#include<cstdio>
#include<map>
#include<string>
using namespace std;
map<string, string> df;
int main()
{
string x, z;
cin>>x;
while(cin>>x && x !="END")
{
cin>>z;
df[z] = x;
}
cin>>x;
getchar();//吃回车
char str[3005];
while(gets(str) && strcmp(str, "END") != 0)
{
int i, len;
z = "";
len = strlen(str);
for(i = 0; i < len; i++)
{
if(!(str[i] >= 'a' && str[i] <= 'z'))
{
if(df[z] == "")
{
cout<<z;
}
else
{
cout<<df[z];
}
z = "";
cout<<str[i];
}
else
z += str[i];
}
cout<<endl;
}
}