Cod sursa(job #1811164)

Utilizator enacheionutEnache Ionut enacheionut Data 20 noiembrie 2016 21:38:01
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.57 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <unordered_map>

using namespace std;

void ReadInput(string &foundText, unordered_map<string, bool> &words,
               unsigned int &wordLength)
{
    string word;
    ifstream inputStream("abc2.in");

    inputStream>> foundText >> word;
    wordLength = word.size();
    words[word] = 1;

    while( !inputStream.eof() )
    {
        inputStream>> word;
        if( !words[word] )
        {
            words[word] = true;
        }
    }
    inputStream.close();
}

int SearchWoords(string &foundText, unordered_map<string, bool> &words,
                 unsigned int wordLength)
{
    unsigned int countWords = 0;
    unsigned int foundTextLength = foundText.size();
    string text = "";

    for( unsigned int index = 0; index <= foundTextLength; ++index )
    {
        if( text.size() < wordLength )
        {
            text += foundText[index];
        }
        else
        {
            if( words[text] )
            {
                ++countWords;
            }

            text.erase(text.begin());
            if( index != foundTextLength )
            {
                text += foundText[index];
            }
        }
    }
    return countWords;
}

int main()
{
    unsigned int wordLength;
    string foundText;
    unordered_map<string, bool> words;

    ReadInput(foundText, words, wordLength);

    ofstream outputStream("abc2.out");
    outputStream<< SearchWoords(foundText, words, wordLength);
    outputStream.close();

    return 0;
}