/* * File: RandomWriter.cpp * ---------------------- * Name: Ana Gurgenidze * Section: #2, Michael Nemsitsveridze * This file is the final project for the Random Writer problem. * */ #include #include #include "simpio.h" #include "random.h" #include "vector.h" #include "map.h" #include "console.h" using namespace std; int main() { Map > data; ifstream inputFile; inputFile.open("TomSawyer.txt"); int k = getInteger("Enter k:"); string str; char c; for (int i = 0; i < k; i++) { if (inputFile.fail()) break; inputFile.get(c); str += c; } while (true) { if (inputFile.fail()) break; inputFile.get(c); data[str].add(c); data.put(str, data[str]); str = str.substr(1, str.length() - 1) + c; } string max; int maxSize = 0; foreach (string str in data) { if (data.get(str).size() > maxSize) { maxSize = data.get(str).size(); max = str; } } string result = max; string tmp = max; while (result.size() < 2000) { int randomIndex = randomInteger(0, k - 1); // ?????? char ch = data.get(tmp).get(randomIndex); result += ch; tmp = tmp.substr(1, k - 1) + ch; } cout << result << endl; inputFile.close(); return 0; }