850 - Crypt Kicker II

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

Moderator: Board moderators

amine.hamdaoui
New poster
Posts: 10
Joined: Tue Aug 07, 2007 7:33 pm

850 - Crypt Kicker II

Post by amine.hamdaoui » Wed Dec 12, 2007 9:00 pm

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

ChainRule
New poster
Posts: 4
Joined: Sun Nov 18, 2007 9:07 am

Re: #850[Crypt KickerII]:HELP!

Post by ChainRule » Mon Apr 07, 2008 4:47 pm

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

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Re: 850 - Crypt Kicker II

Post by Jan » Mon Apr 07, 2008 7:27 pm

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.
Ami ekhono shopno dekhi...
HomePage

tarzxvf
New poster
Posts: 5
Joined: Sun Apr 20, 2008 5:05 pm

Re: 850 - Crypt Kicker II

Post by tarzxvf » Sun Apr 20, 2008 5:19 pm

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

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Re: 850 - Crypt Kicker II

Post by Jan » Sun Apr 20, 2008 6:04 pm

Your code doesn't even pass the sample. Check the last line carefully. ( 'arent they' )
Ami ekhono shopno dekhi...
HomePage

tarzxvf
New poster
Posts: 5
Joined: Sun Apr 20, 2008 5:05 pm

Re: 850 - Crypt Kicker II

Post by tarzxvf » Sun Apr 20, 2008 6:18 pm

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?

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Re: 850 - Crypt Kicker II

Post by Jan » Mon Apr 21, 2008 4:52 am

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.
Ami ekhono shopno dekhi...
HomePage

tarzxvf
New poster
Posts: 5
Joined: Sun Apr 20, 2008 5:05 pm

Re: 850 - Crypt Kicker II

Post by tarzxvf » Mon Apr 21, 2008 8:56 am

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

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Re: 850 - Crypt Kicker II

Post by Jan » Tue Apr 22, 2008 4:15 am

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.
Ami ekhono shopno dekhi...
HomePage

tarzxvf
New poster
Posts: 5
Joined: Sun Apr 20, 2008 5:05 pm

Re: 850 - Crypt Kicker II

Post by tarzxvf » Thu Apr 24, 2008 6:31 pm

I convered fgets to get, but still WA.
Anyway, thanks for your help.

x140l31
Learning poster
Posts: 69
Joined: Tue Jan 30, 2007 12:51 am

Re: 850 - Crypt Kicker II

Post by x140l31 » Thu Aug 28, 2008 1:18 am

any test case for help me?? :(

Code: Select all

Removed after AC

Thanks to rator10
Last edited by x140l31 on Tue Jun 30, 2009 6:57 pm, edited 1 time in total.

edutb
New poster
Posts: 1
Joined: Tue Sep 02, 2008 6:38 am

Re: 850 - Crypt Kicker II

Post by edutb » Tue Sep 02, 2008 6:45 am

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.

amr saqr
New poster
Posts: 29
Joined: Tue Mar 11, 2008 6:35 pm

Re: 850 - Crypt Kicker II

Post by amr saqr » Tue Sep 09, 2008 4:33 pm

it gives me RE :S:S:S:S:S

Code: Select all

Removed after AC
Last edited by amr saqr on Mon Oct 06, 2008 8:09 pm, edited 1 time in total.
C++ Is The Best.

amr saqr
New poster
Posts: 29
Joined: Tue Mar 11, 2008 6:35 pm

Re: 850 - Crypt Kicker II

Post by amr saqr » Wed Sep 10, 2008 9:26 am

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,

Code: Select all

Removed after AC
Last edited by amr saqr on Mon Oct 06, 2008 8:09 pm, edited 1 time in total.
C++ Is The Best.

alirezanoori
New poster
Posts: 26
Joined: Fri Jan 02, 2009 12:41 am

850 - Crypt Kicker II

Post by alirezanoori » Thu Jan 15, 2009 2:48 pm

-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
Last edited by alirezanoori on Fri Jan 23, 2009 4:24 pm, edited 1 time in total.

Post Reply

Return to “Volume 8 (800-899)”