272 - TEX Quotes

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

Post Reply
Zhichao Li
New poster
Posts: 2
Joined: Sun May 05, 2002 10:12 am
Location: Guangzhou, China
Contact:

Problem 272: TeX Quotes

Post by Zhichao Li »

Could you tell my why I got wrong answers?

[pascal] {$E+,I-,N+,Q-,R-,S-}

var
c:char;
ch:char;

begin
c:='`';
while not(eof(input)) do
begin
read(ch);

if ch='"' then
begin
write(c,c);
if c='`' then c:=''''
else c:='`';
end
else write(ch);
end;
end.[/pascal]
ec3_limz
Learning poster
Posts: 79
Joined: Thu May 23, 2002 3:30 pm
Location: Singapore

i think i understand why

Post by ec3_limz »

Instead of outputting 1 backquote "`", you should output 2: "``".

That is what I see so far. Try it first.

Good luck!
jiangwen
New poster
Posts: 2
Joined: Sat Oct 05, 2002 3:03 am

it returns wrong answer?!!!(No272)

Post by jiangwen »

my program returns exactly the right answer in my view:

"To be or not to be," quoth the Bard, "that
is the question".
The programming contestant replied: "I must disagree.
To `C' or not to `C', that is The Question!"
^Z
``To be or not to be,'' quoth the Bard, ``that
is the question''.
The programming contestant replied: ``I must disagree.
To `C' or not to `C', that is The Question!''
Press any key to continue


but the judge-system said it returned wrong answer!
i really can't find out where 's the mistake.

and that's my source file in C++:

[cpp]
#include "iostream.h"
#include "stdio.h"
#include "malloc.h"
void main()
{
struct charlist
{
char alpha;
charlist * next;
};
char ch;
bool lq;
struct charlist* first,*p,*pre;
first=(struct charlist*)malloc(sizeof(charlist));
first->alpha='a';
pre=first;
ch=getchar();
while (true)
{
if (ch=='"')
{
if (lq)
{
ch='`';
lq=!lq;
p=(charlist*)malloc(sizeof(charlist));
p->alpha=ch;
pre->next=p;
p->next=NULL;
pre=p;
p=(charlist*)malloc(sizeof(charlist));
p->alpha=ch;
pre->next=p;
p->next=NULL;
pre=p;
}
else
{
ch='\'';
lq=!lq;
p=(charlist*)malloc(sizeof(charlist));
p->alpha=ch;
pre->next=p;
p->next=NULL;
pre=p;
p=(charlist*)malloc(sizeof(charlist));
p->alpha=ch;
pre->next=p;
p->next=NULL;
pre=p;
}
}
else
{
p=(charlist*)malloc(sizeof(charlist));
p->alpha=ch;
pre->next=p;
p->next=NULL;
pre=p;
}
if (ch==EOF) break;
ch=getchar();
}
p=first->next;
while(p!=NULL)
{
putchar(p->alpha);
p=p->next;
}
pre=first;
p=pre->next;
while (pre!=NULL)
{
p=pre->next;
free(pre);
pre=p;
}
}
[/cpp]


So,please help me,tell me where's the problem?thank you very very very much![/cpp]
Jalal
Learning poster
Posts: 65
Joined: Sun Jun 02, 2002 8:41 pm
Location: BANGLADESH
Contact:

Post by Jalal »

I dont know why u deals with this components like '/' in this problem.
Its so easy. Just print ` ` for the first " and for the secon print the "
thats all. Just a simple for loop which will count even and odd numbered "
in the problem and for odd it will print ` ` and for the even it will print
"
thats all.
not more than this :wink:
User avatar
Sarmento
New poster
Posts: 15
Joined: Tue Apr 22, 2003 9:50 pm
Location: Lisboa, Portugal

Problem 272 - Why is it a wrong answer?

Post by Sarmento »

Can anyone tell me what's the problem with this?????
It's converting all the sentences perfectly.....

Thanks in advance,

joao sarmento

[java]
// @JUDGE_ID: 30701AE 272 Java
import java.io.*;
class Main{

static String ReadLn (int maxLg){
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";

try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n'))
break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}

if ((car < 0) || (lg == 1)) return (null); // eof
return (new String (lin, 0, lg));
}

public static void main (String args[]){
Main myWork = new Main(); // create a dinamic instance
myWork.Begin(); // the true entry point
}

void Begin(){
int l;
boolean closing_quote;
String str;

while((str = Main.ReadLn(255)) != null){
l = str.length();
closing_quote = false;
for(int i = 0; i < l;i++){
if (str.charAt(i) == '"'){
if (closing_quote)
str = str.substring(0,i) + "''" + str.substring(i+1);
else
str = str.substring(0,i) + "``" + str.substring(i+1);
closing_quote = !closing_quote;
}
l = str.length();
}
System.out.println(str);
}

}
}[/java]
-----------------
Jo
soyoja
Experienced poster
Posts: 106
Joined: Sun Feb 17, 2002 2:00 am
Location: Seoul, South Korea
Contact:

Post by soyoja »

In fact, I don't know about Java code...

But in your program, you read an input string line by line.

I think it's not a good method.

Fix your code that reading one character at a time and converting

character each time. I don't know what type of input data will come in.

So I think using character buffer is so dangerous.

P.S ) It's one of the simplest and easiest problem in valladolod problem set. Cheer up~ : )
I love Problem Solving!!! :) :) :)
User avatar
Sarmento
New poster
Posts: 15
Joined: Tue Apr 22, 2003 9:50 pm
Location: Lisboa, Portugal

Line by line or the whole thing

Post by Sarmento »

In this problem you should fetch sentence by sentence, transform and print it or fetch them all and just then transform and print them?
-----------------
Jo
Nick
Learning poster
Posts: 53
Joined: Sun Jan 12, 2003 4:49 am

about the input

Post by Nick »

1. There will be one input and in multiple lines.
2. So one sentence can be in two or more lines.
3. And one line can be more than 255 characters.
4. You always set closing_quote to false each line of input
This means your code will output two "``" s on a parsed sentence

Okay, good luck Sarmento!!
User avatar
Sarmento
New poster
Posts: 15
Joined: Tue Apr 22, 2003 9:50 pm
Location: Lisboa, Portugal

Post by Sarmento »

How do I check for the EOF in java? In C you have '\0', but how about in Java? Is the "\n" too?
-----------------
Jo
turuthok
Experienced poster
Posts: 193
Joined: Thu Sep 19, 2002 6:39 am
Location: Indonesia
Contact:

Post by turuthok »

When you use System.in.read() and it returns a negative value ... assume it's EOF ...

By the way, '\n' is newline, ... it's not EOF.

-turuthok-
The fear of the LORD is the beginning of knowledge (Proverbs 1:7).
User avatar
Sarmento
New poster
Posts: 15
Joined: Tue Apr 22, 2003 9:50 pm
Location: Lisboa, Portugal

Post by Sarmento »

I decided to try another aproach (more C like), and it was Acc(P.E.).

Thanks

joao sarmento

[java]
import java.io.*;
class Main{

public static void main (String args[]){
Main myWork = new Main(); // create a dinamic instance
myWork.Begin(); // the true entry point
}

void Begin(){
int c = 0;
boolean closing_quote = false;
String opening = "``";
String closing = "\'\'";
while (c>=0){
try{
c = System.in.read();
}
catch(IOException e){
System.out.println(e.getMessage());
System.out.println("Fatal error. Ending Program.");
System.exit(0);
}
if (c == '\"'){
System.out.print(closing_quote?closing:opening);
closing_quote = !closing_quote;
}
else
System.out.print((char) c);
}
}
}[/java]
-----------------
Jo
soyoja
Experienced poster
Posts: 106
Joined: Sun Feb 17, 2002 2:00 am
Location: Seoul, South Korea
Contact:

Post by soyoja »

Dear Sarmento.

Congratulations.. But I suggest an opinion.
Please do not post your accepted code...
I think it's no benefit to us ( Especially someone who not solved this
problem yet ). I believe you understand my words.
Thanks.
Alexander Denisjuk
New poster
Posts: 35
Joined: Sat Jan 05, 2002 2:00 am
Contact:

Post by Alexander Denisjuk »

By the way, if you wish something more complicated, consider my variant of main (I skip details):
[cpp]
int main(){
automat A;
while(cin>>A>>cout);
return 0;
}
[/cpp]
:wink:
acm_beginner
New poster
Posts: 1
Joined: Tue Oct 14, 2003 5:11 pm

Problem 272 - Why PE?

Post by acm_beginner »

I know this is a very extremely easy problem... I get Accepted but PE. I suppose that reading character by character and only change " to `` or '' would be enough.
Someone can tell me what's wrong with this code?

[cpp]
#include <iostream>

using namespace std;

int main(void) {
char c;
bool flag;

flag=true;
while (cin) {
cin.read(&c,1);
if (c=='"') {
if(flag) {
cout << "``";
} else {
cout << "''";
}
flag=!flag;
} else
cout << c;
}
return 0;
}
[/cpp]
Joseph Kurniawan
Experienced poster
Posts: 136
Joined: Tue Apr 01, 2003 6:59 am
Location: Jakarta, Indonesia

Post by Joseph Kurniawan »

try adding a newline character right after the last character;
instead of :
[cpp]
return 0;
[/cpp]

try
[cpp]
cout<<endl;
return 0;
[/cpp]

Hope it helps!! :wink: :wink: :wink:
There are 3 things one need to be successful : motivation, ability and chance. And if you're a believer, make it four : God's will.
Post Reply

Return to “Volume 2 (200-299)”