0

**This is a translation, very hastily written. If you need any clarification, just comment.

The question given is: We live in a world with too much garbage. We have found a way to compress the garbage, but it can only be done in a specific way, or else the garbage will explode. The garbage has to be laid out in a line, and it can only be compressed with its neighbor, and only if its neighbor has the same value as it.

The first input is int N, and it represents the amount of garbage in the row. The second input is t, and it must have an input of as many characters as the value in N. If the whole thing is able to be compressed until there's only 1 garbage (t) left, then the output will be "YES".

We've figured out that as long as either N == 1, or all inputs in t (all the characters) are the same, the output will be YES.

Example inputs/outputs:

Input:
2
1 1

Output:
YES

Or

Input:
3
1 2 1

Output: 
NO

Or

Input:
1
5

Output:
YES

Here's what we've got so far:

#include <iostream>
#include <string>
using namespace std;
int N;
string t;
bool allCharactersSame(string s) 
{ 
    int n = s.length(); 
    for (int i = 1; i < n; i++) {

        if (s[i] != s[0]) 
            return false; 
    }
    return true; 
} 
int main()
{
  cin>>N;
  cin >> t;
  if (N == 1)
  {
    cout << "YES";
  }
  else if (allCharactersSame(t))
  {
    cout <<"YES";
  }
  else
  {
    cout<<"NO";
  }
}

The problem with this is that it outputs YES no matter what, and we think it's because it takes the whitespace of the input into consideration. If we don't include spaces, it works fine. BUT the question dictates that we Have To have spaces separating our inputs. So, we're stumped. Any suggestions?

  • if (s[i] != s[0]) this will always compare to the 1st character in the string, not the character next to it. To not just do the work, I'll leave it as an exercise of how you should go through and compare all characters. (you will need 2 loops) – Kai Nov 16 '19 at 03:09
  • @Kai Dude... Man, that's harsh. But fair. So it should be somewhere along the lines of 'if (s[i] != s[i+1])'? 'if (s[i] != s[j])'? Are those even valid? ... And we're lost with the rest. –  Nov 16 '19 at 03:25
  • I'll toss a nice hint - https://stackoverflow.com/questions/4003584/more-elegant-way-to-check-for-duplicates-in-c-array – Kai Nov 16 '19 at 03:43

1 Answers1

1

(I can't comment, therefore I write this as an answer.)

There is some other problem than you think, because the code in the question works as it should. When I gave it input "5 11111" it said "YES" when I gave it "5 12345" it said "NO".

Kai's first comment is slightly weird, when determining whether all characters in a string are the same it is sufficient to compare each of them to the first one, just as you do it in your allCharactersSame() method.

I'd suggest you add some checks on the provided input; the program should probably notice if given N doesn't match given strings' length and it should probably notice when the given string doesn't consist of numbers. As it is now, e.g. input "3 a" says "YES".

AshleyWilkes
  • 741
  • 5
  • 13
  • It does work that way, but the thing is we need to include spaces between our input values. Therefore, '12345' might be NO, but when we put in our required input '1 2 3 4 5', it says YES, because it stops interpreting at '1', taking the space as a stop. :( –  Nov 16 '19 at 03:51
  • if not, you can use ' std::getline(std::cin, t); ' to read the second input. https://en.cppreference.com/w/cpp/string/basic_string/getline And to remove spaces from the string read, see https://stackoverflow.com/questions/83439/remove-spaces-from-stdstring-in-c – AshleyWilkes Nov 16 '19 at 03:59