string s = ""; int c; while ((c = getchar()) != EOF) s.push_back((char)c); Correct read input C++ Try your generated //CPP code with MSVC to check if there's any compile error. If you don't have MSVC installed, just submit the *generated* code. Even after reading all the posts here. Please help. 1. assert program is shorter than input 2. assert no long lines in source code 3. split string literal into vectors 4. assert no '\r' but '\n' after reading input 5. output with std::cout and '\n' 6. many tests and asserts ===this is an example of archive=== might be overkill to use Huffman and base64 shorten variable names in actual archive =================================== //CPP #include <iostream> #include <string> #include <unordered_map> #include <vector> int length = 37; std::vector<std::string> encoded{ "7VKh", "7lg=" }; std::unordered_map<std::string, char> table{ {"00", 'o'}, {"01", 'l'}, {"100", 'w'}, {"1010", ' '}, {"1011", '!'}, {"1100", 'd'}, {"1101", 'e'}, {"1110", 'h'}, {"1111", 'r'} }; struct HuffmanTree { char ch; struct HuffmanTree* left; struct HuffmanTree* right; ~HuffmanTree() { delete left; delete right; } }; void build_tree(HuffmanTree* root, std::string code, char ch) { auto node{root}; for (auto x : code) if (x == '1') { if (node->right == nullptr) node->right = new HuffmanTree(); node = node->right; } else { if (node->left == nullptr) node->left = new HuffmanTree(); node = node->left; } node->ch = ch; } char lookup(std::vector<bool>::iterator& it, const HuffmanTree* tree) { auto node{tree}; while (true) { if (node->left == nullptr) break; if (*it) node = node->right; else node = node->left; it++; } return node->ch; } void huffman_decode(std::vector<bool> bits, const HuffmanTree* tree) { auto it{bits.begin()}; while (it != bits.end()) std::cout << lookup(it, tree); } int six(char c) { if (c >= 'A' and c <= 'Z') return c-'A'; if (c >= 'a' and c <= 'z') return c-'a' + 26; if (c >= '0' and c <= '9') return c-'0' + 52; if (c == '+') return 62; if (c == '/') return 63; return 0; } std::vector<std::string> base64_decode(std::vector<std::string> encoded) { std::vector<std::string> decoded; for (auto s : encoded) { std::string t; auto i{0u}; while (i < s.size()) { auto p0{six(s[i])}; auto p1{six(s[i+1])}; auto p2{six(s[i+2])}; auto p3{six(s[i+3])}; t.push_back((p0<<2) + ((p1&0x30)>>4)); if (s[i+2] != '=') t.push_back(((p1&0x0f)<<4) + ((p2&0x3c)>>2)); if (s[i+3] != '=') t.push_back(((p2&0x03)<<6) + p3); i += 4; } decoded.push_back(t); } return decoded; } std::vector<bool> string_to_bits(int n, std::vector<std::string> v) { std::vector<bool> bits; for (auto s : v) for (auto byte : s) for (auto i{7}; i >= 0; i--) bits.push_back((byte >> i) & 1); while (int(bits.size()) > n) bits.pop_back(); return bits; } int main() { auto decoded{base64_decode(encoded)}; auto bits{string_to_bits(length, decoded)}; auto root{new HuffmanTree()}; for (auto [code, ch] : table) build_tree(root, code, ch); huffman_decode(bits, root); } For a much simpler algorithm, the c++ program got accepted easily. However, the python version still got WA#1. Probably some I/O issue for the python interpreter. Maybe newlines are handled differently for python "print(s, end='')" and c++ "std::cout << s". Edited by author 04.02.2022 08:15 0. I wanted to implement universal approach with Huffman coding + compressing binary code to 64 base code. Not just eng text property (I was upset, because for so interesting task so stupid solution works). 1. I'm Java developer, and I afraid that Java consume too much memory even for input. So I have to rewrite all to cpp. 2. Thinks that costs me a lot of time. Input in cpp. (If you can explain what is wrong. please do ) WRONG inputData = ""; std::string line = ""; while (std::getline(std::cin, line)) { inputData += line; //inputData.push_back('\r');//todo maybe we need \r\n inputData.push_back('\n'); } CORRECT inputData = ""; int c; while ((c = getchar()) != EOF) inputData.push_back((char)c); 3. Escaping characters to produce cpp code: std::string escape(char ch) { switch (ch) { case '\'': return "\\'"; case '\"': return "\\\""; case '\\': return "\\\\"; case '\b': return "\\b"; case '\f': return "\\f"; case '\n': return "\\n"; case '\r': return "\\r"; case '\t': return "\\t"; default: return {ch}; } } I finally got AC. So I wish you good luck with this problem and thank to author so interesting problem. My solutions in the two languages produce the same output on my tests. However, the Go version gets WA1. Please fix this. #include<iostream> #include <stdio.h> #include <stdlib.h> #include <string> using namespace std; int main() { long l; string a,b; cin>>a; l=a.length(); if((l>=10)&&(l<=189)) { b=a.substr(l-2,2);
} else if((l>=190)&&(l<=2700)) b=a.substr(l-3,3); else if((l<=2701)&&(l<=36000)) b=a.substr(l-4,4); else b=a.substr(l-5,5); cout<<"//CPP"<<endl; cout<<"#include<iostream.h>"<<endl; cout<<"int main()"<<endl; int i = atoi(b.c_str()); cout<<"{for(int i=1;i<"<<i+1<<";i++)cout<<i;return 0;}"; return 0; } I suppose my error is either in the procedure of scanning/printing. My program receives input and prints output using file pointers. FILE *fi,*fo; fi = stdin; fo = stdout; And the program reads/writes by fscanf/fprintf. If you have any knowledge concerning the input/output for this problem, please comment. Edited by author 28.03.2013 07:06 Finally AC, Edited by author 28.03.2013 07:06 If you're outputting C or C++ codes, then the followings might help: 1. Each line in your program can't be too long. (I chose to add a line break every 950 characters.) Having lines with length exceeding the compiler's limit will make you WA on test 1. 2. Lengths of any string constant (or array?) should be shorter (no longer?) than 65536 bytes. This is yet another unreasonable compiler limitation. Going over the length limit will make you WA on test 3. I think the statements of this problem must have a full description of compilers used to compile outputs of solutions, otherwise it's nearly impossible to find out what does "WA1" mean. Not to mention that it's just plain wrong to use some magical non-standard-compatible compiler, that accepts "<iostream.h>" and "cout<<i" without using "std" namespace and so on. Please add to statements hints about code formatting your compiler accepts, don't turn this wonderful problem to boring technical problem "Archiver or Satisfy Our Compiler" You can generate archive-program for a simple text on your machine and submit it to see whether it is compiled on the server. Than you can rewrite your archive-program to compare the output with expected text intead of printing it to console. That's totally not the point. The program submitted might use different compiler from the archive-program being tested. It's also not clear exactly which symbols may appeared in the input. On my dev-c++ (gcc) and on the old Timus compiler, they accept such code: char a[]="xxx...xxx"; (there are up to 200000 'x') But after lots of WA1's I tried to run it on VC2008, it won't compile! It said that sth like "string is to long" (Sorry my VC isn't in English so I can't tell exactly) So to solve this problem now, you have to think of a more complicated way... And it took me much effort to escape that terrible WA1.. Exactly! In MSVC2008 we have the following restrictions on strings in C++: Length of one string unit must not be more than 65535 symbols (with ending \0 it is 65536). If you have greater string, you must devide it into several sub-strings, like I did in my solution: const char* psz[] = { "string one", "string two", ... }; And you cant write in one line more than ~2000 charecters in your source file, you have to put newline after each 2000 chars chunk, or compilation error will occure. Yeah, thx, but eventually I split it into array of char[1000] and it's all right to write the whole array on a single line. I think you are wrong. I've submitted my old solution, which has "unsigned char s[]="..." written in one line, and got AC again. The only thing I have is my own experience with MS C++ 2008 compiler. Maybe in VS C++ 2010 (which is currently used on Timus) they took off that restriction. in vc on timus limit ~16000 symbols Edited by author 29.06.2011 18:52 Another gotcha for C++ programmers: The line terminator in the input is "\r\n", so if you read input byte-by-byte, you will see both chars, but if you try to output "\r\n" via std::cout, you will get "\r\r\n" in the output. My solution was to just ignore \r in the input. * Be careful with percent signs using printf function: printf( "...if(c%3==0)..." ); // wrong!!! but sometimes it works printf( "...if(c%%3==0)..." ); // right * It isn't necessary but if you don't want to take care about CR LF characters, you can use this code: #include <io.h> #include <fcntl.h> _setmode(_fileno(stdin),_O_BINARY); _setmode(_fileno(stdout),_O_BINARY); * Don't use these characters: " ? \ Edited by author 03.11.2010 07:54 Check that all of arrays in your source less that 64 KB! (MS VC compiler limitation) There is my code. My stress works fine. The size of output program is something near 4/5 * length bytes. Using gets, all endl are written as '@'. There is an assert, that i haven't forgotten some symbols. Please, help! http://pastebin.com/WnQvat6gI suggest I got CE because of too long strings.) Stupid compiler:) I got AC) I have some question: why symbols '[', ']' and '~' are included in the texts? It's not clear from the statement, but I checked: output of your program (program, that prints input and has little size) may have size more than 64kb (all programs have some restrictions to the size in bytes). I got WA at #1, but I've tested for thousands of kbs of files and I don't know why I'm wrong at all. Would you please show me why I got WA,please!! After trial and trial, I passed the 1st test. But now, on 3rd test, why do I get WA? I think my algo will indeed work on the LITERARY text. ADMIN, would you please tell me what sort of error I got? CE, OLE(too long), or "Indeed" WA? Thanks. Try to crash your program, if size of output is larger, than size of input :) Maybe a good idea, though it might cause a lot of trouble. But how to know whether CE or "indeed WA"? Your solution should get AC, but there is a small problem in the problem checker. It will be fixed soon. WOW, admin, thanks a lot. This message makes me happy this night. One month has passed since my last submittion, have you forgotten updating the checker of this problem? If convenient, please fix the checker for all the users who have got stuck for this. Thank you in advance. PS: I have no means to be malicious. I am just coming to remind you. Please feel relaxed for this message :) Nearly one year have passed since your post. Just a reminder. A reminder. Reminder for ADMINs. One year and a half have passed, since your promise. Your solution has AC now. I rejudged all solutions of August 2010 ADMINS,what punctuation marks are used? How should the prog behave? as i understood we have NORMAL_TEXT as std input. Then we archivate the text. and what to do next? output archivated text or what? tell me plz As all symbols in the input will have codes less that 128 I can convert it to 7-bit char. Theoretically) But what shall I do?... I got AC with this algo, using Pascal; use bitwise operators to compress (shl/shr/or/and); Be carefull with freepascal: it not allow code strings longer than 255 and string constants longer than 255; do not forget to escape #10, #13, #26, and enclose single-quot ' You must also escape #0, and consider that #0 mean "String-end" int ASCIIZ-string representation, but if you using 7-bit encoding, #0 will appear in your output only if input contais '@' (#64) followed by symbol with code less than 31. By problem condition there only CR LF (#13 #10) less than #32, and should not be @, so, #0 will never appear in your output Если на паскале в конце выводимой программы поставить "readln;", получите WA1 Hello everyone! First of all, let me say thanks to the author for this wonderful problem. It really hooked my mind for 2 days. To solve it I used simple bitshift algorithm. My solution is written in C++. I have written 10 tests of different length (starting from 20 000 to more than 200 000 words) and my test system shows that solution is corrent (input=output and source code length is less than original text). But I always get WA4 when submitting it. WHY? Can you please explain the details? I really have no idea. I will appreciate every comment. Thank you. Edited by author 08.08.2010 15:58 Maybe you'll send me your program? Edited by author 10.08.2010 23:35 Sergey, thank you for your responce! I`ve sent email to you. |
|