ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Обсуждение задачи 1723. Книга Сандро

c++ AC with brute force
Послано amirshir 11 янв 2015 21:36
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector <pair<string,int> > ji;
string ghi ;
void j(unsigned short k)
{    string h="" ;
     for(unsigned short i=k;i<ghi.size();i++)
     {


          h+=ghi[i] ;
          ji.push_back(make_pair(h,1)) ;
     }
}
bool cmo(const pair<string,int> &x,const pair<string,int> &y)
{
     return x.first<y.first ;
}
bool cmod(const pair<string,int> &x,const pair<string,int> &y)
{
     if(x.second!=y.second)
          return x.second>y.second ;
     return x.first>y.first ;
}
int main()
{
     cin>>ghi ;
     for(unsigned short i=0;i<ghi.size();i++)
          j(i) ;

     sort(ji.begin(),ji.end(),cmo) ;

     short h=ji.size()-1 ;
     while(h)
     {
          if(ji.at(h).first==ji.at(h-1).first)
               {
                        ji.at(h-1).second+=ji.at(h).second ;
                        ji.erase(ji.begin()+h) ;
               }
          h--;
     }
     sort(ji.begin(),ji.end(),cmod) ;
     cout<<ji.at(0).first ;
}
Re: c++ AC with brute force
Послано Vadimzzz 15 дек 2016 21:00
do u really think its brute?

that is pure brute force:

#include <iostream>
#include <string>

using namespace std;


int main()
{
    string str;
    cin >> str;


    int max = 0;
    int step = 0;
    char posMax{};

    int len = str.length();
    int curmax = 0;

    ++step;
    curmax = 0;
    for (size_t i = 0; i < len; ++i)
        if (str[i] == 'a')
            ++curmax;
    if (max < curmax)
    {
        max = curmax;
        posMax = 'a';
    }
    curmax = 0;

    ++step;
    curmax = 0;
    for (size_t i = 0; i < len; ++i)
        if (str[i] == 'b')
            ++curmax;
    if (max < curmax)
    {
        max = curmax;
        posMax = 'b';
    }
    curmax = 0;

    ++step;
    curmax = 0;
    for (size_t i = 0; i < len; ++i)
        if (str[i] == 'c')
            ++curmax;
    if (max < curmax)
    {
        max = curmax;
        posMax = 'c';
    }
    curmax = 0;

    ++step;
    curmax = 0;
    for (size_t i = 0; i < len; ++i)
        if (str[i] == 'd')
            ++curmax;
    if (max < curmax)
    {
        max = curmax;
        posMax = 'd';
    }
    curmax = 0;

    ++step;
    curmax = 0;
    for (size_t i = 0; i < len; ++i)
        if (str[i] == 'e')
            ++curmax;
    if (max < curmax)
    {
        max = curmax;
        posMax = 'e';
    }
    curmax = 0;

    ++step;
    curmax = 0;
    for (size_t i = 0; i < len; ++i)
        if (str[i] == 'f')
            ++curmax;
    if (max < curmax)
    {
        max = curmax;
        posMax = 'f';
    }
    curmax = 0;

    ++step;
    curmax = 0;
    for (size_t i = 0; i < len; ++i)
        if (str[i] == 'g')
            ++curmax;
    if (max < curmax)
    {
        max = curmax;
        posMax = 'g';
    }
    curmax = 0;

    ++step;
    curmax = 0;
    for (size_t i = 0; i < len; ++i)
        if (str[i] == 'h')
            ++curmax;
    if (max < curmax)
    {
        max = curmax;
        posMax = 'h';
    }
    curmax = 0;

    ++step;
    curmax = 0;
    for (size_t i = 0; i < len; ++i)
        if (str[i] == 'i')
            ++curmax;
    if (max < curmax)
    {
        max = curmax;
        posMax = 'i';
    }
    curmax = 0;

    ++step;
    curmax = 0;
    for (size_t i = 0; i < len; ++i)
        if (str[i] == 'j')
            ++curmax;
    if (max < curmax)
    {
        max = curmax;
        posMax = 'j';
    }
    curmax = 0;

    ++step;
    curmax = 0;
    for (size_t i = 0; i < len; ++i)
        if (str[i] == 'k')
            ++curmax;
    if (max < curmax)
    {
        max = curmax;
        posMax = 'k';
    }
    curmax = 0;

    ++step;
    curmax = 0;
    for (size_t i = 0; i < len; ++i)
        if (str[i] == 'l')
            ++curmax;
    if (max < curmax)
    {
        max = curmax;
        posMax = 'l';
    }
    curmax = 0;

    ++step;
    curmax = 0;
    for (size_t i = 0; i < len; ++i)
        if (str[i] == 'm')
            ++curmax;
    if (max < curmax)
    {
        max = curmax;
        posMax = 'm';
    }
    curmax = 0;

    ++step;
    curmax = 0;
    for (size_t i = 0; i < len; ++i)
        if (str[i] == 'n')
            ++curmax;
    if (max < curmax)
    {
        max = curmax;
        posMax = 'n';
    }
    curmax = 0;

    ++step;
    curmax = 0;
    for (size_t i = 0; i < len; ++i)
        if (str[i] == 'o')
            ++curmax;
    if (max < curmax)
    {
        max = curmax;
        posMax = 'o';
    }
    curmax = 0;

    ++step;
    curmax = 0;
    for (size_t i = 0; i < len; ++i)
        if (str[i] == 'p')
            ++curmax;
    if (max < curmax)
    {
        max = curmax;
        posMax = 'p';
    }
    curmax = 0;

    ++step;
    curmax = 0;
    for (size_t i = 0; i < len; ++i)
        if (str[i] == 'q')
            ++curmax;
    if (max < curmax)
    {
        max = curmax;
        posMax = 'q';
    }
    curmax = 0;

    ++step;
    curmax = 0;
    for (size_t i = 0; i < len; ++i)
        if (str[i] == 'r')
            ++curmax;
    if (max < curmax)
    {
        max = curmax;
        posMax = 'r';
    }
    curmax = 0;

    ++step;
    curmax = 0;
    for (size_t i = 0; i < len; ++i)
        if (str[i] == 's')
            ++curmax;
    if (max < curmax)
    {
        max = curmax;
        posMax = 's';
    }
    curmax = 0;

    ++step;
    curmax = 0;
    for (size_t i = 0; i < len; ++i)
        if (str[i] == 't')
            ++curmax;
    if (max < curmax)
    {
        max = curmax;
        posMax = 't';
    }
    curmax = 0;

    ++step;
    curmax = 0;
    for (size_t i = 0; i < len; ++i)
        if (str[i] == 'u')
            ++curmax;
    if (max < curmax)
    {
        max = curmax;
        posMax = 'u';
    }
    curmax = 0;

    ++step;
    curmax = 0;
    for (size_t i = 0; i < len; ++i)
        if (str[i] == 'v')
            ++curmax;
    if (max < curmax)
    {
        max = curmax;
        posMax = 'v';
    }
    curmax = 0;

    ++step;
    curmax = 0;
    for (size_t i = 0; i < len; ++i)
        if (str[i] == 'w')
            ++curmax;
    if (max < curmax)
    {
        max = curmax;
        posMax = 'w';
    }
    curmax = 0;

    ++step;
    curmax = 0;
    for (size_t i = 0; i < len; ++i)
        if (str[i] == 'x')
            ++curmax;
    if (max < curmax)
    {
        max = curmax;
        posMax = 'x';
    }
    curmax = 0;

    ++step;
    curmax = 0;
    for (size_t i = 0; i < len; ++i)
        if (str[i] == 'y')
            ++curmax;
    if (max < curmax)
    {
        max = curmax;
        posMax = 'y';
    }
    curmax = 0;

    ++step;
    curmax = 0;
    for (size_t i = 0; i < len; ++i)
        if (str[i] == 'z')
            ++curmax;
    if (max < curmax)
    {
        max = curmax;
        posMax = 'z';
    }
    cout << posMax;




    system("pause");
    return 0;
}
Re: c++ AC with brute force
Послано Bakhodir Boydedaev 3 сен 2017 15:17
It is alternative way to solve the problem. You don't have to write all the symbol to check, you can use ASCII code

public static char solve(String s){
        int a[] = new int[200];
        for (int i = 0; i < s.length(); i++) {
            a[s.charAt(i)] ++;
        }

        int index = -1;
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < a.length; i++) {
            if (a[i] > max) {
                index = i;
                max = a[i];
            }
        }
        return (char)(index);
    }