Page 3 of 6
850 - Crypt Kicker II
Posted: Wed Dec 12, 2007 9:00 pm
by amine.hamdaoui
Could you tell me what's wrong in my code?
Code: Select all
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
string ss="the quick brown fox jumps over the lazy dog";
char code[200];
bool check(string line)
{
int i,size;
string mot;
for(i='a'; i<='z'; i++) code[i]='0';
size = line.size();
if(size == ss.size())
{
if(line[3]==' ' && line[9]==' ' && line[15]==' ' && line[19]==' ' && line[25]==' ' && line[30]==' ' && line[34]==' ' && line[39]==' ')
{
for(i=0 ; i<size ; i++) code[line[i]]=ss[i];
for(i='a' ; i<='z' ; i++) if(code[i]=='0') return false;
return true;
}
}
return false;
}
int main()
{
int nbcase,i,j,k,r,size;
string line,ss,vect[101];
cin>>nbcase;
cin.get();
for(i=0 ; i<nbcase ; i++)
{
j=0;
if(i==0) getline(cin,line);
while(true)
{
getline(cin,line);
if(line.size()==0) break;
vect[j] = line;
j++;
}
for(k=0 ; k<j; k++)
{
if(check(vect[k])) break;
}
if(k==j) cout<<"No solution."<<endl;
else
{
for(k=0 ; k<j ; k++)
{
size = vect[k].size();
for(r=0 ; r<size ; r++)
{
if(vect[k][r]!=' ') cout<<code[vect[k][r]];
else cout<<' ';
}
cout<<endl;
}
}
cout<<endl;
}
return 0;
}
Re: #850[Crypt KickerII]:HELP!
Posted: Mon Apr 07, 2008 4:47 pm
by ChainRule
Code: Select all
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char Test[1000]="xnm ceuob lrtzv ita hegfd tsmr xnm ypwq ktj";
int lenA=0;
int lenB=0;
int Num=0;
int x=0;
int i=0;
char sort1;
char sort2;
int counter1=0;
int counter2=0;
int counter3=0;
scanf("%d",&Num);
scanf("%c",&sort1);
scanf("%c",&sort2);
for(i=0;i<Num;i++)
{
char A[20000];
char B[20000];
counter1=0;
counter2=0;
while(1)
{
gets(A);
if(A[0]=='\0')
{
break;
}
else
{
if(strcmp(A,Test)==0)
{
counter2++;
}
lenA=strlen(A);
for(x=0;x<lenA;x++)
{
if(A[x]==97)
A[x]=A[x]+23;
else if(A[x]==98)
A[x]=A[x]+9;
else if(A[x]==99)
A[x]=A[x]+14;
else if(A[x]==100)
A[x]=A[x]+15;
else if(A[x]==101)
A[x]=A[x]+16;
else if(A[x]==102)
A[x]=A[x]+10;
else if(A[x]==103)
A[x]=A[x]+6;
else if(A[x]==104)
A[x]=A[x]+2;
else if(A[x]==105)
A[x]=A[x]-3;
else if(A[x]==106)
A[x]=A[x]-3;
else if(A[x]==107)
A[x]=A[x]-7;
else if(A[x]==108)
A[x]=A[x]-10;
else if(A[x]==109)
A[x]=A[x]-8;
else if(A[x]==110)
A[x]=A[x]-6;
else if(A[x]==111)
A[x]=A[x]-12;
else if(A[x]==112)
A[x]=A[x]-15;
else if(A[x]==113)
A[x]=A[x]+8;
else if(A[x]==114)
A[x]=A[x];
else if(A[x]==115)
A[x]=A[x]+3;
else if(A[x]==116)
A[x]=A[x]-5;
else if(A[x]==117)
A[x]=A[x]-12;
else if(A[x]==118)
A[x]=A[x]-8;
else if(A[x]==119)
A[x]=A[x]+3;
else if(A[x]==120)
A[x]=A[x]-4;
else if(A[x]==121)
A[x]=A[x]-13;
else if(A[x]==122)
A[x]=A[x]-3;
else
A[x]=A[x];
B[counter1]=A[x];
counter1++;
if(x==lenA-1)
{
B[counter1]=1;
counter1++;
}
}
}
}
if(counter3!=0)
printf("\n");
lenB=strlen(B);
if(counter2==0)
{
printf("No solution.\n");
}
else
{
for(x=0;x<lenB;x++)
{
if(B[x]==1)
printf("\n");
else
printf("%c",B[x]);
}
}
counter3++;
}
return 0;
}
I got runtime error,
please help me thanks.
[Edited by Jan] Use code tags
Re: 850 - Crypt Kicker II
Posted: Mon Apr 07, 2008 7:27 pm
by Jan
In my compiler your code doesn't even pass the samples. Since there are blank line between cases, so, the last line of the last case will be terminated by EOF (you will find no '\n' here).
This should be replaced ..
Code: Select all
while(1)
{
gets(A);
if(A[0]=='\0')
{
break;
}
as
Code: Select all
while(gets(A))
{
if(A[0]=='\0')
{
break;
}
And your method is not correct.
Re: 850 - Crypt Kicker II
Posted: Sun Apr 20, 2008 5:19 pm
by tarzxvf
Could someone please help me?
I got WA.
Thanks.
Code: Select all
#include <stdio.h>
#include <string.h>
#define TARGET "the quick brown fox jumps over the lazy dog"
#define MAX_CHARS 100
#define MAX_LINES 100
#define CODETABLE_SIZE 26
#define CODETABLE_BASE (int)'a'
int deduces(int, char *, char *);
void decrypt(int, char *, char *);
int main()
{
char lines[MAX_LINES][MAX_CHARS], codetable[CODETABLE_SIZE], *line;
int i, nlines, ncases;
scanf("%d", &ncases);
getchar();
getchar();
for(i = 0; i < ncases; i++){
memset(lines, 0, sizeof(char) * MAX_CHARS * MAX_LINES);
nlines = 0;
line = (char *)lines;
while(fgets(line, sizeof(char) * MAX_CHARS, stdin) && strlen(line) > 1){
nlines++;
line = line + MAX_CHARS;
}
if(deduces(nlines, (char *)lines, codetable))
decrypt(nlines, (char *)lines, codetable);
else
printf("No solution.\n");
printf("\n");
}
return 0;
}
int deduces(int nlines, char *lines, char *codetable)
{
int i, j;
char *target = TARGET;
char *line = lines;
size_t len;
int codeindex;
int codes = 0;
for(i = 0; i < nlines; i++){
memset(codetable, 0, sizeof(char) * CODETABLE_SIZE);
len = strlen(line) - 1;
if(len == strlen(target)){
for(j = 0; j < len; j++){
codeindex = (int)line[j] - CODETABLE_BASE;
if(line[j] == ' '){
if(target[j] != ' ')
break;
}else if(target[j] == ' '){
break;
}else if(codetable[codeindex] == '\0'){
if(strchr(codetable, target[j])){
break;
}else{
codetable[codeindex] = target[j];
}
}else if(codetable[codeindex] != target[j]){
break;
}
}
if(j == len)
break;
}
line = line + MAX_CHARS;
}
return (i < nlines);
}
void decrypt(int nlines, char *lines, char *codetable)
{
int i, j;
char *line;
line = lines;
for(i = 0; i < nlines; i++){
for(j = 0; j < strlen(line) - 1; j++)
if(line[j] != ' ')
line[j] = codetable[(int)line[j] - CODETABLE_BASE];
printf("%s", line);
line = line + MAX_CHARS;
}
}
Re: 850 - Crypt Kicker II
Posted: Sun Apr 20, 2008 6:04 pm
by Jan
Your code doesn't even pass the sample. Check the last line carefully. ( 'arent they' )
Re: 850 - Crypt Kicker II
Posted: Sun Apr 20, 2008 6:18 pm
by tarzxvf
My output
Code: Select all
now is the time for all good men to come to the aid of the party
the quick brown fox jumps over the lazy dog
programming contests are fun arent they
Could you tell me what's wrong?
Re: 850 - Crypt Kicker II
Posted: Mon Apr 21, 2008 4:52 am
by Jan
Your code generates a blank line after each case. But the problem requires a blank line between cases.
However, just take the sample and remove the last '\n' (newline) character. Then your code fails.
Re: 850 - Crypt Kicker II
Posted: Mon Apr 21, 2008 8:56 am
by tarzxvf
I fixed this bug, but still WA.
My code:
Code: Select all
#include <stdio.h>
#include <string.h>
#define TARGET "the quick brown fox jumps over the lazy dog"
#define TARGETLEN 43
#define MAX_CHARS 100
#define MAX_LINES 100
#define CODETABLE_SIZE 26
#define CODETABLE_BASE (int)'a'
int checkline(char *, char *);
void decrypt(int, char *, char *);
int main()
{
char lines[MAX_LINES][MAX_CHARS], codetable[CODETABLE_SIZE], *line;
int i, nlines, ncases, decodable;
scanf("%d", &ncases);
getchar();
getchar();
for(i = 0; i < ncases; i++){
memset(lines, 0, sizeof(char) * MAX_CHARS * MAX_LINES);
nlines = 0;
line = (char *)lines;
decodable = 0;
while(fgets(line, sizeof(char) * MAX_CHARS, stdin) && strlen(line) > 1){
if(decodable == 0 && strlen(line) - 1 == TARGETLEN && checkline(line, codetable))
decodable = 1;
nlines++;
line = line + MAX_CHARS;
}
if(decodable == 1){
decrypt(nlines, (char *)lines, codetable);
}else{
printf("No solution.\n");
}
if(i < ncases - 1)
printf("\n");
}
return 0;
}
int checkline(char *line, char *codetable)
{
int i;
char *target = TARGET;
size_t len = strlen(line) - 1;
int codeindex;
memset(codetable, 0, sizeof(char) * CODETABLE_SIZE);
for(i = 0; i < len; i++){
codeindex = (int)line[i] - CODETABLE_BASE;
if(line[i] == ' '){
if(target[i] != ' ')
break;
}else if(target[i] == ' '){
break;
}else if(codetable[codeindex] == '\0'){
if(strchr(codetable, target[i])){
break;
}else{
codetable[codeindex] = target[i];
}
}else if(codetable[codeindex] != target[i]){
break;
}
}
if(i == len)
return 1;
else
return 0;
}
void decrypt(int nlines, char *lines, char *codetable)
{
int i, j;
char *line;
line = lines;
for(i = 0; i < nlines; i++){
for(j = 0; j < strlen(line) - 1; j++)
if(line[j] != ' ')
line[j] = codetable[(int)line[j] - CODETABLE_BASE];
printf("%s", line);
line = line + MAX_CHARS;
}
}
My input
Code: Select all
3
vtz ud xnm xugm itr pyy jttk gmv xt otgm xt xnm puk ti xnm fprxq
xnm ceuob lrtzv ita hegfd tsmr xnm ypwq ktj
frtjrpgguvj otvxmdxd prm iev prmvx xnmq
awerwaerwrwa
vtz ud xnm xugm itr pyy jttk gmv xt otgm xt xnm puk ti xnm fprxq
xnm ceuob lrtzv ita hegfd tsmr xnm ypwq ktj
frtjrpgguvj otvxmdxd prm iev prmvx xnmq
awerwaerwrwa
vtz ud xnm xugm itr pyy jttk gmv xt otgm xt xnm puk ti xnm fprxq
xnm ceuob lrtzv ita hegfd tsmr xnm ypwq ktj
frtjrpgguvj otvxmdxd prm iev prmvx xnmq
awerwaerwrwa
vtz ud xnm xugm itr pyy jttk gmv xt otgm xt xnm puk ti xnm fprxq
xnm ceuob lrtzv ita hegfd tsmr xnm ypwq ktj
frtjrpgguvj otvxmdxd prm iev prmvx xnmq
awerwaerwrwa
vtz ud xnm xugm itr pyy jttk gmv xt otgm xt xnm puk ti xnm fprxq
xnm ceuob lrtzv ita hegfd tsmr xnm ypwq ktj
frtjrpgguvj otvxmdxd prm iev prmvx xnmq
awerwaerwrwa
My output
Code: Select all
now is the time for all good men to come to the aid of the party
the quick brown fox jumps over the lazy dog
programming contests are fun arent they
xzurzxurzrzx
now is the time for all good men to come to the aid of the party
the quick brown fox jumps over the lazy dog
programming contests are fun arent they
xzurzxurzrzx
now is the time for all good men to come to the aid of the party
the quick brown fox jumps over the lazy dog
programming contests are fun arent they
xzurzxurzrzx
now is the time for all good men to come to the aid of the party
the quick brown fox jumps over the lazy dog
programming contests are fun arent they
xzurzxurzrzx
now is the time for all good men to come to the aid of the party
the quick brown fox jumps over the lazy dog
programming contests are fun arent they
xzurzxurzrzx
Re: 850 - Crypt Kicker II
Posted: Tue Apr 22, 2008 4:15 am
by Jan
Your code looks correct. But still if there is no newline character after the last case, your code will not print a newline character after the last line. (I doubt there is such case!)
However, try converting fgets() to gets(). Hope it will help.
Re: 850 - Crypt Kicker II
Posted: Thu Apr 24, 2008 6:31 pm
by tarzxvf
I convered fgets to get, but still WA.
Anyway, thanks for your help.
Re: 850 - Crypt Kicker II
Posted: Thu Aug 28, 2008 1:18 am
by x140l31
any test case for help me??
Code: Select all
Removed after AC
Thanks to rator10
Re: 850 - Crypt Kicker II
Posted: Tue Sep 02, 2008 6:45 am
by edutb
I tried all the imputs posted here but i still got w.a t.t
could someone help us with more critical imputs?
thx a lot.
Re: 850 - Crypt Kicker II
Posted: Tue Sep 09, 2008 4:33 pm
by amr saqr
it gives me RE :S:S:S:S:S
Re: 850 - Crypt Kicker II
Posted: Wed Sep 10, 2008 9:26 am
by amr saqr
i've modified my code, tested all the cases here and it passed them all,
it gives me Wrong answer now :S,
here is my code,
850 - Crypt Kicker II
Posted: Thu Jan 15, 2009 2:48 pm
by alirezanoori
-What if the input has characters other than the ones on the decryption string? Is it "No solution." or we should print that character itself?
My program works for ALL of test data in this forum. But gets WA!
Please help me. It's driving me crazy!!!
Code: Select all
Removed After AC
Note: Bug was:
1- Not printing new line after each test case.
2- Setting last (instead of first) key.
Good luck