1584 - Circular Sequence

All about problems in Volume 15. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
Ovro
New poster
Posts: 12
Joined: Mon Nov 11, 2013 8:30 am

Re: 1584 - Circular Sequence

Post by Ovro » Wed Jan 28, 2015 5:07 pm

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;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 1584 - Circular Sequence

Post by brianfry713 » Thu Jan 29, 2015 2:02 am

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
Check input and AC output for thousands of problems on uDebug!

Ovro
New poster
Posts: 12
Joined: Mon Nov 11, 2013 8:30 am

Re: 1584 - Circular Sequence

Post by Ovro » Sat Jan 31, 2015 5:54 pm

Thanks. Got AC. Only used Global variables instead of those temp variables. :)

robin_0
New poster
Posts: 7
Joined: Tue Apr 28, 2015 9:47 am

1584 - Circular Sequence

Post by robin_0 » Tue Apr 28, 2015 9:51 am

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;
}

Post Reply

Return to “Volume 15 (1500-1599)”