Artifact Content
Not logged in

Artifact b6a2a6266ec7de62d9675023c0e7e1e176ac0572


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class BoardFolding { public:
	int howMany(int N, int M, vector <string> compressedPaper)
	{
		// decode
		int tonumber[128];
		for(char c='0'; c<='9'; ++c) tonumber[c] = c-'0';
		for(char c='a'; c<='z'; ++c) tonumber[c] = c-'a' + 10;
		for(char c='A'; c<='Z'; ++c) tonumber[c] = c-'A' + 36;
		tonumber['#'] = 62;
		tonumber['@'] = 63;

		vector<vector<int>> P(N, vector<int>(M));
		for(int i=0; i<N; ++i)
		for(int j=0; j<M; ++j)
			P[i][j] = (tonumber[compressedPaper[i][j / 6]] >> (j % 6)) % 2;

		// solve
		return solve(N, M, P);
	}

	int solve(int H, int W, const vector<vector<int>>& P)
	{
		// transpose
		vector<vector<int>> PT(W, vector<int>(H));
		for(int y=0; y<H; ++y)
		for(int x=0; x<W; ++x)
			PT[x][y] = P[y][x];

		// solve_each
		return solve_one(P) * solve_one(PT);
	}

	int solve_one(const vector<vector<int>>& P)
	{
		// hash
		vector<int> PH;
		map<vector<int>, int> hash;
		for(const auto& Pi: P) {
			if(!hash.count(Pi)) {
				int id = hash.size();
				hash[Pi] = id;
			}
			PH.push_back(hash[Pi]);
		}

		// solve
		return solve_one(PH);
	}

	int solve_one(const vector<int>& P)
	{
		const int N = P.size();

		vector<int> ok_range(N);
		for(int m=0; m<N; ++m)
			for(int w=1; m-w>=0 && m+w<=N; ++w) {
				if(P[m-w] != P[m+w-1])
					break;
				ok_range[m] = w;
			}

		vector<vector<bool>> vis(N, vector<bool>(N+1));

		function<void(int,int)> rec;
		rec = [&](int s, int e) {
			if(vis[s][e])
				return;
			vis[s][e] = true;
			for(int m=s+1; m<e; ++m)
				if(ok_range[m] >= min(m-s, e-m)) {
					if(m-s <= e-m)
						rec(m, e);
					if(e-m <= m-s)
						rec(s, m);
				}
		};
		rec(0, N);

		int cnt = 0;
		for(int s=0; s<N; ++s)
		for(int e=s+1; e<=N; ++e)
			if(vis[s][e] == 1)
				++cnt;
		return cnt;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, BoardFolding().howMany(N, M, compressedPaper));}
int main(){

CASE(1)
	int N = 2; 
	int M = 7; 
	string compressedPaper_[] = {"@@", "@@"};
	  vector <string> compressedPaper(compressedPaper_, compressedPaper_+sizeof(compressedPaper_)/sizeof(*compressedPaper_)); 
	int _ = 84; 
END
CASE(0)
	int N = 2; 
	int M = 2; 
	string compressedPaper_[] = {"1", "3"};
	  vector <string> compressedPaper(compressedPaper_, compressedPaper_+sizeof(compressedPaper_)/sizeof(*compressedPaper_)); 
	int _ = 1; 
END
CASE(2)
	int N = 4; 
	int M = 4; 
	string compressedPaper_[] = {"6", "9", "9", "6"};
	  vector <string> compressedPaper(compressedPaper_, compressedPaper_+sizeof(compressedPaper_)/sizeof(*compressedPaper_)); 
	int _ = 9; 
END
CASE(3)
	int N = 6; 
	int M = 1; 
	string compressedPaper_[] = {"0", "2", "6", "@", "4", "A"};
	  vector <string> compressedPaper(compressedPaper_, compressedPaper_+sizeof(compressedPaper_)/sizeof(*compressedPaper_)); 
	int _ = 6; 
END
CASE(4)
	int N = 3; 
	int M = 3; 
	string compressedPaper_[] = {"0", "2", "0"}
;
	  vector <string> compressedPaper(compressedPaper_, compressedPaper_+sizeof(compressedPaper_)/sizeof(*compressedPaper_)); 
	int _ = 1; 
END
/*
CASE(5)
	int N = ; 
	int M = ; 
	string compressedPaper_[] = ;
	  vector <string> compressedPaper(compressedPaper_, compressedPaper_+sizeof(compressedPaper_)/sizeof(*compressedPaper_)); 
	int _ = ; 
END
CASE(6)
	int N = ; 
	int M = ; 
	string compressedPaper_[] = ;
	  vector <string> compressedPaper(compressedPaper_, compressedPaper_+sizeof(compressedPaper_)/sizeof(*compressedPaper_)); 
	int _ = ; 
END
*/
}
// END CUT HERE