方法一:Trie
思路:
1. 开辟一个结构体数组dict,数组中存放索引值和索引,比如from fiwo
2. 建立一颗Trie,Trie维护 整型变量 index,结构体指针数组 branch,在建立Trie的过程中,若是单词结尾,Trie中index赋值为dict的索引,否则为-1
写法一:string类降低编码复杂度,当然代价是时间复杂度加大约 2 倍
/*
PROG: What Are You Talking About
ID :
LANG: C++
*/
#include <map>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <memory.h>
#include <algorithm>
using namespace std;
struct Dictionary
{
string front, back;
}dict[500004];
struct Trie_Node
{
int index;
Trie_Node *branch[27];
Trie_Node(): index( -1 )
{
memset( branch, 0, sizeof(branch) );
}
};
class Trie
{
public:
Trie();
~Trie() { }
void Trie_Insert( int k, string ss );
int Trie_Find( string ss );
private:
Trie_Node *root;
}t;
/* Valiable */
int M=0;
Trie::Trie()
{
root = new Trie_Node();
}// Trie
void Trie::Trie_Insert( int k, string ss )
{
Trie_Node *ptr = root;
int slen = ss.length();
for ( int i=0; i<slen; ++i )
{
if ( ptr->branch[ ss[i]-'a' ]==NULL )
{
Trie_Node *temp = new Trie_Node();
ptr->branch[ ss[i]-'a' ] = temp;
}
ptr = ptr->branch[ ss[i]-'a' ];
}
ptr->index = k;
}// Trie_Insert
int Trie::Trie_Find( string ss )
{
Trie_Node *ptr = root;
int slen = ss.length();
for ( int i=0; i<slen; ++i )
{
if ( ptr->branch[ ss[i]-'a' ]!=NULL )
ptr = ptr->branch[ ss[i]-'a' ];
else
return -1;
}
return ptr->index;
}// Trie_Find
void ReadData()
{
string s1, s2;
cin >> s1; // Words " START "
while ( cin >> s1 && s1!="END" )
{
cin >> s2;
dict[M].front = s1;
dict[M].back = s2;
t.Trie_Insert( M++, s2 ); // Note " M++ "
}// dictionary
}// ReadData
void Solve()
{
int slen, kk;
string words, line;
cin >> line; // Words " START "
getchar();
while ( getline( cin, line ) && line!="END" )
{
words = "";
slen = line.length();
for ( int i=0; i<slen; ++i )
{
if ( isalpha( line[i] ) )
{
words += line[i];
}
else
{
kk = t.Trie_Find( words );
if ( kk==-1 )
cout << words;
else
cout << dict[kk].front;
cout << line[i];
words = "";
}
}// scanning
cout << endl;
}// books
}// Solve
int main()
{
ReadData();
Solve();
return 0;
}
View Code
/*
PROG: What Are You Talking About
ID :
LANG: C++
*/
#include <map>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <memory.h>
#include <algorithm>
using namespace std;
const int maxn = 15;
struct Dictionary
{
char front[maxn];
char back[maxn];
}dict[500004];
struct Trie_Node
{
int index;
Trie_Node *branch[27];
Trie_Node(): index( -1 )
{
memset( branch, 0, sizeof(branch) );
}
};
class Trie
{
public:
Trie();
~Trie() { }
void Trie_Insert( int k, char ss[] );
int Trie_Find( char ss[] );
private:
Trie_Node *root;
}t;
/* Valiable */
int M=0;
Trie::Trie()
{
root = new Trie_Node();
}// Trie
void Trie::Trie_Insert( int k, char ss[] )
{
Trie_Node *ptr = root;
int slen = strlen( ss );
for ( int i=0; i<slen; ++i )
{
if ( ptr->branch[ ss[i]-'a' ]==NULL )
{
Trie_Node *temp = new Trie_Node();
ptr->branch[ ss[i]-'a' ] = temp;
}
ptr = ptr->branch[ ss[i]-'a' ];
}
ptr->index = k;
}// Trie_Insert
int Trie::Trie_Find( char ss[] )
{
Trie_Node *ptr = root;
int slen = strlen( ss );
for ( int i=0; i<slen; ++i )
{
if ( ptr->branch[ ss[i]-'a' ]!=NULL )
ptr = ptr->branch[ ss[i]-'a' ];
else
return -1;
}
return ptr->index;
}// Trie_Find
void ReadData()
{
char s1[maxn], s2[maxn];
scanf("%s", &s1); // Words " START "
while ( EOF!=scanf("%s", s1), strcmp(s1, "END")!=0 )
{
scanf("%s", &s2);
strcpy( dict[M].front, s1 );
strcpy( dict[M].back, s2 );
t.Trie_Insert( M++, s2 ); // Note " M++ "
}// dictionary
}// ReadData
void Solve()
{
int slen, tlen, kk;
char words[maxn], line[3004];
scanf("%s", line); // Words " START "
getchar();
while ( gets( line ), strcmp( line, "END" )!=0 )
{
tlen = 0;
slen = strlen( line );
memset( words, 0, sizeof(words) );
for ( int i=0; i<slen; ++i )
{
if ( isalpha( line[i] ) )
{
words[ tlen++ ] = line[i];
}
else
{
kk = t.Trie_Find( words );
if ( kk==-1 )
printf("%s", words);
else
printf("%s", dict[kk].front);
printf("%c", line[i]);
tlen = 0;
memset( words, 0, sizeof(words) );
}
}// scanning
printf("\n");
}// books
}// Solve
int main()
{
ReadData();
Solve();
return 0;
}
方法二:STL MAP
编码复杂度低,时间复杂度差
/*
PROG: What Are You Talking About
ID :
LANG: C++
*/
#include <map>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <memory.h>
#include <algorithm>
using namespace std;
int slen;
string s1, s2, line, result;
map <string, string> data;
int main()
{
cin >> s1; // START
while ( cin >> s1, s1!="END" )
{
cin >> s2;
data[ s2 ] = s1;
}// dictionary
cin >> s1;
getchar(); // fetch carry reture
while ( getline( cin, line ) && line!="END" )
{
result = "";
slen = line.length();
for ( int i=0; i<slen; ++i )
{
if ( isalpha( line[i] ) )
{
result += line[i];
}
else
{
if ( data[ result ]=="" )
cout << result;
else
cout << data[ result ];
cout << line[i];
result = "";
}
}// scanning
cout << endl;
}// books
return 0;
}