// lab1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdint.h>
#include <iostream>

#include <fstream>
#include <string>
#include <list>

#include <algorithm>

#include "Impl.h"
#include "ImplList.h"
#include "func.h"

using namespace std;

bool checkExclusion(BM exclusion, ImplList &src, ImplList &tdnf)
{
	ImplList::iterator it;
	for (it = src.begin(); it != src.end(); it++)
		if (!(*it)->Indep)
			if (!tdnf.getValue((*it)->Num, exclusion))
				return false;
	return true;
}

BM findMaxExclusion(ImplList &src, ImplList &tdnf)
{
	BM maxE = 0;
	for (BM e = 0; e < (1U<<tdnf.size()); e++)
		if (count(e) > count(maxE) && checkExclusion(e, src, tdnf))
			maxE = e;
	return maxE;
}

int main(int argc, char* argv[])
{
    ifstream infile;
    string filename = "input.txt";
    if (argc == 2)
        filename = string(argv[1]);
    if (filename != "-")
        infile.open(filename.c_str());
    istream &in = infile.is_open() ? infile : cin;
	while (!in.eof())
	{
		string line;
		in >> line;
		if (in.fail())
			return 0;
		cout << line << endl;

		ImplList srcL(line);
		cout << srcL.getValuesLine() << endl;
		cout << srcL.toString() << endl;

		ImplList *curL = new ImplList(srcL), *newL;

		while ((newL = curL->tryMinimize())!=0)
		{
			delete curL;
			curL = newL;
		}
		cout << "------- TDNF -------" << endl;
		cout << curL->getValuesLine() << endl;
		cout << curL->toString() << endl;

		BM maxE = findMaxExclusion(srcL, *curL);
		ImplList *tdnf = new ImplList(*curL, maxE);
		cout << "------- MDNF -------" << endl;
		cout << tdnf->getValuesLine() << endl;
		cout << tdnf->toString() << endl;

		cout << "------- ---- -------" << endl;
	}
	return 0;
}

