Cod sursa(job #2478548)

Utilizator richard26Francu Richard richard26 Data 22 octombrie 2019 13:08:21
Problema Dusman Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <bits/stdc++.h>

using namespace std;

int main()
{   
    unordered_map <long long, int> mp;

    for (int i = 0; i <= sqrt(2 * pow(10, 9)); i++) {

        mp[pow(i, 2)] = 1;
    } 

    long long nr;
    cin >> nr;

    stringstream ss;
    ss << nr;
    string s1 = ss.str();
    int or_length = s1.length();

    bool ok = false;
    
    queue <long long > q;
    q.push(nr);
    
    while (!q.empty())
    {
        long long x = q.front();
        stringstream ss1;
        ss1 << x;
        string str1 = ss1.str();
        q.pop();
        
        if (mp[x] == 1) {
            cout << or_length - str1.length() << endl;
            return 0;
        }

        for (int i = 0; i < str1.length(); i++) {
            string aux  = str1;
            str1.erase(i, 1);
            
            if (str1[0] != '0' && !str1.empty()) {
                int y;
                stringstream ss2(str1);
                ss2 >> y;
                q.push(y);
            }

            str1 = aux;  

        }

         
    }

    cout << "-1";
    return 0;
    
}