더 어렵게 풀었어요.
2557번 - Hello World
WA...당신은 도대체...ㅋㅋ
빠르게 한 번 해보았습니다. https://www.acmicpc.net/source...
djm03178님꺼를 좀더 어렵게 해봤슴돠
다시 해보았습니다.
어렵게는 아니지만 길게 풀었습니다.
이정도면 어렵게 푼 것 같습니다
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include // 압축
#include
#include
#include
#include
#include
#include
#include
using namespace std;
class MessageException : public std::runtime_error {
public:
using std::runtime_error::runtime_error;
};
class IMessageStrategy {
public:
virtual string process(const string& msg) = 0;
virtual ~IMessageStrategy() = default;
};
static const string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
string base64_encode(const string& input) {
string output;
int val = 0, valb = -6;
for (unsigned char c : input) {
val = (val << 8) + c;
valb += 8;
while (valb >= 0) {
output.push_back(base64_chars[(val >> valb) & 0x3F]);
valb -= 6;
}
}
if (valb > -6) output.push_back(base64_chars[((val << 8) >> (valb + 8)) & 0x3F]);
while (output.size() % 4) output.push_back('=');
return output;
}
string base64_decode(const string& input) {
string output;
vector T(256, -1);
for (int i = 0; i < 64; i++) T[base64_chars[i]] = i;
int val = 0, valb = -8;
for (unsigned char c : input) {
if (T[c] == -1) break;
val = (val << 6) + T[c];
valb += 6;
if (valb >= 0) {
output.push_back(char((val >> valb) & 0xFF));
valb -= 8;
}
}
return output;
}
string compress_string(const string& str) {
z_stream zs{};
deflateInit(&zs, Z_BEST_COMPRESSION);
zs.next_in = reinterpret_cast(const_cast(str.data()));
zs.avail_in = str.size();
int ret;
char outbuffer[32768];
string outstring;
do {
zs.next_out = reinterpret_cast(outbuffer);
zs.avail_out = sizeof(outbuffer);
ret = deflate(&zs, Z_FINISH);
if (outstring.size() < zs.total_out) {
outstring.append(outbuffer, zs.total_out - outstring.size());
}
} while (ret == Z_OK);
deflateEnd(&zs);
return outstring;
}
string decompress_string(const string& str) {
z_stream zs{};
inflateInit(&zs);
zs.next_in = reinterpret_cast(const_cast(str.data()));
zs.avail_in = str.size();
int ret;
char outbuffer[32768];
string outstring;
do {
zs.next_out = reinterpret_cast(outbuffer);
zs.avail_out = sizeof(outbuffer);
ret = inflate(&zs, 0);
if (outstring.size() < zs.total_out) {
outstring.append(outbuffer, zs.total_out - outstring.size());
}
} while (ret == Z_OK);
inflateEnd(&zs);
return outstring;
}
class ComplexEncodingStrategy : public IMessageStrategy {
public:
string process(const string& msg) override {
return base64_encode(compress_string(msg));
}
};
class ComplexDecodingStrategy : public IMessageStrategy {
public:
string process(const string& msg) override {
return decompress_string(base64_decode(msg));
}
};
class MessageHandler {
shared_ptr encoder;
shared_ptr decoder;
mutex mtx;
public:
MessageHandler(shared_ptr enc, shared_ptr dec)
: encoder(enc), decoder(dec) {}
void saveToFile(const string& msg, const string& filename) {
lock_guard lock(mtx);
ofstream ofs(filename);
if (!ofs) throw MessageException("파일을 열 수 없습니다!");
ofs << encoder->process(msg);
}
string loadFromFile(const string& filename) {
lock_guard lock(mtx);
ifstream ifs(filename);
if (!ifs) throw MessageException("파일을 열 수 없습니다!");
stringstream buffer;
buffer << ifs.rdbuf();
return decoder->process(buffer.str());
}
};
template
struct CompileTimeString {
static string get() {
constexpr char arr[] = { chars..., 0 };
return string(arr);
}
};
template
constexpr auto makeCompileTimeString(std::integer_sequence) {
return CompileTimeString{};
}
#define STR_TO_SEQ(str) \
makeCompileTimeString, str[0], str[1], str[2], str[3], str[4], str[5], str[6], str[7], str[8], str[9], str[10], str[11]>(std::integer_sequence{})
int main() {
try {
const string message = "Hello World!";
auto encoder = make_shared();
auto decoder = make_shared();
MessageHandler handler(encoder, decoder);
string filename = "hello.dat";
handler.saveToFile(message, filename);
auto future = async(launch::async, [&handler, &filename]() {
string result = handler.loadFromFile(filename);
cout << result << endl;
});
future.get();
} catch (const exception& e) {
cerr << "오류 발생: " << e.what() << endl;
}
return 0;
}
댓글을 작성하려면 로그인해야 합니다.
point_102us 5년 전 5
제곧내 어렵게 풀어보실분 구합니다