Monday, October 16, 2017

UVa problem solution 489 - Hangman Judge

problem link:

Discuss: in this problem as you can count stroke only unique wrong guess you have to erase all duplicate element from guess string.you may can erase all duplicate element from string also as one correct guess for all same character.and this tricky part  that you can't short guess string using sorting method.and compare and follow the instruction of problem statement.


try yourself before see the code



#include<bits/stdc++.h>
using namespace std;
int main()
{

    int n;
    while(cin >>n&&n!=-1)
    {
        int i,j,cnt=0,flag=0,l,m,ok=0,similar=0;
            string str,check;
            cin >>str;
            cin >>check;

            m=str.length();
            //std::sort(check.begin(), check.end());
       //check.erase(std::unique(check.begin(), check.end()), check.end());
       //cout <<check<<endl;
        l=check.length();
          for(i=0;i<l;i++)
            for(j=i+1;j<l;j++)
          {
              if(check[i]==check[j])
                check[j]='#';
          }
            for(i=0;i<l;i++)
            {
                flag=0;
                for(j=0;j<m;j++)
                {
                    if(str[j]==check[i])
                    {
                          str[j]='*';
                          flag=1;
                          similar++;
                    }
                }
                if(!flag&&check[i]!='#')
                    cnt++;
                    if(similar>=m)
                        break;
            }
            for(i=0;i<m;i++)
            {
                if(str[i]>='a'&&str[i]<='z')
                {
                    ok=1;
                    break;
                }
            }
            cout <<"Round "<<n<<endl;
              if(ok&&cnt<7)
                cout  <<"You chickened out."<<endl;
         else if(cnt>=7)
                cout <<"You lose."<<endl;
           else if(!ok)
                cout <<"You win."<<endl;

    }
    return 0;
}

No comments:

Post a Comment