455 - Periodic Strings
Moderator: Board moderators
455 - Periodic Strings
Pleaes tell me the fault of this code.
Why it is W.A??
#include<iostream.h>
#include<string.h>
int main()
{
char t[100];
int r;
while(cin>>t)
{
int s=strlen(t);
char p[100];
char ch[2]={0};
ch[0]=t[0];
strcpy(p,ch);
int i;
for(i=1,r=1;i<s;i++,r++)
{
int l,k=1,count,max,index=0,b=0;
max=s-r+1;
while(k<=max)
{
l=1;
count=0;
while(l<=r)
{
if(p[l-1]!=t[k+l-1-1])goto a;
else count++;
l++;
}
a: k++;
if(count==r)
{
b++;
index=b;
}
}
if(index*r==s)break;
ch[0]=t;
strcat(p,ch);
}
cout<<r<<"\n";
}
return 0;
}
Why it is W.A??
#include<iostream.h>
#include<string.h>
int main()
{
char t[100];
int r;
while(cin>>t)
{
int s=strlen(t);
char p[100];
char ch[2]={0};
ch[0]=t[0];
strcpy(p,ch);
int i;
for(i=1,r=1;i<s;i++,r++)
{
int l,k=1,count,max,index=0,b=0;
max=s-r+1;
while(k<=max)
{
l=1;
count=0;
while(l<=r)
{
if(p[l-1]!=t[k+l-1-1])goto a;
else count++;
l++;
}
a: k++;
if(count==r)
{
b++;
index=b;
}
}
if(index*r==s)break;
ch[0]=t;
strcat(p,ch);
}
cout<<r<<"\n";
}
return 0;
}
this problem has multiple input, first read the number of strings follow.
http://acm.uva.es/problemset/minput.html
http://acm.uva.es/problemset/minput.html
455 Periodic Strings
What's wrong in my program?
I got WA.
I cannot find the fault
help me
thanks!
Here is my source.
----------------------------
#include "stdio.h"
#include "string.h"
void main()
{
int i,j,n,t,r,z;
char arr[100],brr[100];
scanf("%d\n",&r);
for(z=1;z<=r;z++)
{
scanf("\n");
gets(arr);
for(i=0;i<strlen(arr);i++)
{
n=i+1;
for( j=0 ;j<=i ; j++ )
brr[j]=arr[j];
for(j=i+1;j<strlen(arr);j++)
{
brr[j]=brr[j%n];
}
brr[j]=0;
if(strcmp(arr,brr)==0)
{
printf("%d\n\n",n);
break;
}
}
}
}
I got WA.
I cannot find the fault
help me
thanks!
Here is my source.
----------------------------
#include "stdio.h"
#include "string.h"
void main()
{
int i,j,n,t,r,z;
char arr[100],brr[100];
scanf("%d\n",&r);
for(z=1;z<=r;z++)
{
scanf("\n");
gets(arr);
for(i=0;i<strlen(arr);i++)
{
n=i+1;
for( j=0 ;j<=i ; j++ )
brr[j]=arr[j];
for(j=i+1;j<strlen(arr);j++)
{
brr[j]=brr[j%n];
}
brr[j]=0;
if(strcmp(arr,brr)==0)
{
printf("%d\n\n",n);
break;
}
}
}
}
-
- New poster
- Posts: 1
- Joined: Sun Aug 18, 2002 4:31 am
Why WA?
My code is :
[c]
#include<stdio.h>
#include<string.h>
void main()
{
char string[81];
int period=0,N,counter,length,index,flag;
scanf("%d",&N);
for(counter = 1; counter <= N; counter++)
{
scanf("%s",&string);
length = strlen(string);
for(index = 0; index < length; index++)
{
period++;
if(string[0] == string [index+1])
{
printf("%d\n",period);
flag = 1;
break;
}
else
flag = 0;
}
if(flag == 0)
{
printf("%d\n",length);
break;
}
if(counter < N)
printf("\n");
period = 0;
}
}[/c]
Can anybody help me?
[c]
#include<stdio.h>
#include<string.h>
void main()
{
char string[81];
int period=0,N,counter,length,index,flag;
scanf("%d",&N);
for(counter = 1; counter <= N; counter++)
{
scanf("%s",&string);
length = strlen(string);
for(index = 0; index < length; index++)
{
period++;
if(string[0] == string [index+1])
{
printf("%d\n",period);
flag = 1;
break;
}
else
flag = 0;
}
if(flag == 0)
{
printf("%d\n",length);
break;
}
if(counter < N)
printf("\n");
period = 0;
}
}[/c]
Can anybody help me?
-
- Learning poster
- Posts: 94
- Joined: Wed Jul 31, 2002 12:44 pm
- Location: Dacca, Bangladesh
- Contact:
p455 any special input?
i got p10298(power strings) accepted but i get WA for p455 using the same algo.
can anyone tell me if there is any special input for this prob?
thanks in advance
can anyone tell me if there is any special input for this prob?
![:roll:](./images/smilies/icon_rolleyes.gif)
thanks in advance
![:wink:](./images/smilies/icon_wink.gif)
im the lazy lazy boy
-
- Learning poster
- Posts: 94
- Joined: Wed Jul 31, 2002 12:44 pm
- Location: Dacca, Bangladesh
- Contact:
i am sure 'bout the fact that my code to get the period is right, because is just said that i got p10298 accepted.
and i'm also aware that this is a mulinput problem. plz see the part the ways i take the data...
[c]
int main(){
char fstr[101];
long powerS, mulinput;
fgets(fstr, sizeof(fstr), stdin);
sscanf(fstr, "%ld", &mulinput);
fgets(fstr, sizeof(fstr), stdin);
while(mulinput){
fgets(fstr, sizeof(fstr), stdin);
sscanf(fstr, "%s", str); /* str[1005] is global */
fgets(fstr, sizeof(fstr), stdin); /* track the blank line */
powerS = get_power();
printf("%ld\n", powerS);
mulinput--;
}
}
[/c]
and i've also tried with
[c]
int main(){
char fstr[101];
long powerS, mulinput;
fgets(fstr, sizeof(fstr), stdin);
sscanf(fstr, "%ld", &mulinput);
fgets(fstr, sizeof(fstr), stdin);
while(mulinput){
while(fgets(fstr, sizeof(fstr), stdin)!=NULL){
if (!fstr[0] || fstr[0]=='\n')
break;
sscanf(fstr, "%s", str); /* str[1005] is global */
powerS = get_power();
printf("%ld\n", powerS);
}
mulinput--;
}
}
[/c]
so what now?![:(](./images/smilies/icon_frown.gif)
![:wink:](./images/smilies/icon_wink.gif)
and i'm also aware that this is a mulinput problem. plz see the part the ways i take the data...
[c]
int main(){
char fstr[101];
long powerS, mulinput;
fgets(fstr, sizeof(fstr), stdin);
sscanf(fstr, "%ld", &mulinput);
fgets(fstr, sizeof(fstr), stdin);
while(mulinput){
fgets(fstr, sizeof(fstr), stdin);
sscanf(fstr, "%s", str); /* str[1005] is global */
fgets(fstr, sizeof(fstr), stdin); /* track the blank line */
powerS = get_power();
printf("%ld\n", powerS);
mulinput--;
}
}
[/c]
and i've also tried with
[c]
int main(){
char fstr[101];
long powerS, mulinput;
fgets(fstr, sizeof(fstr), stdin);
sscanf(fstr, "%ld", &mulinput);
fgets(fstr, sizeof(fstr), stdin);
while(mulinput){
while(fgets(fstr, sizeof(fstr), stdin)!=NULL){
if (!fstr[0] || fstr[0]=='\n')
break;
sscanf(fstr, "%s", str); /* str[1005] is global */
powerS = get_power();
printf("%ld\n", powerS);
}
mulinput--;
}
}
[/c]
so what now?
![:(](./images/smilies/icon_frown.gif)
I have some problems with this program
it gots WA, but I have no idea why
Here's my source, maybe You can find the bug, because i have spent a lot of time on searching it, but i haven't found it. Maybe it's a problem with input?
[cpp]// Periodic Strings
#include <iostream.h>
#include <string.h>
char word[100];
int numoftests;
void check()
{
int length=1;
for (int i=1; i<strlen(word); i++)
if (word!=word[i-length])
length=i;
if (strlen(word)%length) length=strlen(word);
cout << length;
}
int main()
{
cin >> numoftests;
for (int i=0; i<numoftests; i++)
{
cin >> word;
if (i) cout << "\n\n";
check();
}
return 0;
}[/cpp]
Thanx[/cpp]
![:cry:](./images/smilies/icon_cry.gif)
![:P](./images/smilies/icon_razz.gif)
[cpp]// Periodic Strings
#include <iostream.h>
#include <string.h>
char word[100];
int numoftests;
void check()
{
int length=1;
for (int i=1; i<strlen(word); i++)
if (word!=word[i-length])
length=i;
if (strlen(word)%length) length=strlen(word);
cout << length;
}
int main()
{
cin >> numoftests;
for (int i=0; i<numoftests; i++)
{
cin >> word;
if (i) cout << "\n\n";
check();
}
return 0;
}[/cpp]
Thanx[/cpp]
Doodge
-------------------------------------------------------
Things should be as simple as only possible, but no simpler.
-------------------------------------------------------
Things should be as simple as only possible, but no simpler.
Sorry. I was worried that your method wasn't quite right, but I couldn't think of a case where it gave the wrong answer, so I hoped it would work.
A bit of thought is needed to find a counterexample:
Input:
Offhand, I can't think of an easy way to fix this without totally rewriting your check() method.![:(](./images/smilies/icon_frown.gif)
![:)](./images/smilies/icon_smile.gif)
Input:
The output should be 6, not 12. Tracing through check(), length goes from 2 -> 4 -> 8 -> 10. But 12 % 10 is 2, so it outputs 12 even though the right answer is 6.1
abaaababaaab
Offhand, I can't think of an easy way to fix this without totally rewriting your check() method.
![:(](./images/smilies/icon_frown.gif)
wonder what's wrong here
hello all,
I'm wondering why I got stuck on this problem... Is there some fancy input I don't handle quite right or I don't handle the multiple input well enough?
all comments are appreciated
[c]#include <stdio.h>
#include <string.h>
#define MX 81
int comp(char *what,char *where,int l) {
int i=0;
while(i<l && what==where) i++;
return i==l;
}
main() {
char s[MX],tmp[MX],*p;
int i,j,L,N;
scanf("%d",&N);
while(N>0) {
scanf("%s",&s);
L=strlen(s);
for(i=1,j=1;j && i<L;i++) {
tmp[i-1]=s[i-1];
if(L%i==0) {
j=L/i-1;
p=&s;
while(j>0 && comp(tmp,p,i)) {
j--;
p=&p;
}
}
}
printf("%d\n",(i==L)?L:i-1);
if(--N) printf("\n");
}
}[/c]
I'm wondering why I got stuck on this problem... Is there some fancy input I don't handle quite right or I don't handle the multiple input well enough?
all comments are appreciated
[c]#include <stdio.h>
#include <string.h>
#define MX 81
int comp(char *what,char *where,int l) {
int i=0;
while(i<l && what==where) i++;
return i==l;
}
main() {
char s[MX],tmp[MX],*p;
int i,j,L,N;
scanf("%d",&N);
while(N>0) {
scanf("%s",&s);
L=strlen(s);
for(i=1,j=1;j && i<L;i++) {
tmp[i-1]=s[i-1];
if(L%i==0) {
j=L/i-1;
p=&s;
while(j>0 && comp(tmp,p,i)) {
j--;
p=&p;
}
}
}
printf("%d\n",(i==L)?L:i-1);
if(--N) printf("\n");
}
}[/c]
Dealing with failure is easy: Work hard to improve.
Success is also easy to handle: You've solved the wrong problem. Work hard to improve.
Success is also easy to handle: You've solved the wrong problem. Work hard to improve.