COMP2113
COMP2113_ENGG1340 Programming technologies and Computer programming II [Section 2BC] [2023]
Loading...
Searching...
No Matches
1.cpp File Reference
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
Include dependency graph for 1.cpp:

Go to the source code of this file.

Functions

int main (void)
 

Function Documentation

◆ main()

int main ( void )

Definition at line 6 of file 1.cpp.

6 {
7 std::vector<std::string> inputs;
8 std::string temp;
9 while (true) {
10 std::getline(std::cin, temp);
11 if (temp == "!") {
12 break;
13 }
14 else {
15 if (temp.size() > 1) {
16 inputs.push_back(temp);
17 }
18 }
19 }
20 struct Occurrence {
21 std::string bigram[2];
22 unsigned int count;
23 };
24
25 std::vector<Occurrence> occurrences;
26 for (unsigned int i = 0; i < inputs.size(); i++) {
27 for (unsigned int j = 0; j < inputs[i].size() - 1; j++) {
28 bool found = false;
29 for (unsigned int k = 0; k < occurrences.size(); k++) {
30 if (occurrences[k].bigram[0] == inputs[i].substr(j, 1) &&
31 occurrences[k].bigram[1] == inputs[i].substr(j + 1, 1)
32 ) {
33 occurrences[k].count++;
34 found = true;
35 break;
36 }
37 }
38 if (!found) {
39 Occurrence temp;
40 temp.bigram[0] = inputs[i].substr(j, 1);
41 temp.bigram[1] = inputs[i].substr(j + 1, 1);
42 temp.count = 1;
43 occurrences.push_back(temp);
44 }
45 }
46 }
47 Occurrence mostCommon, currentBigram;
48 for (unsigned int i = 0; i < occurrences.size(); i++) {
49 if (i == 0) {
50 mostCommon = occurrences[i];
51 }
52 else {
53 currentBigram = occurrences[i];
54 if (currentBigram.count > mostCommon.count) {
55 mostCommon = currentBigram;
56 }
57 }
58 }
59 std::sort(inputs.begin(), inputs.end());
60 for (unsigned int i = 0; i < inputs.size(); i++) {
61 if (inputs[i].find(mostCommon.bigram[0] + mostCommon.bigram[1]) != std::string::npos) {
62 std::cout << inputs[i] << std::endl;
63 }
64 }
65 return 0;
66}
Node * find(Node *head, int num)
Definition ex4ex5.cpp:62

References find().

Here is the call graph for this function: