455 - Periodic Strings

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

Moderator: Board moderators

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: Help with 455

Post by mf » Wed Jan 21, 2009 4:46 pm

Obaida wrote:I tested all the cases but still WA.
Really? I think you didn't - your program doesn't correctly read input with multiple cases (read the description of input format again, and pay attention to last sentence there), so it couldn't possible pass e.g. test cases posted above by Sedefcho.

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: Help with 455

Post by Obaida » Thu Jan 22, 2009 8:27 am

after adding this line to my code:

Code: Select all

if(n>0)getchar();
I still got wA. While i checked the test cases above & they are all right. :oops:
try_try_try_try_&&&_try@try.com
This may be the address of success.

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: Help with 455

Post by mf » Thu Jan 22, 2009 10:32 am

Your variable 'found' can be used uninitialized on line 38.

g++ can detect and warn you automatically about such errors - always compile with g++ -Wall -W.

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 455 - Periodic Strings

Post by Obaida » Thu Jan 22, 2009 11:22 am

Any way i edited my code like this but still got wA.

Code: Select all

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

int main()
{
	bool found=1;
	int i,n,root,len,j,k;
	char st[81];
	scanf("%d",&n);
	getchar();getchar();
	while(n--)
	{
		gets(st);
		len = strlen(st);
		root = len/2;
		found = 1;
		for(i=1;i<=root;i++)
		{
			if(len%i==0)
			{
				found=1;
				for(j=i;j<len;j+=i)
				{
					for(k=0;k<i;k++)
					{
						if(st[k] != st[j+k])
						{
							found = 0;
							break;
						}
					}
					if(!found)break;
				}
			}
			if(found){printf("%d\n",i);break;}
		}
		if(found==0)printf("%d\n",len);
		if(n>0)puts("");
		if(n>0)getchar();
	}
	return 0;
}
I didn't have g++. could you give me a link to download the software.
try_try_try_try_&&&_try@try.com
This may be the address of success.

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: 455 - Periodic Strings

Post by mf » Thu Jan 22, 2009 11:41 am

Now your code doesn't work for strings of length 1.
I didn't have g++. could you give me a link to download the software.
Google mingw or cygwin - these are two ports of gcc to windows.

But probably your current compiler too can warn you about uninitialized variables and other errors - look in its help. And there's also this great static code analysis tool - splint, which can detect even more errors.

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 455 - Periodic Strings

Post by Obaida » Thu Jan 22, 2009 12:06 pm

I got Acc in this problem. :) :) :)
Thank you so much.
try_try_try_try_&&&_try@try.com
This may be the address of success.

Imran Bin Azad
New poster
Posts: 3
Joined: Thu Jun 03, 2010 8:55 pm

Re: Problem 455: WA

Post by Imran Bin Azad » Thu Jun 03, 2010 9:05 pm

i'm gettin' wa in this code...
need help

Code: Select all

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

int main()
{
    int i,j,k,l,len,done,x,t;
    char s[100],c[100];
    scanf("%d",&t);
    getchar();
    
    for (x=1; x<=t; ++x)
    {
          getchar();
          gets(s);
          len=strlen(s);
          
          for (i=1; i<=len; ++i)
          {
              for (j=0; j<i; ++j)
              {
                  c[j]=s[j];
              }    
              c[j]='\0';
              
              done=1;
              for (k=0,l=0; k<len; ++k,++l)
              {
                  if (c[l]!=s[k])
                  {
                     done=0;
                     break;               
                  }
                  if (l==j-1)
                     l=-1;
              }
              if (done==1)
              {
                 printf("%d\n",j);
                 break;            
              }
          } 
          if (x!=t)
             printf("\n");
    }
            
    return 0;
}

angrad
New poster
Posts: 3
Joined: Tue Aug 17, 2010 9:58 am

455 - Periodic Strings

Post by angrad » Tue Aug 17, 2010 10:04 am

I'm ACM fresh and now I'm confused in the following code.I don't know where it has mistakes.So many times did I submit.Who can help me? :-?

Code: Select all

#include <stdio.h>
#include <string.h>
int main()
{
    char str[100], buf[10];
    int i, j, len, flag, n;
    scanf("%d%*c", &n);
    while (n--)
    {
        gets(buf);
        scanf("%s%*c", str);
        len = strlen(str);
        for (i=1; i<=len; i++)
        {
            flag = 1;
            if (len%i==0)
            {
                for (j=i; j<len; j++)
                {
                    if (str[j] != str[j%i])
                    {
                        flag = 0;
                        break;
                    }
                }
                if (flag)
                {
                    printf("%d", i);
                    printf(n?("\n\n"):(""));
                    break;
                }
            }
        }
    }
    return 0;
}

mohiuddin.duet
New poster
Posts: 1
Joined: Mon Jan 03, 2011 10:45 pm

getting WA (455) please help

Post by mohiuddin.duet » Mon Jan 03, 2011 11:01 pm

...just...getting... WA .. WA .. WA on 455 and can't determine where is the problem........would somebody...help...please....... : :evil:
Here is my code:

#include <stdio.h>
#include <string.h>
int main() {
char word[85];
unsigned long n,i, j, len;
scanf("%lu",&n);
while(n--){
scanf("%s", word);
len = strlen(word);
for(i=1;i<=len;i++)
if(len%i==0){
for(j=0;j+i<len;j+=i)
if(word[j]!=word[j+i]) break;
if(j+i>=len) break;
}
printf("%lu\n\n", i);
}
return 0;
}

elmagnifico
New poster
Posts: 5
Joined: Mon Apr 30, 2012 10:15 am

Re: getting WA (455) please help

Post by elmagnifico » Tue May 01, 2012 11:10 am

I am getting WA.. Please help sumone Here is my code:

Code: Select all


import java.io.*;
import java.util.*;
class Main
{
      public static void main(String args[])throws IOException
      {
          BufferedReader x=new BufferedReader(new InputStreamReader(System.in));
          int period=0,n=0,p=0,z=0,i=0,j=0,l=0,k=0;String s="",t="";
          n=Integer.parseInt(x.readLine());
          for(i=1;i<=n;i++)
                   {
                        s=x.readLine();s=x.readLine();l=s.length();
                       check:{
                                       for(j=1;j<l;j++)
                                       {
                                                p=0;k=0;t="";period=0;
                                                if(l%j==0)
                                                            {
                                                                   p=0;k=0;t="";
                                                                   z=l/j;
                                                                   String a[]=new String[z];
                                                                   for(k=0;k<l;k++)
                                                                              {
                                                                                     if(t.length()==j)
                                                                                             {
                                                                                                   a[p]=t;t="";p++;
                                                                                             }
                                                                                     t=t+s.charAt(k);
                                                                              }
                                                                    a[p]=t;
                                                                    for(k=1;k<z;k++)
                                                                              {
                                                                                      if(!(a[0].equals(a[k])))
                                                                                      {period=-1;break;}
                                                                              }
                                                                    if(period!=-1)
                                                                              {
                                                                                      period=j;break check;
                                                                              }
                                                                }
                                                       }
                                               }
                                      if(period==0 || period==-1)
                                      period=l;
                                      System.out.println(period);
                                      if(i!=n)
                                      System.out.println();
                           }
               }
}

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

Re: getting WA (455) please help

Post by brianfry713 » Wed May 02, 2012 12:44 am

I like editing with xemacs, you can hit tab on a line to fix the indentation, which it also helps you with as you code.

As Scape pointed out, try reading input without depending on the line formatting being correct.
Check input and AC output for thousands of problems on uDebug!

cipher007
New poster
Posts: 1
Joined: Mon May 14, 2012 9:14 pm

455 - Periodic Strings

Post by cipher007 » Mon May 14, 2012 9:18 pm

getting WA...checked all cases

import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int tc = in.nextInt();
while((tc--)>0)
{
String str = in.next();
int l = str.length();
boolean flag = false;
int i;
for(i = 1; i<=l/2; i++)
{
if(l%i != 0)
continue;
boolean check = true;
String sub = str.substring(0, i);
for(int j = 0; j < l/i; j++)
{
if(!sub.equals(str.substring(j*i, (j+1)*i)))
{
check = false;
break;
}
}
if(check)
{
flag = true;
break;
}
}
if(flag)
System.out.println(i);
else
System.out.println(l);
System.out.println();
}
}
}

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

Re: 455 - Periodic Strings

Post by brianfry713 » Mon May 14, 2012 11:18 pm

Two consecutive output are separated by a blank line. Don't print a blank line at the end.
Check input and AC output for thousands of problems on uDebug!

graph_dp
New poster
Posts: 7
Joined: Fri Sep 14, 2012 8:33 am

uva=455 i am gettign WA why.............

Post by graph_dp » Thu Nov 08, 2012 8:10 pm

#include <iostream>
#include <string>
#define inf 1<<20
using namespace std;

string a;
int countSubstring(string sub)
{
if (sub.length() == 0) return 0;
int count = 0;
for (size_t offset = a.find(sub); offset != std::string::npos;
offset = a.find(sub, offset + sub.length()))
{
++count;
}
return count;
}

int main()
{
string s;int n,min;
cin>>n;
bool flag=false;
while(n--){
if(flag){cout<<endl;}
flag=true;
cin>>a;min=inf;
for(int i=0;i<a.size()/2;i++){
s+=a;
int sub_len=s.size();
int str_count=countSubstring(s);
if(sub_len*str_count==a.size()){
if(sub_len<min){
min=sub_len;

}

}


}
cout<<min<<endl;
a.erase();
s.erase();

}


return 0;
}

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

Re: uva=455 i am gettign WA why.............

Post by brianfry713 » Fri Nov 09, 2012 11:00 pm

Input:

Code: Select all

1

asdf
correct output:4
Check input and AC output for thousands of problems on uDebug!

Post Reply

Return to “Volume 4 (400-499)”