Cod sursa(job #3296624)

Utilizator stefanchpStefan Chiper stefanchp Data 14 mai 2025 17:51:52
Problema Factorial Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.76 kb
#include <iostream>
#include <queue>
#include <stack>
using namespace std;

class Stiva2Cozi {
private:
    queue<int> q1, q2;

public:
    /// Metoda1: adaugam toate elementele intr-o coada, iar atunci cand avem operatia de pop/top, le mutam in cealalta pe toate 
    /// mai putin pe ultimul pe care il eliminam/returnam, apoi punem elementele inapoi in prima coada
    /// complexitate push: O(1)
    /// complexitate pop: O(n)
    /// Metoda 2: inainte de a aduga un element in coada le mutam pe toate in cealalta si dupa adaugare le punem inapoi
    /// astfel coada va tine elementele in ordine inversa
    /// complexitate push: O(n) -> data de mutarea elementelor
    /// complexitate pop: O(1)
    void push(int x)
    {
        q1.push(x);
    }
    void pop() 
    {
        if (q1.empty()) 
        {
            cout << "Stiva este goala!" << endl;
            return;
        }
        while (q1.size() > 1) 
        {
            q2.push(q1.front());
            q1.pop();
        }
        q1.pop();
        swap(q1, q2);
    }
    int top() 
    {
        if (q1.empty()) 
        {
            cout << "Stiva este goala!" << endl;
            return -1;
        }
        int topElement;
        while (q1.size() > 1) 
        {
            q2.push(q1.front());
            q1.pop();
        }
        topElement = q1.front();
        q1.pop();
        q2.push(topElement);
        swap(q1, q2);
        return topElement;
    }
    bool empty() 
    {
        return q1.empty();
    }
};

class Solution {
public:
    #include <unordered_map>
    vector<int> findSubstring(string s, vector<string>& words) {
        vector<int> result;
        unordered_map<string, int> map;
        int length = words[0].size(), length2 = s.size();
        for (auto word:words)
            map[word]++;

        for (int offset = 0; offset < length; offset++) 
        {
            int size_ = 0;
            unordered_map<string, int> seen;
            for (int i = offset; i + length <= length2; i += length) 
            {
                string sub = s.substr(i, length);
                if (map.find(sub) == map.end()) 
                {
                    seen.clear();
                    size = 0;
                    continue;
                }
                seen[sub]++;
                size_++;
                while (seen[sub] > map[sub]) 
                {
                    string first = s.substr(i - (size_ - 1) * length, length);
                    seen[first]--;
                    size_--;
                }
                if (size_ == words.size())
                    result.push_back(i - (size_ - 1) * length);
            }
        }
        return result;
    }
};

int main()
{
    
}