245 - Uncompress

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

Moderator: Board moderators

liang1425
New poster
Posts: 5
Joined: Thu May 26, 2005 8:20 am

245 WA

Post by liang1425 »

I always get WA,but I don't know why.
(The testdata is correct)
Please help me~~Thanks a lot

Code: Select all

/*Q245: Uncompress*/
#include <stdio.h>
#include <stdlib.h>
typedef struct listnode *ptr;
typedef struct listnode{
   char word[50];
   ptr link;
};
ptr creat(char *word){
   ptr node;
   node=malloc(sizeof(struct listnode));
   strcpy(node->word,word);
   node->link=NULL;
   return node;
}
ptr insert(ptr list,char *word){
   ptr node;
   node=creat(word);
   node->link=list;
   return node;
}
inorder(ptr list,int n){
  int i;
  ptr phead,temp;

  phead=list;
  for(i=1;i<n;i++){
     temp=list;
     list=list->link;
  }
  printf("%s",list->word);
  if(n>1){
     temp->link=list->link;
     list=insert(phead,list->word);
  }
  return list;
}
int main()
{
 int p=0,k=0,s=0,n=0;
 char t,word[50];
 ptr list;
 list=creat(" ");

 while(scanf("%c",&t)!=EOF && t!='0'){
    if(isalpha(t)){word[p++]=t; printf("%c",t); s=1;}
    else if(t>=48 && t<58){ k=1; n*=10; n+=t-48; }
    else{
       if(!k && s){word[p]='\0'; list=insert(list,word); p=0; s=0; printf("%c",t);}
       else if(k){list=inorder(list,n);k=0; n=0; printf("%c",t);}
       else printf("%c",t);
    }
 }

      return 0;
}
Jeff
New poster
Posts: 7
Joined: Mon May 02, 2005 5:39 pm

245 WA

Post by Jeff »

I don't dnow why....please help me....

Code: Select all

cut after AC...^___^......
IRA
Learning poster
Posts: 82
Joined: Sat Jan 07, 2006 6:52 am

245 Help

Post by IRA »

Input Data:

Dear Sally,

Please, please do it--1 would 4
Mary very, 1 much. And 4 6
8 everything in 5's power to make
14 pay off for you.

-- Thank 2 18 18--
0

Who can tell me why the output is like as follow?
==================================
Dear Sally,

Please, please do it--it would please
Mary very, very much. And Mary would
do everything in Mary's power to make
it pay off for you.

-- Thank you very much--
==================================
Thanks....
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

Read carefully...

Input:

Code: Select all

Dear Sally, 

Please, please do it--1 would 4 
Mary very, 1 much. And 4 6 
8 everything in 5's power to make 
14 pay off for you. 

-- Thank 2 18 18-- 
0 
Think that you have a linked list of the words. Any time you get a word it will be added. If you get a number n then go backwords and find the nth word. Add the word in the linked list and delete the previous one.

Output:

Code: Select all

Dear Sally, 

Please, please do it--it would please 
Mary very, very much. And Mary would 
do everything in Mary's power to make 
it pay off for you. 

-- Thank you very much-- 
Hope it works.
Ami ekhono shopno dekhi...
HomePage
lulo
New poster
Posts: 1
Joined: Sat Feb 18, 2006 1:44 pm

245 Compile Error

Post by lulo »

I can compile it in window, but don't know why there is a compile error when i submit the code to judge.

Code: Select all

#include <iostream>
#include <ctype.h>

using namespace std;

struct node {
	char word[51];
	node *next;
	int charno;
	node *prev;
};

void output(node *tail) {
	node *ptr;
	while (tail) {
		cout << tail->word;
		ptr = tail;
		tail = tail->prev;
		delete ptr;
	}
}

int main() {
	int i;
	node *wlist;
	node *ptr;
	node *pop;
	node *tail;
	char c;

	wlist = NULL;
	ptr = wlist;

	c = getchar();
	while (true) {
		// for char
		while (isalpha(c)) {
			i = 0;
			pop = new node;
			pop->charno = 0;
			while (isalpha(c)) {
				pop->word[i++] = c;
				c = getchar();
			}
			pop->word[i] = 0;
			if (! wlist) {
				pop->next = pop;
				pop->prev = NULL;
				tail = pop;
			} else {
				pop->next = wlist;
				pop->prev = NULL;
				wlist->prev = pop;
			}
			wlist = pop;
		}
		// for number
		while (isdigit(c)) {
			if (c == '0') {
				// output
				output(tail);
				exit(0);
			}
			i = c - '0';
			c = getchar();
			while (isdigit(c)) {
				i *= 10;
				i += c - '0';
				c = getchar();
			}
			ptr = wlist;
			pop = new node;
			pop->charno = i;
			for (;i>0;i--) {
				if ((ptr->charno != 0) && (ptr->charno<i))
					while ((ptr->charno != 0) && (ptr->charno<i)) ptr = ptr->next;
				else {
					ptr = ptr->next;
					while ((ptr->charno != 0) && (ptr->charno<i)) ptr = ptr->next;
				}
			}
			strcpy(pop->word, ptr->word);
			pop->next = wlist;
			pop->prev = NULL;
			wlist->prev = pop;
			wlist = pop;
		}
		// for non number and char
		while (! (isdigit(c) || isalpha(c))) {
			i = 0;
			pop = new node;
			pop->charno = -1;
			while (! (isdigit(c) || isalpha(c)) && i<50) {
				pop->word[i++] = c;
				c = getchar();
			}
			pop->word[i] = 0;
			if (! wlist) {
				pop->next = pop;
				pop->prev = NULL;
				tail = pop;
			} else {
				pop->next = wlist;
				pop->prev = NULL;
				wlist->prev = pop;
			}
			wlist = pop;
		}
	}
	return 0;
}
sclo
Guru
Posts: 519
Joined: Mon Jan 23, 2006 10:45 pm
Location: Vancouver, BC, Canada
Contact:

Post by sclo »

your code was way too long to read. It can definitely be done in less than 40 lines of code.
Experimenter
Learning poster
Posts: 76
Joined: Thu Mar 13, 2003 5:12 am
Location: Russia

Post by Experimenter »

sclo wrote:your code was way too long to read. It can definitely be done in less than 40 lines of code.
I am sorry about that.
it would be great if you replied this post. really.
kwedeer
New poster
Posts: 44
Joined: Thu Dec 15, 2005 11:28 pm

Post by kwedeer »

so - this is not direct reply for problem in the first post of thread - but - my usual request for help!!!

So - here is my code - array pieces[...] contain the words to be exchanged by numbers in encryption/decription. No special collection (e.g. very nice Classes.TStringList) is used as Judge gives compile error in such a case.

My code give the same result for test case - but judge gives WA. I have some thought that it mau be related that judge may handle carriage return/line feed for newlines differently than my WinXP/Delphi6 on which I am developing and testing.

Thanks a lot for any hint or advice in advance!!! Especially - it could be so great if someone coudl provide some tests cases against which my program fails to five the correct answer!!!

Code: Select all

program Prg2450(input, output);

{$APPTYPE CONSOLE}

//uses Classes;

var  ch: char;
     piece: String;
     number: String;
     intNumber: SmallInt;
     errorCode: Cardinal;
     posPiece,posNumber: Cardinal;
     inPiece,inNumber: boolean;
     chCode: integer;
     pieces: array[0..1000000] of String;
     positions: array[0..1000000] of Integer;
     currentPieceNo: Integer;
     currentPosition: Integer;
     i: Integer;
     inNewLine: Boolean;
     ch13: char;
     //strList: TStringList;
begin
   {
  AssignFile(Input, 'in02__.txt');
  Reset(Input);
  AssignFile(Output, 'out02__.txt');
  Rewrite(Output);
    }
  setLength(piece,50);
  setLength(number,50);
  currentPieceNo:=1;
  inPiece:=false;
  inNumber:=false;
  posPiece:=1;
  posNumber:=1;
  while not Eof(input) do
  begin
    if Eoln(input) then begin
      // begin
      if inPiece then begin
            for i:=currentPieceNo downto 2 do begin
              positions[i]:=positions[i-1];
            end;
            write(copy(piece,1,posPiece-1));
            pieces[currentPieceNo]:=copy(piece,1,posPiece-1);
            positions[1]:=currentPieceNo;
            currentPieceNo:=currentPieceNo+1;
            inPiece:=false;
            posPiece:=1;
      end;
      if inNumber then begin
            val(copy(number,1,posNumber-1),intNumber,errorCode);
            write(pieces[positions[intNumber]]);
            currentPosition:=positions[intNumber];
            for i:=intNumber downto 2 do begin
              positions[i]:=positions[i-1];
            end;
            positions[1]:=currentPosition;
            inNumber:=false;
            posNumber:=1;
      end;
      // end

      ReadLn(input);
      Read(input,ch);
      chCode:=LongInt(ch);
      //output together with tests - end or not to end
      if chCode=48 then exit;
      if chCode=13 then begin
        ch13:=ch;
        read(ch);
        chCode:=LongInt(ch);
        if chCode=48
          then exit
          else begin
            writeln(output);
            write(ch13);
          end;
      end else begin
        writeln(output);
      end;
    end else begin
      Read(input, ch);
      chCode:=LongInt(ch);
    end;
    case chCode of
      0..31,32,33..47, 58..64, 91..96, 125..255:
        begin
          if inPiece then begin
            for i:=currentPieceNo downto 2 do begin
              positions[i]:=positions[i-1];
            end; //???
            write(copy(piece,1,posPiece-1));
            pieces[currentPieceNo]:=copy(piece,1,posPiece-1);
            positions[1]:=currentPieceNo;
            currentPieceNo:=currentPieceNo+1;
            inPiece:=false;
            posPiece:=1;
          end;
          if inNumber then begin
            val(copy(number,1,posNumber-1),intNumber,errorCode);
            // let 0-lth element is left unused
            write(pieces[positions[intNumber]]);
            currentPosition:=positions[intNumber];
            // moving the current word to the front of list
            for i:=intNumber downto 2 do begin
              positions[i]:=positions[i-1];
            end;
            positions[1]:=currentPosition;
            inNumber:=false;
            posNumber:=1;
          end;
          if (chCode<>10)
            then write(ch)
            else writeLn(output);
        end;
      48..57:
        begin
          if ((not inNumber) and (chCode=48)) then exit;
          number[posNumber]:=ch;
          posNumber:=posNumber+1;
          inNumber:=true
        end;
      else
        begin
          piece[posPiece]:=ch;
          posPiece:=posPiece+1;
          inPiece:=true
        end;
    end; // case
  end; // while
end.
t.tahasin
New poster
Posts: 38
Joined: Tue May 28, 2013 11:21 pm

Re: 245 WA

Post by t.tahasin »

Always WA.
Please help.

Code: Select all

Removed afte acc.

Last edited by t.tahasin on Fri Jul 12, 2013 7:33 am, edited 1 time in total.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 245 WA

Post by brianfry713 »

Input:

Code: Select all

Dear Sally,

   Please, please do it--1 would 4
Mary very, 1 much.  And 4 6
8 everything in 5's power to make
14 pay off for you.

   -- Thank 2 18 10--
0
Output should be:

Code: Select all

Dear Sally,

   Please, please do it--it would please
Mary very, very much.  And Mary would
do everything in Mary's power to make
it pay off for you.

   -- Thank you very power--
Check input and AC output for thousands of problems on uDebug!
t.tahasin
New poster
Posts: 38
Joined: Tue May 28, 2013 11:21 pm

Re: 245 WA

Post by t.tahasin »

@brianfry713: Thank you very much. Now I got accepted. I didn't notice that type of errors first time.
Post Reply

Return to “Volume 2 (200-299)”