Discussion of Problem 1345. HTMLI spent 5+ hours trying to understand why I am having WA#9. DON'T use high level classes/methods like Console.Write/StreamReader/StreamWriter. Process input and send to output as raw bytes. I didn't find what was the issue (I am already tired with this shit), but I suppose StreamWriter or StreamReader can mess up with \r \n characters in some cases. Here is code example for WA#9. I use Console.OpenStandardInput(0) as input, and Console.OpenStandardOutput(0) as output. private static void SolveStateMachine(Stream input, Stream output) { using var writer = new StreamWriter(output, Encoding.ASCII, 1024); void Write(char ch) { writer.Write(ch); } void WriteS(string str) { writer.Write(str); }
... } When I change about code to below, I am getting AC: private static void SolveStateMachine(Stream input, Stream output) { void Write(char ch) { byte[] buffer = new byte[1]; buffer[0] = (byte)ch; output.Write(buffer, 0, 1); } void WriteS(string str) { byte[] buffer = Encoding.ASCII.GetBytes(str); output.Write(buffer, 0, buffer.Length); }
... } I got it. Test#9 is about characters with code >= 128. I was using Encoding.ASCII in C#, which causes replacing of all such characters with '?'. If your high level classes use UTF-8 encoding by default, you will also have problems. Input: _123 My first solution output: _<span class=number>123</span> My second solution output: _123 But all passed. So which answer are correct? Task description says: "The correct source of a program in Pascal is given" Your input is definitely not correct source input1 sd5=#124'ashds'#12#154'sah'; input 2 sd5:=#123+#12; output1: sd5=<span class=string>#124</span><span class=string>'ashds'</span><span class=string>#12</span><span class=string>#154</span><span class=string>'sah'</span>; What is test 2? I can't think of anymore tests, it's really killing me. Here is my code, if you can see a mistake, please tell me. #include<stdio.h> #include<string.h> #include<iostream> #include<cctype> #include<memory.h> char keywords[35][64] = {{'a','n','d'},{'a','r','r','a','y'},{'b','e','g','i','n'},{'c','a','s','e'},{'c','l','a','s','s'},{'c','o','n','s','t'},{'d','i','v'},{'d','o'},{'e','l','s','e'},{'e','n','d'},{'f','o','r'},{'f','u','n','c','t','i','o','n'},{'i','f'},{'i','m','p','l','e','m','e','n','t','a','t','i','o','n'},{'i','n','t','e','r','f','a','c','e'},{'m','o','d'},{'n','o','t'},{'o','f'},{'o','r'},{'p','r','o','c','e','d','u','r','e'},{'p','r','o','g','r','a','m'},{'r','e','c','o','r','d'},{'r','e','p','e','a','t'},{'s','h','l'},{'s','h','r'},{'s','t','r','i','n','g'},{'t','h','e','n'},{'t','o'},{'t','y','p','e'},{'u','n','i','t'},{'u','n','t','i','l'},{'u','s','e','s'},{'v','a','r'},{'w','i','t','h'},{'w','h','i','l','e'}}; char s[1024][256]; bool IsKeyword(char y[256]) { int j = 0; bool itis = false; for(int i = 0; i<35; i++) { if(tolower(y[0]) == keywords[i][0] && strlen(y) == strlen(keywords[i])) { for(j = 1; j<strlen(y); j++) { itis = false; if(tolower(y[j]) != keywords[i][j]) break; itis = true; } } if(itis && !((s[i][j+1] >='a' && s[i][j+1]<='z') || (s[i][j+1] >='A' && s[i][j+1] <='Z'))) {memset(y,0,sizeof(y));return true;} } memset(y,0,sizeof(y)); return false; } int main() { int x,y; int strings = 0; int skip = 0; int i = 0; bool was; while(gets(s[i++])!=0) strings++; for(int i = 0;i<strings; i++) { int j = 0; was = false; if(strlen(s[i]) == 0) {printf("\n");continue;} for(j; j<strlen(s[i]);j++) { if(s[i][j]>='0' && s[i][j]<='9' && (s[i][j-1] == '=' || s[i][j-1] == ' ' || j == 0)) { printf("<span class=number>"); int numdots = 0; while((s[i][j] >='0' && s[i][j] <='9') || (s[i][j] == '.' && numdots < 2 && (s[i][j+1] >='0' && s[i][j+1] <='9'))) { printf("%c", s[i][j]); j++; if(s[i][j] == '.') numdots++; } j--; if(j == strlen(s[i])) {if(s[i][j] == '.') printf("</span>.\n"); else printf("</span>.\n"); continue;} else {if(s[i][j] == '.') printf("</span>."); else printf("</span>"); continue;} j--; continue; } if((s[i][j] >='a' && s[i][j]<='z') || (s[i][j] >='A' && s[i][j] <='Z')) { char insert[64]; int len = 0; for(int k = j; k<strlen(s[i]); k++) { if((s[i][k] >='a' && s[i][k]<='z') || (s[i][k] >='A' && s[i][k] <='Z')) { memset(insert,0,sizeof(insert)); len = 0; while((s[i][k] >='a' && s[i][k]<='z') || (s[i][k] >='A' && s[i][k] <='Z') || (s[i][k]>='0' && s[i][k]<='9')) {insert[len++] = s[i][k];k++;} if(IsKeyword(insert)) { printf("<span class=keyword>"); for(j; j<k; j++) printf("%c", s[i][j]); if(len == strlen(s[i])) printf("</span>"); else printf("</span>"); break; } //else //{ // for(j;j<=k;j++) // printf("%c", s[i][j]); //} } else break; } } if(s[i][j] == '{' || (s[i][j] == '/' && s[i][j+1] == '/')) { if((s[i][j] == '{' && skip == 0)) { j++; printf("<span class=comment>{"); for(int k = j; k<strlen(s[i]); k++) { j = k; if(s[i][k] == '{') skip++; if(s[i][k] == '}') { if(skip == 0) {printf("}</span>");j++;break;} else {skip--;} } printf("%c", s[i][k]); if(k == strlen(s[i])-1) {i++; k = -1;printf("\n");} } } else { printf("<span class=comment>"); for(j;j<strlen(s[i]);j++) printf("%c", s[i][j]); j = strlen(s[i]); printf("</span>");}
} if(s[i][j] == '\'' || s[i][j] == '#') { if(s[i][j] == '\'') { j++; printf("<span class=string>\'"); int len; while(s[i][j] !='\'') { len = j; printf("%c", s[i][j]); if(j == strlen(s[i])-1) {i++; j = 0; printf("\n"); continue;} j++; } printf("\'</span>"); continue; } else if(s[i][j+1] >= '0' && s[i][j+1] <='9') { printf("<span class=string>#"); j++; int len; for(int k = j; (s[i][k] >='0' && s[i][k]<='9'); k++) {len = j; printf("%c", s[i][j]);j++;} printf("</span>"); j--; continue; } } if((j!=strlen(s[i]) || j!=strlen(s[i])) && was == false) printf("%c", s[i][j]); if((j >= strlen(s[i])-1))//i != strings-1) {printf("\n");} } } //for(int i = 0;;i++); } Please, help me. My code: #include <iostream> #include <cstring> #include <cctype> using namespace std; const int MaxSourceSize = 100000; const int MaxBuffSize = 1000; const int nKeyWord = 36; const char *comment_start = "<span class=comment>"; const char *keyword_start = "<span class=keyword>"; const char *string_start = "<span class=string>"; const char *number_start = "<span class=number>"; const char *end_teg = "</span>"; const char KeyWords[nKeyWord][sizeof( "implementation" )] = { "and", "array", "begin", "case", "class", "const", "div", "do", "else", "end", "for", "function", "if", "implementation", "interface", "mod", "not", "of", "or", "procedure", "program", "record", "repeat", "shl", "shr", "string", "then", "to", "type", "unit", "until", "uses", "var", "with", "while" }; int main() { register int i, j, g; char source[MaxSourceSize]; cin.get ( source, MaxSourceSize, 0 ); cin.ignore(); for ( i = 0; source[i]; i++ ) { if ( isalpha ( source[i] ) || source[i] == '_' ) { char buff[MaxBuffSize]; bool itKeyWord = false; for ( g = i, j = 0; isalnum ( source[g] ) || source[g] == '_'; g++, j++ ) buff[j] = tolower ( source[g] ); buff[j] = 0; for ( j = 0; j < nKeyWord; j++ ) if ( !strcmp ( buff, KeyWords[j] ) ) { itKeyWord = true; break; } if ( itKeyWord ) cout << keyword_start; for ( i; isalnum ( source[i] ) || source[i] == '_'; i++ ) cout << source[i]; i--; if ( itKeyWord ) cout << end_teg; } else { if ( isdigit ( source[i] ) ) { cout << number_start; for ( i; isdigit ( source[i] ) || ( source[i] == '.' && source[i+1] != '.' ); i++ ) cout << source[i]; i--; cout << end_teg; } else { switch ( source[i] ) { case '{': { int in = 0; cout << comment_start; for ( ; ; i++ ) { if ( source[i] == '{' ) in++; else if ( source[i] == '}' ) { in--; if ( !in ) {cout << source[i];break;} } cout << source[i]; } cout << end_teg; //i--; };break; case '\\': { if ( source[i+1] == '\\' ) { cout << comment_start; for ( i; source[i] != '\n'; i++ ) cout << source[i]; cout << end_teg; i--; } else { cout << '\\'; } };break; case '\'': { cout << string_start; cout << source[i++]; for ( i; source[i] != '\''; i++ ) cout << source[i]; cout << source[i]; cout << end_teg; };break; case '#': { cout << string_start; i++; cout << "#"; for ( i; isdigit ( source[i] ) || ( source[i] == '.' && isdigit ( source[i+1] ) ); i++ ) cout << source[i]; cout << end_teg; i--; };break; default: { cout << source[i]; };break; } } } } return NULL; } If you have WA#7 check that your program works correct with strings with symbols "{" and "//" try this test a:array[1..5] of integer; my AC program out: a:<span class=keyword>array</span>[<span class=number>1</span>..<span class=numb er>5</span>] <span class=keyword>of</span> integer; Got AC with a 34-lines Java program. 5 lines for the RE: "('[^']*'|#\\d+)|"+ // 1: str "(\\d+(?:\\.\\d+)?)|"+ // 2: num "(\\{[^}]*\\}|//[^\\r\\n]*)|"+ // 3: comment "([a-z_][a-z_0-9]*)|"+ // 4: ID "(.)" // 5: anything else 5 lines of kw: {"and", "array", "begin", "case", "class", "const", "div", "do", "else", "end", "for", "function", "if", "implementation", "interface", "mod", "not", "of", "or", "procedure", "program", "record", "repeat", "shl", "shr", "string", "then", "to", "type", "unit", "until", "uses", "var", "while", "with"}; and 10 copy/pasted lines of the main loop: while in.findWithinHorizon is not null, check which group matched and print accordingly Pitfalls: The list of the keywords in the statement is not sorted and thus binarySearch in it fails. The statement about "//..." comments is simply wrong: line feed is \n, in reality test 6 uses carriage-return (\r). Got AC 0.001 sec from the first effort :) I saw many requests for clarification here, but it's easier to describe grammar in terms of behavior. Go sequentially through input data. 1. If you meet '{' - everything until closest corresponding '}' is a single comment, no matter what's inside. '}' corresponds to '{' if all {,} characters between them form valid bracket sequence. 2. If you meet '//' - everything until '\n' is a single comment, no matter what's inside. 3. If you meet ' - everything until next ' is a string, no matter what's inside. 3. If you meet '#' followed by a digit - everything until first non-digit is a string. 4. If you meet a digit, then everything until first non-digit is a number. If first non-digit character is '.' AND it is followed by another digit, then this post-dot digit sequence is appended to the number as well as the dot itself. I.e. "1.23" is a single number, but "123.a" is number '123' followed by '.', followed by identifier 'a'. 5. If you meet underscore or letter, everything until first non-digit-non-underscore-non-letter is an identifier. It may be a keyword. 6. Output all other characters as is. Edited by author 17.08.2008 01:02 These statements should be stated in problem description. They are all right and to recreate them I tried 50 submissions. In western sytes it is a rule punctual and clear descriptions all conditions but in our country often used believeness in super talantedness of us. Edited by author 10.01.2009 20:26 Really helpful! Link to this post should be added to the statement :-) <PRE> and a line </PRE> replace "and a line" with it's translation (и строку) :-) 1. #123ident; 2. #123'str'; 3. #123#124; 4. # (without following digits and not as part of {comment} and 'string') 5. 222.not_a_digit; 6. 222.222.222; 7. 222#123; 8. 222'str'; 9. ident#123; 10. ident'str'; I didn't consider these expressions in my program and got WA4. There can be only №1, №2, №3 and №5 (only as [1..11]). Edited by author 17.05.2009 21:31 If you have WA on test #6 pay attention on the: slashes are the part of the comment while line feed is not On this test unit Unit1; interface implementation end. right answer is <span class=keyword>unit</span> Unit1; <span class=keyword> interface implementation end</span>. or <span class=keyword>unit</span> Unit1; <span class=keyword>interface</span> <span class=keyword>implementation</span> <span class=keyword>end</span>. can {} comments be nested? { bla bla bla { comment on bla bla bla } } How should it be outputed: <c>{ bla bla bla { comment on bla bla bla }</c> } or <c>{ bla bla bla { comment on bla bla bla } }</c> In PASCAL it is normal for string to have an apostrophe in it, but it should be written as double apostrophe. 'It''s a book' should be highlighted (here) as <s>'It'</s><s>'s a book'</s> or <s>'It''s a book'</s> as logic says it should be done? It is normal practice to write #$A7 to indicate that character is 0xA7, what should be done? This should not be highlighted? If I have fucked up everybody with my questions, excuse me =(. But what should be done with negative numbers? Hey,hey,hey You are using taboo words!!!!! Ooooooooooooooopz! Here are answers: Nested comments are not present! 'It''s a book' should became <s>'It'</s><s>'s a book'</s> There are no hexadecimal characters! (e.g. #$A7) No negative numbers! Question is'n clear after all! What answer must be for test {}{} Acording problem description this string may be understood by two ways: 1. Two sequental commentries; 2. One commentry with inner part }{ Thus grammar uncertain and description not right. I got Ac 0.031 Finally and explanation of this and another situations given by Kosman on thif forum. Here is important that Pascal language is used. Edited by author 10.01.2009 20:24 Edited by author 10.01.2009 20:25 Who understand the task? I mean difinition of "number": A number begins with a digit and contains only digits and possibly one point “.”, followed by one or more digits. As in the case of a string a number is the maximal by inclusion sequence of symbols that satisfies the given above requirement. I wrote a program: program Example1; var a2,a3:byte; n,t:integer; begin read(n); t:=n; a2:=0; while n mod 2 = 0 do begin n:=n div 2; inc(a2); end; a3:=0; while n mod 3 = 0 do begin n:=n div 3; inc(a3); end; write(n,'=2^',a2,'*3^',a3,'*',n); {this program writes a number in n=(2^k)*(3^l)*m form} {10=2^1*3^0*5} {36=2^2*3^2*1} end. Could you write HTML-answer? <span class=keyword>program</span> Example1; <span class=keyword>var</span> a2,a3:byte; n,t:integer; <span class=keyword>begin</span> read(n); t:=n; a2:=<span class=number>0</span>; <span class=keyword>while</span> n <span class=keyword>mod</span> <span class=number>2</span> = <span class=number>0</span> <span class=keyword>do</span> <span class=keyword>begin</span> n:=n <span class=keyword>div</span> <span class=number>2</span>; inc(a2); <span class=keyword>end</span>; a3:=<span class=number>0</span>; <span class=keyword>while</span> n <span class=keyword>mod</span> <span class=number>3</span> = <span class=number>0</span> <span class=keyword>do</span> <span class=keyword>begin</span> n:=n <span class=keyword>div</span> <span class=number>3</span>; inc(a3); <span class=keyword>end</span>; write(n,<span class=string>'=2^'</span>,a2,<span class=string>'*3^'</span>,a3,<span class=string>'*'</span>,n); <span class=comment>{this program writes a number in n=(2^k)*(3^l)*m form}</span> <span class=comment>{10=2^1*3^0*5}</span> <span class=comment>{36=2^2*3^2*1}</span> <span class=keyword>end</span>. why I have Crash??? program Project1; {$APPTYPE CONSOLE} {$H+} const Key: array [1..35] of string = ('AND', 'ARRAY', 'BEGIN' , 'CASE', 'CLASS', 'CONST', 'DIV', 'DO', 'ELSE', 'END', 'FOR' , 'FUNCTION', 'IF', 'IMPLEMENTATION', 'INTERFACE', 'MOD' , 'NOT', 'OF', 'OR', 'PROCEDURE', 'PROGRAM', 'RECORD' , 'REPEAT', 'SHL', 'SHR', 'STRING', 'THEN', 'TO', 'TYPE' , 'UNIT', 'UNTIL', 'USES', 'VAR', 'WITH', 'WHILE'); var S, St: String; Chi, I, J, T: LongInt; Num, Tochka, Comment, Stri: Boolean; Ch: Char; function PosAfter(Sub, S: String; C: LongInt): LongInt; var T: LongInt; begin delete(S, 1, C); T:=Pos(Sub, S); if T<>0 then PosAfter:=C+T else PosAfter:=0; end; Function UpperCase(S: String): String; const ChUp: String = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; ChDown: String = 'abcdefghijklmnopqrstuvwxyz'; var I, T: LongInt; Te: String; begin Te:=S; for I := 1 to Length(S) do begin T:=pos(Te[I], ChDown); if T<>0 then begin Te[I]:=ChUp[T]; end; end; UpperCase:=Te; end; begin while not seekeof do begin readln(S); I:=0; if Comment then begin T:=Pos('}', S); if T>0 then begin Comment:=False; insert('</span>', S, T+1); I:=T+length('</span>'); end else begin writeln(S); Continue; end; end; if Stri then begin T:=Pos('''', S); if T>0 then begin Comment:=False; insert('</span>', S, T+1); I:=T+length('</span>'); end else begin writeln(S); Continue; end; end; while I<=Length(S) do begin Inc(I); if (Num) then begin if (S[I] in ['0'..'9', '.']) then begin if (S[I]='.')and(not Tochka) then begin Tochka:=True; continue; end; if S[I]='.' then begin if S[I-1]='.' then begin insert('</span>', S, I-1); inc(I, length('</span>')); end else begin insert('</span>', S, I); inc(I, length('</span>')); end; Tochka:=False; Num:=False; end; continue; end; if S[I-1]='.' then begin insert('</span>', S, I-1); inc(I, length('</span>')); end else begin insert('</span>', S, I); inc(I, length('</span>')); end; Tochka:=False; Num:=False; end; if (Chi>0) then begin if (Chi=1)and(S[I] in ['0'..'9']) then begin insert('<span class=string>', S, I-1); inc(I, length('<span class=string>')); Chi:=2; continue; end; if (not (S[I] in ['0'..'9'])) then begin if Chi=2 then begin insert('</span>', S, I); inc(I, length('</span>')); end; Chi:=0; end; end; if S[I]='{' then begin insert('<span class=comment>', S, I); inc(I, Length('<span class=comment>')); T:=PosAfter('}', S, I); if T>0 then begin insert('</span>', S, T+1); I:=T+length('</span>'); continue; end else begin Comment:=True; break; end; end; if S[I]='''' then begin insert('<span class=string>', S, I); inc(I, Length('<span class=string>')); T:=PosAfter('''', S, I); if T>0 then begin insert('</span>', S, T+1); I:=T+length('</span>'); Continue; end else begin Stri:=True; Break; end; end; if S[I]='#' then begin Chi:=1; Continue; end; if (copy(S, I, 2)='//') then begin insert('<span class=comment>', S, I); S:=S+'</span>'; Break; end; if (Chi=0)and(S[I] in ['0'..'9'])and(not Num) then begin if not Num then begin insert('<span class=number>', S, I); inc(I, length('<span class=number>')); Num:= True; end; continue; end; for J := 1 to Length(Key) do begin St:=copy(S, I, length(Key[J])); if (UpperCase(St)=Key[J])and((I=1)or (not (S[I-1] in ['a'..'z', 'A'..'Z'])))and ((I+length(Key[J])=length(S))or (not(S[length(Key[J])+I] in ['a'..'z', 'A'..'Z']))) then begin insert('</span>', S, I+length(Key[J])); insert('<span class=keyword>', S, I); inc(I, Length('<span class=keyword>')+length(Key[J])+length('</span>')); break; end; end; end; writeln(S); end; end. URAAAA I have AC!!! Crash was because I>length(S)!!!!! //WA.. #include <stdio.h> #include <string.h> #define maxn 100100 #define lcase(a) (a>='A'&&a<='Z'?a+32:a) char s[maxn],ch; long sp; char strs[35][30] = { "and", "array", "begin", "case", "class", "const", "div", "do", "else", "end", "for", "function", "if", "implementation", "interface", "mod", "not", "of", "or", "procedure", "program", "record", "repeat", "shl", "shr", "string", "then", "to", "type", "unit", "until", "uses", "var", "with", "while" }; char tmp[maxn]; char tmp2[maxn]; long bs () { long l, m, r, t;
l = 0; r = 34;
while (l<=r) { m = ((l+r)>>1); t = strcmp(strs[m],tmp2); if (t == 0) return m; if (t > 0) r = m - 1; else l = m + 1; } return -1; } void comment (long l, long r) {
printf("<span class=comment>"); for (;l<=r;l++) printf("%c",s[l]); printf("</span>"); } void string (long l, long r) {
printf("<span class=string>"); for (;l<=r;l++) printf("%c",s[l]); printf("</span>"); } void number (long l, long r) {
printf("<span class=number>"); for (;l<=r;l++) printf("%c",s[l]); printf("</span>"); } void identifier (long l, long r) { long i;
i = 0; for (;l<=r;l++) { tmp2[i] = lcase(s[l]); tmp[i ++] = s[l]; } tmp2[i] = 0; tmp[i] = 0; i = bs(); if (i > -1) printf("<span class=keyword>%s</span>",tmp); else printf("%s",tmp); } int main () { long i,j,t;
sp = 0; while (scanf("%c",&ch) > 0) s[sp++] = ch; s[sp] = 0; for (i=0;i<sp;) { if (s[i] == '{') { //Comment Type 1 j = i + 1; while (s[j] != '}') j ++; comment(i,j); i = j + 1; } else if (s[i] == '/' && s[i+1] == '/') { //Comment Type 2 j = i + 1; while (s[j] != '\n') j ++; comment(i,j - 1); i = j; } else if (s[i] == '\'') { j = i + 1; while (s[j] != '\'') j ++; string(i,j); i = j + 1; } else if (s[i] == '#' && s[i+1]>='0' && s[i+1]<='9') { j = i + 2; while (s[j] >= '0' && s[j] <= '9') j ++; string(i,j - 1); i = j; } else if (s[i]>='0' && s[i]<='9') { // while (1); t = 0; j = i + 1; while (s[j] >= '0' && s[j] <= '9' || s[j] == '.' && s[j+1] >= '0' && s[j+1] <= '9' && !t) { if (s[j] == '.') t ++; j ++; } number(i,j - 1); i = j; } else if (s[i] >= 'A' && s[i] <= 'Z' || s[i] >= 'a' && s[i] <= 'z' || s[i] == '_') { j = i + 1; while (s[j] >= 'A' && s[j] <= 'Z' || s[j] >= 'a' && s[j] <= 'z' || s[j] >= '0' && s[j] <= '9' || s[j] == '_') j ++; identifier(i,j - 1); i = j; } /* else if (s[i] == '.' && s[i+1] >= '0' && s[i+1] <= '9') { j = i + 1; while (s[j] >= '0' && s[j] <= '9') j ++; number(i,j - 1); i = j; } */ else //Others printf("%c",s[i++]); }
return 0; } Does the famous Anhui programmar and IOI contestant YangYi have some problems? Solve it yourself! input1 begin end. output1 <span class=keyword>begin</span> <span class=keyword>end</span>. or <span class=keyword>begin end</span>. input2 'as as' output2 <span class=string>'as</span> as'</span> or <span class=string>'as</span> as<span class=string>'</span> input3 1. outpu3 <span class=number>1</span>. or <span class=number>1.</span> Edited by author 23.01.2007 21:12 1 <span class=keyword>begin</span> <span class=keyword>end</span>. 2 <span class=string>'as as'</span> 3 <span class=number>1</span>. I tried all the tests you gave in the board before, and I'm all right!! But I still got WA#5. :( Can you help me??? Please give me some tests or hints. OR you can fix my code. My code: const span:array[1..4] of string=('comment>','keyword>','string>','number>'); key:array[1..35] of string=('and','array','begin','case','class','const', 'div','do','else','end','for','function','if','implementation','interface', 'mod','not','of','or','procedure','program','record','repeat','shl','shr', 'string','then','to','type','unit','until','uses','var','with','while'); sym=',./?<>\|=+-~!@#$%^&*()0987654321`{}[];:" '#39; sp='<span class='; se='</span>'; var st,sst:string; ch:char; i,hp:integer; comm,f:boolean; function getnewword(var st:string):string; var i:integer; begin if pos(st[1],sym)<>0 then begin getnewword:=st[1]; delete(st,1,1); exit; end; if comm then begin i:=pos('}',st); if i<>0 then begin write(copy(st,1,i),se); delete(st,1,i); end else begin write(st); st:=''; end; exit(''); end; for i:=1 to length(st) do if (pos(st[i],sym)<>0) then begin getnewword:=copy(st,1,i-1); delete(st,1,i-1); exit; end; getnewword:=st; st:=''; end; procedure putspan(i:integer); begin write(sp,span[i]); end; procedure putres(st:string); begin putspan(2); write(st,se); end; function chkres(sst:string):boolean; var i:integer; st:string; begin st:=lowercase(sst); for i:=1 to 35 do if st=key[i] then exit(true); exit(false); end; procedure putsym(ch:char); procedure putnum(ch:char); begin putspan(4); write(ch); i:=1; repeat if st[i] in ['0'..'9'] then inc(i) else if st[i]='.' then begin f:=not f; if not f then break else inc(i); end else break; until 1=8; if st[i-1]='.' then dec(i); write(copy(st,1,i-1),se); delete(st,1,i-1); end; procedure putstr; begin putspan(3); write(#39); i:=pos(#39,st); write(copy(st,1,i),se); delete(st,1,i); end; procedure putchr; var i:integer; begin putspan(3); write('#'); i:=1; f:=false; while st[i] in ['0'..'9'] do inc(i); write(copy(st,1,i-1),se); delete(st,1,i-1); end; procedure putcom(ch:char); begin putspan(1); write(ch); if ch='/' then begin write(st,se); st:=''; end else begin i:=pos('}',st); if i=0 then begin write(st); st:=''; comm:=true; end else begin write(copy(st,1,i),se); delete(st,1,i); end; end; end; begin case ch of '0'..'9':putnum(ch); #39:putstr; '#':putchr; '{':putcom(ch); '/':if st[1]='/' then putcom(ch) else write(ch) else write(ch); end; end; procedure main; begin { assign(input,'1345.in'); reset(input); assign(output,'1345.out'); rewrite(output);} while not eof do begin readln(st); while st<>'' do begin sst:=getnewword(st); if comm then begin if sst='}' then begin write('}',se); comm:=false; end else begin hp:=pos('}',st); if hp=0 then begin write(st); st:=''; end else begin write(copy(st,1,hp),se); delete(st,1,hp); comm:=false; end; end; continue; end else if pos(sst[1],sym)<>0 then putsym(sst[1]) else begin if chkres(sst) then putres(sst) else write(sst); end; end; writeln; end; { close(input); close(output);} end; begin comm:=false; main; end. Edited by author 24.12.2006 11:44 Who can help me? WA #5. Give me some tests, plz. Stupid mistake: 1. right answer is <span class=number>1</span>. |
|