## 272 - TEX Quotes

Moderator: Board moderators

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

### Problem 272: TeX Quotes

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

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

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

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]

Jalal
Learning poster
Posts: 65
Joined: Sun Jun 02, 2002 8:41 pm
Contact:
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

Sarmento
New poster
Posts: 15
Joined: Tue Apr 22, 2003 9:50 pm
Location: Lisboa, Portugal

### Problem 272 - Why is it a wrong answer?

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

joao sarmento

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

byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";

try
{
while (lg < maxLg)
{
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;

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:
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!!!

Sarmento
New poster
Posts: 15
Joined: Tue Apr 22, 2003 9:50 pm
Location: Lisboa, Portugal

### Line by line or the whole thing

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

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!!

Sarmento
New poster
Posts: 15
Joined: Tue Apr 22, 2003 9:50 pm
Location: Lisboa, Portugal
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:
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).

Sarmento
New poster
Posts: 15
Joined: Tue Apr 22, 2003 9:50 pm
Location: Lisboa, Portugal
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{
}
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:
Dear Sarmento.

Congratulations.. But I suggest an opinion.
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:
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]

acm_beginner
New poster
Posts: 1
Joined: Tue Oct 14, 2003 5:11 pm

### Problem 272 - Why PE?

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) {
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
try adding a newline character right after the last character;