When calculating the common data items in two string linked lists, you need to consider repeated options.

Source: Internet
Author: User

Problem
/*
// Given two lists of strings build a new list that has all strings that appear in both the original lists. if the same string appears more than once output it as same times as it appears in both lists
//
// Example:
// "Dog", "bird", "elephant", "dog", "dog", "cat"
// "Cat", "dog", "dog", "cat", "cat", "fish"
// Result (order doesn' t matter)
// "Dog", "dog", "cat"


*/


Solution
[Cpp]
# Include <iostream>
# Include <list>
# Include <iterator>
# Include <algorithm>
# Include <string>
 
Using namespace std;
 
Void find_comm_strings (list <string> & output, list <string> & listA, list <string> & listB)
{
ListA. sort ();
ListB. sort ();
 
List <string >:: const_iterator citA = listA. begin ();
List <string >:: const_iterator citB = listB. begin ();
 
While (citA! = ListA. end () & citB! = ListB. end ()){
Int eq = (* citA). compare (* citB );
If (eq = 0 ){
Output. push_back (* citA );
CitA ++;
CitB ++;
}
Else if (eq> 0 ){
CitB ++;
}
Else {
CitA ++;
}
}
}
 
Int main (int argc, char * argv [])
{
List <string> listA;
List <string> listB;
List <string> output;
 
Cout <"list A:" <endl;
ListA. push_back ("dog ");
ListA. push_back ("bird ");
ListA. push_back ("elephant ");
ListA. push_back ("dog ");
ListA. push_back ("dog ");
ListA. push_back ("cat ");
Copy (listA. begin (), listA. end (), ostream_iterator <string> (cout ,","));
Cout <endl;
 
Cout <"list B:" <endl;
ListB. push_back ("cat ");
ListB. push_back ("dog ");
ListB. push_back ("dog ");
ListB. push_back ("cat ");
ListB. push_back ("cat ");
ListB. push_back ("fish ");
Copy (listB. begin (), listB. end (), ostream_iterator <string> (cout ,","));
Cout <endl;
 
Find_comm_strings (output, listA, listB );
 
Cout <"common strings" <endl;
Copy (output. begin (), output. end (), ostream_iterator <string> (cout ,","));
Cout <endl;
 
Return 0;
}

# Include <iostream>
# Include <list>
# Include <iterator>
# Include <algorithm>
# Include <string>

Using namespace std;

Void find_comm_strings (list <string> & output, list <string> & listA, list <string> & listB)
{
ListA. sort ();
ListB. sort ();

List <string >:: const_iterator citA = listA. begin ();
List <string >:: const_iterator citB = listB. begin ();

While (citA! = ListA. end () & citB! = ListB. end ()){
Int eq = (* citA). compare (* citB );
If (eq = 0 ){
Output. push_back (* citA );
CitA ++;
CitB ++;
}
Else if (eq> 0 ){
CitB ++;
}
Else {
CitA ++;
}
}
}

Int main (int argc, char * argv [])
{
List <string> listA;
List <string> listB;
List <string> output;

Cout <"list A:" <endl;
ListA. push_back ("dog ");
ListA. push_back ("bird ");
ListA. push_back ("elephant ");
ListA. push_back ("dog ");
ListA. push_back ("dog ");
ListA. push_back ("cat ");
Copy (listA. begin (), listA. end (), ostream_iterator <string> (cout ,","));
Cout <endl;

Cout <"list B:" <endl;
ListB. push_back ("cat ");
ListB. push_back ("dog ");
ListB. push_back ("dog ");
ListB. push_back ("cat ");
ListB. push_back ("cat ");
ListB. push_back ("fish ");
Copy (listB. begin (), listB. end (), ostream_iterator <string> (cout ,","));
Cout <endl;

Find_comm_strings (output, listA, listB );

Cout <"common strings" <endl;
Copy (output. begin (), output. end (), ostream_iterator <string> (cout ,","));
Cout <endl;

Return 0;
}
Output
[Cpp]
List:
Dog, bird, elephant, dog, dog, cat,
List B:
Cat, dog, dog, cat, cat, fish,
Common strings
Cat, dog, dog,
Press any key to continue...

List:
Dog, bird, elephant, dog, dog, cat,
List B:
Cat, dog, dog, cat, cat, fish,
Common strings
Cat, dog, dog,
Press any key to continue...

 

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.