[Baidu STAR] Baidu language translator (C, C ++, C)

Source: Internet
Author: User
Tags first string

Question 6 of the first round of the 2006 Baidu STAR Program Design Competition

Baidu language translator

Time Limit: 1 s

Baidu engineers pay great attention to efficiency. During the long-term development and testing process, they gradually created a set of unique ratings. They usually talk, hold meetings, and even use a lot of technical documents.

In order to allow new employees to adapt to Baidu culture more quickly and better read the company's technical documents, the Human Resources Department decided to develop a dedicated translation system, translate the acronyms and terminology in the relevant documents into daily languages.

Input data:
The input data consists of three parts:

1. The first line contains an integer n (n <= 10000), indicating the total number of Shrinkage words.

2. Followed by N rows of input, each line contains two strings separated by spaces. The first string is a scale-down language (only uppercase English characters are contained and cannot exceed 10 characters), and the second string is a daily language (excluding spaces and cannot exceed 255 characters ).

3. Documents containing abbreviations starting from n + 2 and ending with input. (The total length cannot exceed 1000000 characters)

Output Data:

Output a document that converts a scale-down into a daily language. (Converts a scaling phrase to a daily language, and other characters are retained as they are)

Input example
6

PS portal search Department

NLP Natural Language Processing

PM Product Marketing Department

HR Human Resources Department

Information Technology Promotion Department

MD Market Development Department

Baidu's departments include ps, PM, HR, PMD, MD, etc. PS also includes NLP groups.

Output example

Baidu's departments include the portal search department, product marketing department, Human Resources Department, product promotion department, and market development department. The portal search Department also includes the natural language processing team.

Note:

1. The input data is in a mix of Chinese and English, and the Chinese data adopts GBK encoding.

2. To ensure the uniqueness of the answer, the conversion of the scale-in language adopts the principle of positive maximum matching (from left to right in the positive direction. Please note that the input is an example of the translation of PMD.


Original questions transferred from: http://www.programfan.com/acm/show.asp? Qid = 112

The question itself is not very difficult, mainly because of the processing of strings and the addition of a sort. I used three languages respectively C, C ++, and C #, and all the code was compiled under vs2008. If you have any suggestions or questions, please leave a message for discussion.


C # implementation:

Using system; using system. collections. generic; using system. LINQ; using system. text; namespace Baidu language translator {class program {/* To ensure the "maximum positive matching principle", sort the abbreviations in length from large to small. */Static void sortstring (INT num, string [] suolue, string [] richang) {for (INT I = 0; I <num-1; I ++) {for (Int J = I + 1; j <num; j ++) {string temp = ""; if (suolue [J]. length> suolue [I]. length) {temp = suolue [J]; suolue [J] = suolue [I]; suolue [I] = temp; temp = richang [J]; richang [J] = richang [I]; richang [I] = temp ;}}}} /* convert the scale language in the document to the daily language */static void transform (INT num, ref string strinput, string [] suolue, string [] richang) {for (INT I = 0; I <num; I ++) {strinput = strinput. replace (suolue [I], richang [I]) ;}} static void main (string [] ARGs) {console. writeline ("Enter the number of acronyms:"); int n = convert. toint32 (console. readline (); string [] suolue = new string [N]; string [] richang = new string [N]; console. writeline ("\ n enter the specific acronyms:"); For (INT I = 0; I <n; I ++) {string S = console. readline (); suolue [I] = S. split ('') [0]; richang [I] = S. split ('') [1];} console. writeline ("\ n enter the body:"); string strinput = console. readline (); datetime start = datetime. now; // start time sortstring (n, suolue, richang); transform (n, ref strinput, suolue, richang); datetime end = datetime. now; // End Time timespan span = end. subtract (start); console. writeline ("\ N output text:"); console. writeline (strinput); console. writeline ("running time: {0} s", span. totalseconds. tostring (); console. readkey ();}}}


C ++ implementation:

# Include <iostream> # include <string> # include <ctime> using namespace STD;/* To ensure the "maximum positive matching principle ", sorts the abbreviations in length from large to small. */Void sortstring (INT num, string suolue [], string richang []) {for (INT I = 0; I <num-1; ++ I) for (Int J = I + 1; j <num; ++ J) if (suolue [I]. length () <suolue [J]. length () {suolue [I]. swap (suolue [J]); richang [I]. swap (richang [J]);}/* converts the scale language in the document to the daily language */void transform (INT num, string & S, string suolue [], string richang []) {for (INT I = 0; I <num; ++ I) {While (S. find (suolue [I])! = String: NPOs) {int Index = S. find (suolue [I]); S. erase (index, suolue [I]. length (); S. insert (index, richang [I]) ;}} int main () {clock_t start, end; int N; cout <"Enter the number of acronyms: "<Endl; CIN> N; string suolue [100], richang [100], s; // error: String suolue [N], richang [N], S; for cout <"\ n, enter the specific acronyms:" <Endl; For (INT I = 0; I <n; ++ I) {CIN> suolue [I]> richang [I];} cout <"\ n enter the body:" <Endl; CIN> S; start = clock (); // start timing sortstring (n, suolue, richang); transform (N, S, suolue, richang); cout <"\ N output text: \ n "<S <Endl; end = clock (); // cout <" running time: "<double (end-start) /1000 <"s \ n" <Endl; System ("pause"); Return 0 ;}


C implementation:

# Include <stdio. h> # include <string. h> # include <time. h> struct abbreviation {char suolue [10]; char richang [255] ;};/* to ensure "maximum positive matching principle ", sorts the abbreviations in length from large to small. */Void sortstring (INT num, struct abbreviation myabbreviations []) {for (INT I = 0; I <num-1; ++ I) for (Int J = I + 1; j <num; ++ J) if (strlen (myabbreviations [I]. suolue) <strlen (myabbreviations [J]. suolue) {struct abbreviation temp = myabbreviations [I]; myabbreviations [I] = myabbreviations [J]; myabbreviations [J] = temp ;}} /* convert the scale language in the document to the daily language */void transform (INT num, char textinput [], struct abbreviation myabbreviations []) {int lengt H; char temp [10000]; for (INT I = 0; I <num; ++ I) {char * P = strstr (textinput, myabbreviations [I]. suolue); While (P! = NULL) {length = strlen (myabbreviations [I]. suolue); * p = 0; strcpy (temp, P + length); strcat (textinput, myabbreviations [I]. richang); strcat (textinput, temp); P = strstr (textinput, myabbreviations [I]. suolue) ;}}void main () {int N; struct abbreviation myabbreviations [100]; char textinput [10000]; clock_t start, end; printf ("Enter the number of acronyms: \ n"); scanf ("% d", & N); printf ("\ n enter the specific acronyms: \ n "); For (INT I = 0; I <n; ++ I) {scanf (" % S % s ", myabbreviations [I]. suolue, myabbreviations [I]. richang);}/* getchar (); */printf ("\ n please input the body: \ n"); getchar (); gets (textinput ); start = clock (); // start time sortstring (n, myabbreviations); transform (n, textinput, myabbreviations); printf ("\ n the output text is: % s \ n ", textinput); End = clock (); // End Time printf (" running time: % g s ", double (end-Start)/1000 ); getchar ();}
Welcome to my independent technical blog | sameideal.com


Related Article

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.