Artifact 13f8fe20f39799a28232a433023ea7dbf97a333f
#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 <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;
class AmoebaCode { public:
// ���C�����[�`��
int find(string code, int K)
{
int answer = 1;
for(int D=2; D<=K; ++D)
if( can(code, K, 0, string(D-1,'X')) ) // �ŏ������� D �ȏ�ɂȂ�悤�ɂł���H
answer = max(answer, D);
return answer; // �ł���悤�� D �̍ő�l��Ԃ�
}
// �ucode[i..$) �� 1 �` K �߂��邩�H�v���ċA�I�ɔ���
// prev �ɂ� code[i] �̒��O D-1 ���������ČĂяo���B
// ���� D-1 �����͔����Ȃ��ƍŏ����� D �ȏ�ɂȂ�Ȃ��̂Ŕ����܂��傤
map<pair<size_t,string>, bool> memo;
bool can(const string& code, const int K, size_t i, const string& prev)
{
if( i == code.size() )
return true; // �Ō�܂Ŗ��܂����BOK!
pair<int,string> key(i, prev);
if( memo.count(key) )
return memo[key]; // �������B���łɌ��������Ȃ�o���Ă錋�ʂ�Ԃ�
// code[i] �ɓ���镶���̌����
vector<char> cand;
if( code[i] == '0' ) // '0' �Ȃ� 1 ���� K �̂ǂ�ɒu�������Ă�����
for(int k=1; k<=K; ++k)
cand.push_back( char('0'+k) );
else // ����ȊO�Ȃ�u���������Ȃ�
cand.push_back( code[i] );
bool ok = false;
for(int k=0; k<cand.size(); ++k) // ����S�������Ă݂�
if( count(prev.begin(), prev.end(), cand[k]) // prev �������
&& can(code, K, i+1, prev.substr(1)+cand[k]) ) // code[i+1..$) ���S�����߂����OK
ok |= true;
return memo[key] = ok; // �������̕\�ɋL������ return
}
};
// 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(_, AmoebaCode().find(code, K));}
int main(){
CASE(0)
string code = "01";
int K = 1;
int _ = 1;
END
CASE(1)
string code = "1001";
int K = 2;
int _ = 1;
END
CASE(2)
string code = "1010";
int K = 2;
int _ = 2;
END
CASE(3)
string code = "01001";
int K = 3;
int _ = 3;
END
CASE(4)
string code = "10012031001";
int K = 3;
int _ = 2;
END
CASE(5)
string code = "00000000000000000000000000000000000000000000000000";
int K = 7;
int _ = -1;
END
/*
CASE(6)
string code = ;
int K = ;
int _ = ;
END
*/
}
// END CUT HERE