Pagini recente » Cod sursa (job #1997109) | Cod sursa (job #466041) | Cod sursa (job #143833) | Cod sursa (job #2911236) | Cod sursa (job #271046)
Cod sursa(job #271046)
#include<fstream>
#include<iomanip>
#include<string.h>
#define Nmax 10002
using namespace std;
char s[Nmax],sconst[Nmax];
ifstream fin("perle.in");
ofstream fout("perle.out");
inline void copy_a(char c[],const char wcopy[],int i) //l-am facut cred :-S
{char aux[Nmax],aux2[Nmax];
memset(aux,0,sizeof(aux)); memset(aux2,0,sizeof(aux2));
strncpy(aux,c,i);
strcpy(aux2,c+i+1);
memset(c,0,sizeof(c));
strcpy(c,aux); strcat(c,wcopy); strcat(c,aux2);
}
void determen(char sconst[],char s[])
{int i;
for(i=1;i<strlen(sconst);++i)
{switch(sconst[i])
{
case 'A': sconst[i]=s[i];break;
case 'B': {if(s[i]=='2') copy_a(sconst,"2B",i);
else if(s[i]=='1')copy_a(sconst,"1A3AC",i);}break;
case 'C': {if(s[i]=='2') sconst[i]='2';
else if(s[i]=='3') copy_a(sconst,"3BC",i);
else if(s[i]=='1') copy_a(sconst,"12A",i);}break;
}
}
}
int perle_valid(char s[],int lg)
{
if(lg==1) return 0;
if(lg==2||lg==4) return -1;
if(lg==3)
if(s[0]=='1'&&s[1]=='2') return 0;
else return -1;
if(lg==5)
if(s[0]=='1'&&s[2]=='3'&&s[4]=='2') return 0;
else return -1;
if(lg==6)
if(s[0]=='2'&&s[1]=='1'&&s[3]=='3'&&s[5]=='2') return 0;
else return -1;
if(s[0]=='1') strcpy(sconst,"1A3AC");
else if(s[0]=='2') strcpy(sconst,"2B");
else strcpy(sconst,"3BC");
determen(sconst,s);
if(memcmp(s,sconst,sizeof(sconst))) return -1;
return 0;
}
int main()
{int n,i,j,lg;
fin>>n;
for(i=1;i<=n;++i)
{fin>>lg;
for(j=0;j<lg;++j) fin>>setiosflags(ios::skipws)>>s[j];
fout<<(perle_valid(s,lg)+1)<<"\n"; memset(s,0,sizeof(s)); memset(sconst,0,sizeof(sconst));
}
return 0;
}