Baidu STAR Program Design Competition-Preliminary Round 3-string replacement

Source: Internet
Author: User

Question 3 (100 points for all four questions): String replacement (30 points)

Description: Please writeProgram, Replace the string in the text with another string based on the specified correspondence.

Input data: bytes is another text (including Chinese) indicating the correspondence between string (S1) and string (S2), which is about 10 thousand rows, each line contains two strings (S1 and S2) separated by a \ t or space. The S1 of each row in dict.txt is not sorted and may be duplicated. In this case, the S2. Each string in text.txtand dict.txt may contain any character except whitespace. Only S1 in text.txt and dict.txt can be replaced. (For convenience, you can download the text.txtand dict.txt files. The input files with different content will be used during actual running .)

Output Data: print the entire text after text.txtis replaced by dict.txt on the standard output.

Rating criteria: the program output result must be correct. The smaller the memory usage, the better the program execution time.

My program:

# Pragma warning (Disable: 4786 )
# Include < String >
# Include < Map >
# Include < Fstream >
# Include < Cassert >
# Include < Cstdio >

Using   Namespace STD;

Map <   String , String   > Dict;

Void Loaddict ( Const   Char   * Filename)
{
String A, B;
Ifstream DIC;
Assert (filename ! = Null );
Dic. Open (filename );
While (DIC > A > B)
{
Dict [A]=B;
}
Dic. Close ();
}

Const   Char   * Replaceword ( Const   String   & Word)
{
Map <   String , String   > : Iterator word2 = Dict. Find (Word );
If (Word2 = Dict. End ())
{
ReturnWord. c_str ();
}
Else
{
ReturnWord2->Second. c_str ();
}
}

Int Main ()
{
Bool Ischinese =   False ;
Char C;
String Word;
Loaddict ( " Dict.txt " );

File * Fp = Fopen ( " Text.txt " , " RT " );
While ( ! Feof (FP ))
{
C = Fgetc (FP );
If (Ischinese)
{
Word+ =C;
Ischinese= False;
}
Else
{
If (C & 0x80) = 0 & isspace (c ))
{
If ( ! Word. Empty ())
{
Printf ("% S", Replaceword (Word ));
Word= "";
}
Printf ( " % C " , C );
}
Else
{
If (C &   0x80 )
{
Ischinese= True;
}
Word + = C;
}
}
}
Fclose (FP );

If ( ! Word. Empty ())
{
Printf ("% S", Replaceword (Word ));
}
Return   0 ;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.