Page 1 of 1

Re: 1584 - Circular Sequence

Posted: Wed Jan 28, 2015 5:07 pm
by Ovro
Getting WA. Can someone tell what's the problem?? Thanks in Advance.

Code: Select all

#include<iostream>
#include<cstring>
using namespace std;

char *createStr(char str[], int pos)
{
	char temp[120];
	int len = strlen(str);

	for(int i=0;i<len;i++)
	{
		temp[i] = str[(i+pos)%len];
	}
	temp[len] = NULL;
	return temp;
}

char *detectOutput(char str[])
{
	int len = strlen(str);
	char temp[120], tempF[120];

	strcpy(tempF, str);
	for(int i=0;i<len;i++)
	{
		strcpy(temp, createStr(str, i));
		if(strcmp(temp, tempF) < 0)
			strcpy(tempF, temp);
	}
	return tempF;
}


int main()
{
	int test;
	char str[120];
	char temp[120];
	cin >> test;

	while(test--)
	{
		cin >> str;
		strcpy(temp, detectOutput(str));
		cout << temp << endl;
	}
	return 0;
}

Re: 1584 - Circular Sequence

Posted: Thu Jan 29, 2015 2:02 am
by brianfry713
When I compile your code using g++ I get these:
7: warning: address of local variable ‘temp’ returned
21: warning: address of local variable ‘tempF’ returned

Re: 1584 - Circular Sequence

Posted: Sat Jan 31, 2015 5:54 pm
by Ovro
Thanks. Got AC. Only used Global variables instead of those temp variables. :)

1584 - Circular Sequence

Posted: Tue Apr 28, 2015 9:51 am
by robin_0
I am getting WA . :(
but why :(
my inputs work fine :(

Code: Select all

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>

char temp[1005],min[1005];

int main()
{
    int t,i,j,n;
    char inp[1005];

    scanf("%d",&t);

    while(t--)
    {
        scanf("%s",inp);

        n=strlen(inp);

        strcpy(min,inp);


        for(j=1;j<n;j++)
        {
            strcpy(temp,"");

            for(i=0;i<n;i++)
            {
                if((i+j)<n)
                {
                    temp[i]=inp[i+j];
                }
                else
                {
                    temp[i]=inp[i+j-n];
                }
            }
            if(strcmp(temp,min)<0) strcpy(min,temp);
         }

        printf("%s\n",min);


    }

    return 0;
}