Untitled

mail@pastecode.io avatar
unknown
c_cpp
6 months ago
1.0 kB
6
Indexable
Never
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    string textinput;
    getline(cin, textinput);
    stringstream ss(textinput);
    
    string word;
    map<string, int> wordCount;
    
    // Count the occurrences of each word
    while(ss >> word) {
        wordCount[word]++;
    }
    
    vector<string> repeatedWords;
    
    // Check which words have been repeated
    for(auto &it: wordCount) {
        if(it.second > 1) {
            repeatedWords.push_back(it.first);
        }
    }

    // Sort words lexicographically
    sort(repeatedWords.begin(), repeatedWords.end());

    if(repeatedWords.empty()) {
        cout << "NA" << endl;
    } else {
        for(size_t i = 0; i < repeatedWords.size(); ++i) {
            cout << repeatedWords[i];
            if(i < repeatedWords.size() - 1) cout << " ";
        }
        cout << endl;
    }

    return 0;
}