489 – Hangman Judge UVA Solution

This is an easy problem from uva judge http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=430

Adhoc.

How I did it

  • Read the round number.
  • Read answer and the guess string.
  • Traverse the guess string and search each character in the answer string. If more than 6 characters are not found in the answer array, the player lose(break the loop). If all the answer characters were founded, the player wins(break the loop).
  • Print the result.

Hints

  • check 2 conditions:     a) Once that the wrong guesses is equal to 7 break the loops.  b) Once that all the characters were found, break the loops.
  • If one condition is full filled, you need to break the loops and tests(win or loose). Otherwise end the loops(chickened out).

Code

#include <iostream>
#include <cstdio>
#include <set>
#include <vector>
using namespace std;

bool checkStatus(vector<int>estados)
{
    for(int v=0; v<estados.size(); v++)
        if(estados[v] == 0)
           return false;
return true;
}

int main()
{
vector<int> estados;
string answer, guess;
int round;

while(scanf("%d", &round))
{
    if(round == -1)
       break;

    cin>>answer;
    cin>>guess;

    bool flagProb = true, flag;
    estados.assign(answer.size(), 0);
    set<char> wrong;

    for(int x=0; x<guess.size(); x++)
    {
        flag = false;
        for(int y=0; y<answer.size(); y++)
            if(guess[x] == answer[y])
            {
                flag = true;
                estados[y] = 1;
            }
        if(!flag)
        {
            wrong.insert(guess[x]);
            if(wrong.size() == 7)
            {
                flagProb = false;
                break;
            }
        }
        if(checkStatus(estados))
        {
            flagProb =  true;
            break;
        }
    }
    printf("Round %d\n", round);
    if(!flagProb)
       printf("You lose.\n");
    else if(checkStatus(estados))
            printf("You win.\n");
    else
        printf("You chickened out.\n");
}
return 0;
}

Leave a comment