CSES - Datatähti 2024 alku - Results
Submission details
Task:Monistus
Sender:Ojar23
Submission time:2023-11-09 19:38:05 +0200
Language:C++ (C++11)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'std::string replaceFirst(std::string)':
input/code.cpp:12:28: error: 'pow' was not declared in this scope
   12 |     int firstPlace = (5 * (pow(10, 5))) + 1;
      |                            ^~~
input/code.cpp:15:43: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   15 |         if ((word.find(std::to_string(i)) < firstPlace) && (word.find(std::to_string(i)) != std::string::npos) && std::isalpha(word.substr(word.find(std::to_string(i)), 1)[0]) == 0)
      |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~

Code

#include <iostream>
#include <algorithm>
#include <string>
#include <cctype>
#include <cstring>
bool has_any_digits(const std::string& s)
{
    return std::any_of(s.begin(), s.end(), ::isdigit);
}
std::string replaceFirst(std::string word)
{
    int firstPlace = (5 * (pow(10, 5))) + 1;
    for (int i = 1; i < 10; i++)
    {
        if ((word.find(std::to_string(i)) < firstPlace) && (word.find(std::to_string(i)) != std::string::npos) && std::isalpha(word.substr(word.find(std::to_string(i)), 1)[0]) == 0)
        {
            firstPlace = int(word.find(std::to_string(i)));
        }
    }
    word = word.replace(firstPlace, 1, word.substr(firstPlace + 1, stoi(word.substr(firstPlace, 1))));
    return word;
}
//98abcdefghijk54lmnopqrstuvw65xyz987ABCDE43FGHI987JKLMN654OPQRSTU765VWXYZ+-
int main()
{
    std::string word = "";
    std::cin >> word;
    while (has_any_digits(word))
    {
        word = replaceFirst(word);
    }
    std::cout << word;
}