Created
January 8, 2024 17:30
-
-
Save juanfal/560bcc48c81fc95c9169e00fb1757495 to your computer and use it in GitHub Desktop.
unique words first position entered
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// x1.words.cpp | |
// juanfc 2024-01-08 | |
// | |
#include <iostream> | |
#include <array> | |
using namespace std; | |
const int MAX_DIFF_WORDS = 1000; | |
const int MAX_REP = 1000; | |
typedef array<int,MAX_REP> TPos; | |
struct TWord { | |
string w; | |
TPos pos; | |
int n; | |
}; | |
typedef array<TWord,MAX_DIFF_WORDS> TWords; | |
void add(TWords& ws, string w, int pos); | |
void printWords(TWords ws); | |
int main() | |
{ | |
TWords ws; | |
string w; | |
int pos = 0; | |
while (cin >> w and w != "end") | |
add(ws, w, ++pos); | |
printWords(ws); | |
return 0; | |
} | |
void add(TWords& ws, string w, int pos) | |
{ | |
int i = 0; | |
while (ws[i].w != "" and ws[i].w != w) | |
++i; | |
if (ws[i].w == "") { | |
ws[i].w = w; | |
ws[i].pos[0] = pos; | |
ws[i].n = 1; | |
} else { | |
ws[i].pos[ ws[i].n++ ] = pos; | |
} | |
} | |
void printWords(TWords ws) | |
{ | |
for (int i = 0; ws[i].w != ""; ++i) { | |
cout << ws[i].w << ": "; | |
for (int j = 0; j < ws[i].n; ++j) { | |
cout << ws[i].pos[j] << ", "; | |
} | |
cout << endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment