#ifndef __EMITTED_VALIDATE_H
#define __EMITTED_VALIDATE_H

#include <bits/stdc++.h>

typedef void (*feedback_function)(const char*, ...);

const int EXITCODE_AC = 0;
const int EXITCODE_WA = 0;

// declare a constant to be used as user_out.
const std::string FILENAME_USER_OUTPUT = "user_out";
// declare a constant to be used as input.
const std::string FILENAME_INPUT = "input";

#define USAGE "./%s\n"
// we have to define USAGE as dummy because we don't want to modify the lib. too much.

std::ifstream judge_in, judge_ans;
// another ifstream must be used for user_out;
std::ifstream user_out;

char *feedbackdir = NULL;

void vreport_feedback(const char* msg, va_list pvar) {
    assert(stderr);
    vfprintf(stderr, msg, pvar);
}

void report_feedback(const char* msg, ...) {
    va_list pvar;
    va_start(pvar, msg);
    vreport_feedback(msg, pvar);
}

void judge_message(const char* msg, ...) {
    va_list pvar;
    va_start(pvar, msg);
    vreport_feedback(msg, pvar);
}

void judge_error(const char* msg, ...) {
    va_list pvar;
    va_start(pvar, msg);
    vreport_feedback(msg, pvar);
    assert(0);
}

// cleaning up.
void cleanup() {
    fflush(stderr);
    fclose(stderr);
    fflush(stdout);
    fclose(stdout);
    judge_in.close();
    user_out.close();
}

// write another function ``scoring()`` for ac or wa.
void scoring(double score) {
    fprintf(stdout, "%.f", score + 1e-9);
}

void accept() {
    scoring(100.);
    cleanup();
    exit(EXITCODE_AC);
}

void accept_with_score(double scorevalue) {
    scoring(scorevalue);
    cleanup();
    exit(EXITCODE_AC);
}

void wrong_answer(const char* msg, ...) {
    va_list pvar;
    va_start(pvar, msg);
    vreport_feedback(msg, pvar);
    scoring(0.);
    cleanup();
    exit(EXITCODE_WA);
}

void init_io() {
    judge_in.open(FILENAME_INPUT, std::ios_base::in);
    if(judge_in.fail()) {
        judge_error("failed to open input file.\n");
    }
    user_out.open(FILENAME_USER_OUTPUT, std::ios_base::in);
    if(user_out.fail()) {
        judge_error("failed to open user output file.\n");
    }
    judge_ans.open("answer", std::ios_base::in);
    if(judge_ans.fail()) {
        judge_error("failed to open answer file.\n");
    }
}
using namespace std;
bool check(string s, string s2, string t) {
    int l, l2 = 0;
    for (int i = 0; i < t.length(); i++) {
        while (l < s.length() && s[l] != t[i])
            l++;
        if (l >= s.length())
            return 0;
        while (l2 < s2.length() && s2[l2] != t[i])
            l2++;
        if (l2 >= s2.length())
            return 0;
        l++; l2++;
    }
    return 1;
}
bool check(istream &inf, istream &ans, istream &ouf, feedback_function waf) {
	string s_ans;
	int len;
	string s, s2, t;

	if (!(ans >> s_ans)) {
		if (ouf >> t) {
			waf("Sai. Không tồn tại xâu con chung!\n");
			return 0;
		}
		judge_message("OK. Không có xâu con chung!\n");
		return 1;
	}
	else
		len = s_ans.length();

    if (!(ouf >> t))
        waf("Sai. Du lieu ra khong dung dinh dang.\n");

    if (t.length() != len)
        waf("Sai. Do dai xau con chung dai nhat khong dung (You: %d vs DA: %d).\n", t.length(), len);

    inf >> s >> s2;

    if (!check(s, s2, t))
        waf("Sai. Xau con chung khong dung.\n");

    if (ouf >> t)
        waf("Sai. Du lieu ra khong dung dinh dang.\n");

    judge_message("OK. Do dai xau con chung dai nhat la %d!\n", len);
    return 1;
}
#endif

int main(void) {
    init_io();
    check(judge_in, judge_ans, user_out, wrong_answer);
    accept();
}
