Page 4 of 5

Re: Help with 455

Posted: Wed Jan 21, 2009 4:46 pm
by mf
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.

Re: Help with 455

Posted: Thu Jan 22, 2009 8:27 am
by Obaida
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:

Re: Help with 455

Posted: Thu Jan 22, 2009 10:32 am
by mf
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.

Re: 455 - Periodic Strings

Posted: Thu Jan 22, 2009 11:22 am
by Obaida
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.

Re: 455 - Periodic Strings

Posted: Thu Jan 22, 2009 11:41 am
by mf
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.

Re: 455 - Periodic Strings

Posted: Thu Jan 22, 2009 12:06 pm
by Obaida
I got Acc in this problem. :) :) :)
Thank you so much.

Re: Problem 455: WA

Posted: Thu Jun 03, 2010 9:05 pm
by Imran Bin Azad
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;
}

455 - Periodic Strings

Posted: Tue Aug 17, 2010 10:04 am
by angrad
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;
}

getting WA (455) please help

Posted: Mon Jan 03, 2011 11:01 pm
by mohiuddin.duet
...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;
}

Re: getting WA (455) please help

Posted: Tue May 01, 2012 11:10 am
by elmagnifico
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();
                           }
               }
}

Re: getting WA (455) please help

Posted: Wed May 02, 2012 12:44 am
by brianfry713
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.

455 - Periodic Strings

Posted: Mon May 14, 2012 9:18 pm
by cipher007
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();
}
}
}

Re: 455 - Periodic Strings

Posted: Mon May 14, 2012 11:18 pm
by brianfry713
Two consecutive output are separated by a blank line. Don't print a blank line at the end.

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

Posted: Thu Nov 08, 2012 8:10 pm
by graph_dp
#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;
}

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

Posted: Fri Nov 09, 2012 11:00 pm
by brianfry713
Input:

Code: Select all

1

asdf
correct output:4