CAKEDOOM: Getting wa

#include

using namespace std;

#include<string.h>
int main()
{
int T;
cin>>T;

for(int i=0; i<T; i++)
{
        int K;
        cin>>K;
        char S[100];
        cin>>S;
        int flag=1;
        if(strlen(S)>1)
        {
                       for(int j=0; j<strlen(S) && flag; j++)
                       {
                               if(S[j]!='?')
                               {
					if(j==strlen(S)-1)
					{
						if(S[j]==S[0] || S[j]==S[j-1])
						{
							cout<<"NO";
							flag=0;
						}
					}
					else if(j==0)
					{
						if(S[0]==S[1] || S[0]==S[strlen(S)-1])
						{
							cout<<"NO";
							flag=0;
						}
					}

                                            else if(S[j]==S[j+1] || S[j]==S[j-1])
                                            {
                                                            cout<<"NO";
                                                            flag=0;
                                            }
                               }
                       }
        }
        else
        {
            if(S[0]!='?')
                cout<<S[0];
            else
                cout<<"0";
            flag=0;
        }

    if(K==2 && flag)
    {
	if(strlen(S)%2!=0)
		cout<<"NO";
	else
	{
		int first=0,second=0,s=strlen(S),c=0;
		for(int k=0;k<strlen(S);k++)
		{
			if(S[k]=='0' && k%2==0 || S[k]=='1' && k%2!=0)
				first=1;
			else if(S[k]=='0' && k%2!=0 || S[k]=='1' && k%2==0)
				second=1;
			else if(S[k]=='?')
				c++;				
		}		
		int l=s/2;
		if(c==strlen(S))
			first=1;
		if(first==1 && second==1)
		{
			cout<<"NO";
			flag=0;
		}
		if(first==1&&flag)
		{
			while(l--)
				cout<<"01";
		}
		else if(second==1&&flag)
		{
			while(l--)
				cout<<"10";
		}
	}
	flag=0;
    }
        for(int j=0; j<strlen(S) && flag; j++)
        {
                if(S[j]!='?')
                    cout<<S[j];
                else
                {
                    for(int k=0;k<K;k++)
                    {
                            if(j==strlen(S)-1)
                            {
                                              if(S[0]-'0'!=k && S[j-1]-'0'!=k)
                                              {
                                                             S[j]=(char)(k+48);
                                                             cout<<(char)(k+48);
                                                             break;
                                              }
                            }
                            else if(j==0)
                            {
                                 if(S[j+1]-'0'!=k && S[strlen(S)-1]-'0'!=k)
                                 {
                                                S[j]=(char)(k+48);                
                                                cout<<(char)(k+48);
                                                break;
                                 }
                            }
                            else
                            {
                                if(S[j+1]-'0'!=k && S[j-1]-'0'!=k)
                                {
                                                 S[j]=(char)(k+48);
                                                 cout<<(char)(k+48);
                                                 break;
                                }
                            }
                    }
                }
        }
        cout<<"\n";
                
                                
                             
}

}

Please discuss this in this thread : http://discuss.codechef.com/questions/1109/wa-for-cakedoom