[String + map] analyzes the STL practices and some traps of poj 1002--487-3279

Source: Internet
Author: User

Question: Click to open the link

Find the duplicate number. The first reaction is map (which they use for hash), which is easy to write (I am a pointer to the weak slag). However, some traps have been found during actual application, leading to several wa.

The principle is very simple: Create a table, convert it to a number, ignore the horizontal bar, add it to the map, and automatically open the memory ++ according to the features automatically managed by the map (automatically keep the ascending order, etc) the number of times repeated.

But the trap is also quite troublesome. It may be related to my strange thinking.

1. Delete the horizontal bar.

The following code looks right ?? Delete a horizontal bar ~

Error 1:

string::iterator it;for(it=tar.begin();it!=tar.end();it++)   {if((*it)=='-'){tar.erase(it);  }}

However, if this statement is used to delete the horizontal bar, illegal memory will be reported. Why? Do not believe that you write a 123-4-5-6--7 -- try. That is, two horizontal bars cannot be prevented, and the last horizontal bars cannot be prevented. After deletion, the buffer zone is generated ..

Positive Solution. Save it with another string res ..

for(int p=0;p<tar.size();p++){if(isdigit(tar[p]))res+=tar[p];}

2. Two storage methods.

Incorrect solution: Map ["1234567"] ++ is used for saving. If you update the Statement by the overloaded operator [], it is very troublesome to search all the known parameters.

Positive Solution: Situation Analysis

map<string,int>::iterator apper=phonepack.find(conv);if(apper==phonepack.end()){ phonepack.insert(map<string,int>::value_type(conv,1));}else{phonepack[conv]++;}

3. Pay attention to the absence of output, which is a pure read question.

Based on the above situation, the AC is finally available, although there is also a point card time of 1700/2000 Ms...

# Include <string> # include <map> # include <algorithm> # include <ctype. h> # include <stdlib. h ># include <iostream> using namespace STD; Map <string, int> phonepack; string phonecall (string TAR) {string res; For (INT I = 0; I <tar. size (); I ++) {If (TAR [I] = 'A' | tar [I] = 'B' | tar [I] = 'C') tar. replace (I, 1, "2 "); else if (TAR [I] = 'D' | tar [I] = 'E' | tar [I] = 'F') tar. replace (I, 1, "3"); else if (TAR [I] = 'G' | tar [I] = 'H' | tar [I] = 'I') tar. replace (I, 1, "4 "); else if (TAR [I] = 'J' | tar [I] = 'K' | tar [I] = 'l') tar. replace (I, 1, "5 "); else if (TAR [I] = 'M' | tar [I] = 'n' | tar [I] = 'O') tar. replace (I, 1, "6 "); else if (TAR [I] = 'P' | tar [I] = 'R' | tar [I] = 's') tar. replace (I, 1, "7 "); else if (TAR [I] = 'T' | tar [I] = 'U' | tar [I] = 'V') tar. replace (I, 1, "8 "); else if (TAR [I] = 'W' | tar [I] = 'X' | tar [I] = 'y') tar. replace (I, 1, "9");}/* string: ITER Ator it; for (IT = tar. Begin (); it! = Tar. end (); It ++) // if you do not add this sentence, the system may read invalid memory because the identifier is deleted, -Many errors may occur {If (* It) = '-') {tar. erase (it); cout <tar <Endl ;}} */For (INT p = 0; P <tar. size (); P ++) {If (isdigit (TAR [p]) RES + = tar [p];} return res;} int main () {int testcase; map <string, int >:: iterator it2; CIN> testcase; For (INT I = 0; I <testcase; I ++) {string region; string Conv; cin> region; Conv = phonecall (region); Map <string, int >:: iterator apper = phonepack. find (Conv); If (apper = Phonepack. end () {phonepack. insert (Map <string, int >:: value_type (Conv, 1) ;}else {phonepack [Conv] ++ ;}} int Pos = 0; // initialization, wa again for (it2 = phonepack. begin (); it2! = Phonepack. end (); it2 ++) {If (* it2 ). second> 1) // The output format is troublesome {for (INT I = 0; I <= 2; I ++) cout <(* it2 ). first [I]; cout <"-"; for (Int J = 3; j <= 6; j ++) cout <(* it2 ). first [J]; cout <"<(* it2 ). second <Endl; POS ++ ;}} if (Pos = 0) {cout <"no duplicates. "<Endl;} 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.