zoukankan      html  css  js  c++  java
  • 检查随机序列重复[C++]

    /*
     * File:   Main.cpp
     * Author: 88250 <DL88250@gmail.com>, http://blog.csdn.net/DL88250
     *
     * Created on May 13, 2008, 6:25 PM
     */

    #include <iostream>
    #include <fstream>
    #include <algorithm>
    #include <vector>
    #include <time.h>

    using namespace std;

    /**
     * Check the same record in a file.
     *
     * Every record in data file is a random serial, like the followings:
     * // data file
     * 1902323484354370234844
     * 1928473090393719374
     * ....
     */

    vector<string> records; // store the data records
    vector<vector<string> > statistics; //statistics

    /**
     * Read the records from the data file which named "data.txt" into memory,
     * using a list store them.
     */
    void readRecords() {
        cout << "Get starting read records...." << endl;
        ifstream fin("data.txt");

        if (!fin) {
            cout << "Cannot open input file!" << endl;
            return;
        }

        string aLine;
        while (getline(fin, aLine)) {
            records.push_back(aLine);
        }
        fin.close();
        cout << "The amount of records: " << records.size() << endl;
    }

    /**
     * Display the data records in console.
     * @param amount display amount, start from {@link #records}'s beginning
     */
    void displayRecords(int amount) {
        if (amount < 0 || amount > records.size()) {
            cout << "The specified amount exceeds the Data records" <<
                    "size!" << endl;
        }
        cout << "Display: " << endl;
        for (int i = 0; i < amount; i++) {
            cout << records.at(i) << endl;
        }
        cout << endl;
    }

    /**
     * Display the statistic results in console.
     */
    void displayStats() {
        cout << "Statistics: " << endl;
        cout << "A amount of the same data records: " << statistics.size() << endl;
        for (int i = 0; i < statistics.size(); i++) {
            vector<string> aEqualities = statistics.at(i);
            cout << aEqualities.at(0) << " occurs " << aEqualities.size() << endl;
        }
    }
    /*
    bool greater(string s1, string s2){
        return s1.compare(s2);
    }
     */

    /**
     * Check the same data records.
     */
    void checkTheSame() {

        sort(records.begin(), records.end()); // sort them

        // displayRecords(10);
        for (int i = 0; i < records.size() - 1; i++) {
            string record1 = records.at(i);
            string record2 = records.at(i + 1);
            if (record1 == record2) {
                vector<string> equalities;
                equalities.push_back(record1);
                equalities.push_back(record2);
                statistics.push_back(equalities);
            }
        }
        displayStats();
    }

    int main(int argc, char** argv) {
        readRecords();
      
        displayRecords(10);
        checkTheSame();
        cout << "Elapsed time: " << clock() / CLOCKS_PER_SEC << endl;
        return (EXIT_SUCCESS);
    }


  • 相关阅读:
    POJ 1182 食物链(带权并查集)
    UVa 10655 n次方之和(矩阵快速幂)
    2016湘潭邀请赛—Heartstone
    2016湘潭邀请赛—Gambling
    UVa 10375 选择与除法(唯一分解定理)
    UVa 1637 纸牌游戏(全概率公式)
    POJ 2443 Set Operation(压位加速)
    UVa 11248 网络扩容(最大流(需要优化))
    51Nod 1737 配对(树的重心)
    51Nod 1070 Bash游戏 V4(斐波那契博弈)
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6470278.html
Copyright © 2011-2022 走看看