• nullnull计算两个字符串链表中的共同数据项,需要考虑重复选项的情况


    在写这篇文章之前,xxx已经写过了几篇关于改nullnull主题的文章,想要了解的朋友可以去翻一下之前的文章

        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 many 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
    #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
    list A:
    dog,bird,elephant,dog,dog,cat,
    list B:
    cat,dog,dog,cat,cat,fish,
    common strings
    cat,dog,dog,
    Press any key to continue . . .


    文章结束给大家分享下程序员的一些笑话语录: 问:你觉得让你女朋友(或者任何一个女的)从你和李彦宏之间选一个,你觉得她会选谁?  
      答:因为李艳红这种败类,所以我没女友!

  • 相关阅读:
    linux 查看磁盘空间大小
    Redis内存碎片率
    redis的incr和incrby命令
    redis如何清空当前缓存和所有缓存
    ArcGIS矢量数据批量合并工具
    arcgis 获得工具有多少个
    GoogleEarth二次开发难点和技巧
    ArcGIS 智能批量赋高程工具
    arcgis python支持汉字
    ArcGIS 宗地图批量打印输出
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3093352.html
Copyright © 2020-2023  润新知