Cod sursa(job #157618)

Utilizator Andreid91Ciocan Andrei Andreid91 Data 13 martie 2008 10:07:45
Problema Lampa Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.75 kb
#include<fstream.h>
#include<string.h>

void intorc (char s[60000])
	{
	long i;
	char ch;
	for (i=0;i<strlen(s)/2;i++)
		{
		ch=s[i];
		s[i]=s[strlen(s)-i-1];
		s[strlen(s)-i-1]=ch;
		}
	}
int main()
{
long j,k; int n;
long m,i;
char s[600000];
ifstream f("lampa.in");
f>>n>>m;
f>>s;
f.close();
char s1[52000],s2[52000];
s1[0]='1';s2[0]='2';s1[1]=s2[1]=0;
for (j=3;j<=n;j++)
	{
	k=strlen(s1);
	strcat (s1,s2);
	strcpy(s2,s1);
	for (i=k;i<strlen(s2);i++)
	s1[i-k]=s2[i];
	s1[strlen(s2)-k]=0;
	}
long nr1=0,nr2=0;
for (i=0;i<strlen(s2);i++)
	if (s2[i]=='1') nr1++;
		  else nr2++;
long p;char c1[600000],c2[600000],max1[600000],max2[600000];max1[0]='z';max1[1]=0;max2[0]=0;c1[0]=0;c2[0]=0;
long h;int sw=0;
int sw1=0; m=strlen(s);
for (i=1;i<m/nr1;i++)
	if ((m-nr1*i)%nr2==0) {
			      p=(m-nr1*i)/nr2;
			      if (s2[0]=='1') {
					    strncpy(c1,s,i);
					    strncpy(c2,s,i+p);
					    c1[i]=0;c2[i+p]=0;
					    intorc(c2);
					    c2[p]=0;
					    intorc(c2);
					    }
				       else {
					    strncpy(c2,s,p);
					    strncpy(c1,s,i+p);
					    c1[i+p]=0;c2[p]=0;
					    intorc(c1);
					    c1[i]=0;
					    intorc(c1);
					    }
			      k=i+p;
			      sw=0;
			      for (j=2;j<strlen(s2) && !sw;j++)
				    {
				    if (s2[j]=='1') for (h=0;h<strlen(c1)&&!sw;h++)
							if (c1[h]!=s[k++]) sw=1;
				    if (s2[j]=='2') for (h=0;h<strlen(c2)&&!sw;h++)
							if (c2[h]!=s[k++]) sw=1;
				    }
			      if (sw==0) {
					 sw1=1;
					 if (strcmp(max1,c1)>0) {
								strcpy(max1,c1);max1[strlen(c1)]=0;
								strcpy(max2,c2);max2[strlen(c2)]=0;
								}
			      }          }
ofstream g("lampa.out");
if (sw1==0) g<<-1;
      else g<<max1<<"\n"<<max2;
g.close();
return 0;
}