SPOJ Alfabet Morse'a

0

Witam,

Rozwiązuję zadanie ze spoja o alfabecie Morse'a. Wszystko w konsoli działa, podane przypadki też, a jednak spoj nie przyjmuje. Gdzie może tkwić błąd?

#include <iostream>
#include <map>
#include <string>

using namespace std;


int main() {
    string napisDoZaszyfrowania;

      int liczbaTestow = 0;
      //int maxDlugoscCiagu = 1000;
     int dlugoscNapisu = napisDoZaszyfrowania.length();
     int maxDlugoscCiagu = 1000;


    map <char, string> alfabetMorsa;

    alfabetMorsa['A']=".-/";
    alfabetMorsa['a']=".-/";
    alfabetMorsa['B']="-.../";
    alfabetMorsa['b']="-.../";
    alfabetMorsa['C']="-.-./";
    alfabetMorsa['c']="-.-/";
    alfabetMorsa['D']="-../";
    alfabetMorsa['d']="-../";
    alfabetMorsa['E']="./";
    alfabetMorsa['e']="./";
    alfabetMorsa['F']=".-../";
    alfabetMorsa['f']=".-../";
    alfabetMorsa['G']="--./";
    alfabetMorsa['g']="--./";
    alfabetMorsa['H']="..../";
    alfabetMorsa['h']="..../";
    alfabetMorsa['I']="../";
    alfabetMorsa['i']="../";
    alfabetMorsa['J']=".---/";
    alfabetMorsa['j']=".---/";
    alfabetMorsa['K']="-.-/";
    alfabetMorsa['k']="-.-/";
    alfabetMorsa['L']="..-./";
    alfabetMorsa['l']="..-./";
    alfabetMorsa['M']="--/";
    alfabetMorsa['m']="--/";
    alfabetMorsa['N']="-./";
    alfabetMorsa['n']="-./";
    alfabetMorsa['O']="---/";
    alfabetMorsa['o']="---/";
    alfabetMorsa['P']=".--./";
    alfabetMorsa['p']=".--./";
    alfabetMorsa['Q']="--.-/";
    alfabetMorsa['q']="--.-/";
    alfabetMorsa['R']=".-./";
    alfabetMorsa['r']=".-./";
    alfabetMorsa['S']=".../";
    alfabetMorsa['s']=".../";
    alfabetMorsa['T']="-/";
    alfabetMorsa['t']="-/";
    alfabetMorsa['U']="..-/";
    alfabetMorsa['u']="..-/";
    alfabetMorsa['V']="...-/";
    alfabetMorsa['v']="...-/";
    alfabetMorsa['W']=".--/";
    alfabetMorsa['w']=".--/";
    alfabetMorsa['X']="-..-/";
    alfabetMorsa['x']="-..-/";
    alfabetMorsa['Y']="-.--/";
    alfabetMorsa['y']="-.--/";
    alfabetMorsa['Z']="--../";
    alfabetMorsa['z']="--../";
    alfabetMorsa[' ']="/";

    while (getline(cin, napisDoZaszyfrowania) && (liczbaTestow < 100)) {
        for (int i = 0; (i < napisDoZaszyfrowania.length()  && (napisDoZaszyfrowania.length() < maxDlugoscCiagu)); i++)  {
            map <char, string> :: iterator itr = alfabetMorsa.find(napisDoZaszyfrowania[i]);

            if (itr!= alfabetMorsa.end()) {
                cout << itr->second;

            }
            liczbaTestow++;

        }
        cout << "\n";



    }

    return 0;

}


0

Obstawiam problemy z formatem wyjscia.

  1. && (liczbaTestow < 100) to mi się nie podoba, nie rozumiem po co to w ogóle tu jest. Nie musisz sprawdzać poprawnosci danych na spoju
  2. Jesteś pewna ze nie wczyta ci np. na koniec pustej linii i ty potem wypiszesz też pustą linię i na wyjściu będzie o 1 newline za dużo?
  3. Ba, nawet i bez takich kombinacji masz o jeden newline za dużo, bo po ostatnim wyniku nie powinno go być.
0
Shalom napisał(a):

Obstawiam problemy z formatem wyjscia.

  1. && (liczbaTestow < 100) to mi się nie podoba, nie rozumiem po co to w ogóle tu jest. Nie musisz sprawdzać poprawnosci danych na spoju
  2. Jesteś pewna ze nie wczyta ci np. na koniec pustej linii i ty potem wypiszesz też pustą linię i na wyjściu będzie o 1 newline za dużo?
  1. W zadaniu jest napisane " Wejście składa się z nieznanej liczby wierszy (nie więcej niż 100) ", dlatego zrobiłam ten waruenk
  2. Być może, o tym nie pomyślałam
0
Shalom napisał(a):

Obstawiam problemy z formatem wyjscia.

  1. && (liczbaTestow < 100) to mi się nie podoba, nie rozumiem po co to w ogóle tu jest. Nie musisz sprawdzać poprawnosci danych na spoju
  2. Jesteś pewna ze nie wczyta ci np. na koniec pustej linii i ty potem wypiszesz też pustą linię i na wyjściu będzie o 1 newline za dużo?
  3. Ba, nawet i bez takich kombinacji masz o jeden newline za dużo, bo po ostatnim wyniku nie powinno go być.
  1. Jak mogę się pozbyć tej zbędnej linii?

Trochę zmieniłam ten mój kod:

#include <iostream>
#include <map>
#include <string>

using namespace std;


int main() {
    string napisDoZaszyfrowania;

      int liczbaTestow = 0;
      //int maxDlugoscCiagu = 1000;
     int dlugoscNapisu = napisDoZaszyfrowania.length();
     int maxDlugoscCiagu = 1000;


    map <char, string> alfabetMorsa;

    alfabetMorsa['A']=".-/";
    alfabetMorsa['a']=".-/";
    alfabetMorsa['B']="-.../";
    alfabetMorsa['b']="-.../";
    alfabetMorsa['C']="-.-./";
    alfabetMorsa['c']="-.-/";
    alfabetMorsa['D']="-../";
    alfabetMorsa['d']="-../";
    alfabetMorsa['E']="./";
    alfabetMorsa['e']="./";
    alfabetMorsa['F']=".-../";
    alfabetMorsa['f']=".-../";
    alfabetMorsa['G']="--./";
    alfabetMorsa['g']="--./";
    alfabetMorsa['H']="..../";
    alfabetMorsa['h']="..../";
    alfabetMorsa['I']="../";
    alfabetMorsa['i']="../";
    alfabetMorsa['J']=".---/";
    alfabetMorsa['j']=".---/";
    alfabetMorsa['K']="-.-/";
    alfabetMorsa['k']="-.-/";
    alfabetMorsa['L']="..-./";
    alfabetMorsa['l']="..-./";
    alfabetMorsa['M']="--/";
    alfabetMorsa['m']="--/";
    alfabetMorsa['N']="-./";
    alfabetMorsa['n']="-./";
    alfabetMorsa['O']="---/";
    alfabetMorsa['o']="---/";
    alfabetMorsa['P']=".--./";
    alfabetMorsa['p']=".--./";
    alfabetMorsa['Q']="--.-/";
    alfabetMorsa['q']="--.-/";
    alfabetMorsa['R']=".-./";
    alfabetMorsa['r']=".-./";
    alfabetMorsa['S']=".../";
    alfabetMorsa['s']=".../";
    alfabetMorsa['T']="-/";
    alfabetMorsa['t']="-/";
    alfabetMorsa['U']="..-/";
    alfabetMorsa['u']="..-/";
    alfabetMorsa['V']="...-/";
    alfabetMorsa['v']="...-/";
    alfabetMorsa['W']=".--/";
    alfabetMorsa['w']=".--/";
    alfabetMorsa['X']="-..-/";
    alfabetMorsa['x']="-..-/";
    alfabetMorsa['Y']="-.--/";
    alfabetMorsa['y']="-.--/";
    alfabetMorsa['Z']="--../";
    alfabetMorsa['z']="--../";
    alfabetMorsa[' ']="/";

    while (getline(cin, napisDoZaszyfrowania)) {
        for (int i = 0; (i < napisDoZaszyfrowania.length(); i++)  {
            map <char, string> :: iterator itr = alfabetMorsa.find(napisDoZaszyfrowania[i]);

            if (itr!= alfabetMorsa.end()) {
                cout << itr->second;

            }
            liczbaTestow++;

        }
        cout << "\n";



    }

    return 0;

}


0
  1. Dla takiego zadania małe i wielkie litery to duplikacja danych. Wystarczy znormalizować wejście do małych liter.
  2. c++ od wersji 11 ma parę fajnych bajerów, które upraszczają kod, jak słowo auto czy pętlę foreach.
  3. liczbaTestow nie jest używana.
  4. Zamiast map można użyć unordered_map.
  5. Zamiast unordered_map i map można użyć płaski vector ponieważ rozmiar alfabetu morsa jest niezmienny. (Choć przyznam, że problem jest na tyle trywialny, że nie warto kombinować).
for (auto x : napisDoZaszyfrowania)  {
    auto xm = alfabetMorsa.find(char(tolower(x)));
    if (xm != alfabetMorsa.end()) {
        cout << xm->second;
    }
}
0

Kod można trochę skrócić, używając przy okazji unordered_map, która zapewnia szybsze wyszukiwanie elementów.

#include <iostream>
#include <unordered_map>

using namespace std;

const unordered_map<char,string> morse_code =
{
 {'A',".-"},{'B',"-..."},{'C',"-.-."},{'D',"-.."},{'E',"."},
 {'F',"..-."},{'G',"--."},{'H',"...."},{'I',".."},{'J',".---"},
 {'K',"-.-"},{'L',".-.."},{'M',"--"},{'N',"-."},{'O',"---"},
 {'P',".--."},{'Q',"--.-"},{'R',".-."},{'S',"..."},{'T',"-"},
 {'U',"..-"},{'V',"...-"},{'W',".--"},{'X',"-..-"},{'Y',"-.--"},
 {'Z',"--.."},{'0',"-----"},{'1',".----"},{'2',"..---"},{'3',"...--"},
 {'4',"....-"},{'5',"....."},{'6',"-...."},{'7',"--..."},{'8',"---.."},
 {'9',"----."},{'.',".-.-.-"},{',',"--..--"},{'?',"..--.."},{'=',"-...-"},
};

int main()
{
    string text;
    while( getline(cin,text) )
    {
        if( text == "" ) break;
        for( const auto& letter : text )
        {
            auto iter { morse_code.find( isalpha(letter)?toupper(letter):letter ) };
            if( iter != morse_code.end() ) cout << iter->second << " ";
        }
        cout << endl;
    }

    return 0;
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1