Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 39796

WA in BUY1GET1

$
0
0

I am getting wrong Answer in the problem BUY1GET1 . This is my code

        #include<iostream>
        #include<vector>
        #include<string.h>
        #include<algorithm>

        using namespace std;

        struct S{
         char st[200];
        };

        int main(){
            std::ios_base::sync_with_stdio(false);
            long int num,k,t;
            vector <S> s;
            S str;
            int t1;
            cin>>t1;
            for(int i=0;i<t1;i++)
            {
                cin>>str.st;
                s.push_back(str);
            }

              for(int i=0;i<t1;i++)
            {

                if(strlen(s.at(i).st)==1)
                {
                    cout<<"1"<<endl;
                    continue;
                }

                    num=0;
                    k=1;
                    t=1;

               sort( s.at(i).st , s.at(i).st+strlen(s.at(i).st) );
                while( k<strlen(s.at(i).st) )
                {
                    t=1;


                while( (s.at(i).st[k-1]==s.at(i).st[k])  && ( k<strlen(s.at(i).st) ) )
                {
                    t++;
                    k++;
                }


                    if(t%2==0)
                        num+=(t/2);
                    else
                        num+=( (t/2)+1 );

                    k++;

                    if(k==strlen(s.at(i).st))
                        num++;

                }

                cout<<num<<endl;

            }

        return 0;
        }

My program gives the following output for the input string as

INPUT

1

ssassa

OUTPUT

3

I assume, this is as expected. Would like to know where it went wrong.. And my apologies for complicating the code by using both STL containers and C-Style arrays..

Thanks.


Viewing all articles
Browse latest Browse all 39796

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>