Problem E
Time Limit : 10000/5000ms (Java/Other) Memory Limit : 102400/204800K (Java/Other)
Total Submission(s) : 5 Accepted Submission(s) : 5
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Input
Output
Sample Input
START from fiwo hello difh mars riwosf earth fnnvk like fiiwj END START difh, i'm fiwo riwosf. i fiiwj fnnvk! END
Sample Output
hello, i'm from mars. i like earth!
代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <conio.h>
typedef struct Trie
{
int flag;
Trie *child[26];
char *translation;
} root, node;
root *init( )
{
root *r= ( root * )malloc( sizeof( root ) );
r->flag= 0;
memset( r-> child, 0, sizeof( r-> child ) );
return r;
}
void insert( node *parent, char *in, char *trans )
{
if( *in== '\0' )
{
parent-> flag= 1;
parent-> translation= trans;
return;
}
if( !parent-> child[ *in- 'a' ] )
parent-> child[ *in- 'a' ]= init( );
insert( parent-> child[ *in- 'a' ], in+ 1, trans );
}
bool search( node *parent, char *in )
{
if( *in== '\0' )
{
if( parent-> flag== 1 )
{
printf( "%s", parent-> translation );
return true;
}
else
return false;
}
if( !parent-> child[ *in- 'a' ] )
return false;
else
search( parent-> child[ *in- 'a' ], in+ 1 );
}
bool legal( char chr )
{
if( chr>= 'a'&& chr<= 'z' )
return true;
else
return false;
}
int main()
{
char en[12], mar[12], words[3005];
scanf( "%s", en );
root *r= init( );
while( scanf( "%s", en ), strcmp( en, "END" ) )
{
char *trans= ( char * )malloc( sizeof( en ) );
strcpy( trans, en );
scanf( "%s", mar );
insert( r, mar, trans );
}
scanf( "%s%*", en );
char c, temp[12];
int cnt= 0;
while( c= getchar( ) )
{
if( ( c< 'a'|| c> 'z' )&&( c< 'A'|| c> 'Z' ) )
{
temp[cnt]= '\0';
if( !strcmp( temp, "END" ) )
break;
if( !search( r, temp ) )
printf( "%s", temp );
printf( "%c", c );
cnt= 0;
}
else
{
temp[cnt++]= c;
}
}
return 0;
}
#include"string"
#include"map"
using namespace std;
int main()
{
string a,b;
map<string ,string>m;
cin>>a;
while(cin>>a&&a!="END")
{
cin>>b;
m[b]=a;
}
cin>>a;
char tem[3001];
getchar();
while(1)
{
gets(tem);
if(strcmp(tem,"END")==0)
break;
int i,len;
len=strlen(tem);
b="";
for(i=0;i<len;i++)
{
if(tem[i]<'a'||tem[i]>'z')
{
if(m[b]!="")
cout<<m[b];
else
cout<<b;
b="";
cout<<tem[i];
}
else
b+=tem[i];
}
cout<<endl;
}
return 0;
}