Artifact Content
Not logged in

Artifact 02fca67c4fcabadb60ada5301d3a316c54ac1690


#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 NextHomogeneousStrings { public:
	string getNext(int d, int n, string seed, long long k) 
	{
		int M = seed.size();

		LL L=0, R=1; // [L,R)
		while( R-L > 2 ) {
			LL C = (R+L)/2;
			string t = xth(d,n,C,M);
			(t=="" || seed<t ? R : L) = C;
		}
		return seed==xth(d,n,L,M) ? xth(d,n,L+k-1,M) : xth(d,n,L+k,M);
	}
	string xth(int d, int n, LL x, int M)
	{
		return rec( string(), x, d, n, M );
	}
	string rec( string hist, LL x, int d, int n, int M )
	{
		if( M == 0 )
			return "";
		for(char c='a'; c<='z'; ++c)
		{
			string hh = hist + c;
			// todo: normalize
			if( x < cnt(hh,d,n,M) )
				return c + rec(hist+c, x, d, n, M-1);
			x -= cnt(hh,d,n,M);
		}
		return "";
	}

	LL cnt(string hist, int d, int n, int M)
	{
		if( M == 0 )
			return 1;

		// todo: memoize

		LL ans = 0;
		for(char c='a'; c<='z'; ++c)
		{
			char cc = c;
			if( find(hist.begin(), hist.end(), c)==hist.end() )
				cc = *max_element(hist.begin(),hist.end()) + 1; // todo: end then 'a'
			string h2 = hist+cc;
			// todo: check violation
			// todo: drop 1st (if needed)
			// todo: normalize
			//ans += cnt(h2, dd, nn, M-1);
		}
		return ans;
	}
};

// 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 string& Expected, const string& 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(_, NextHomogeneousStrings().getNext(d, n, seed, k));}
int main(){

CASE(0)
	int d = 1; 
	int n = 2; 
	string seed = "aaa"; 
	long long k = 3LL; 
	string _ = "ddd"; 
END
CASE(1)
	int d = 2; 
	int n = 3; 
	string seed = "abc"; 
	long long k = 0LL; 
	string _ = "aca"; 
END
CASE(2)
	int d = 2; 
	int n = 4; 
	string seed = "ttrrzz"; 
	long long k = 6LL; 
	string _ = "ttsssc"; 
END
CASE(3)
	int d = 6; 
	int n = 8; 
	string seed = "txyaaxaassaaaarghjsdohasdghususdidisisdodo"; 
	long long k = 10000000000000000LL; 
	string _ = "txyaaxaassaaaarghjsgaaaaaaaaadntffiniqrddy"; 
END
CASE(4)
	int d = 2; 
	int n = 5; 
	string seed = "zzzzzaa"; 
	long long k = 100LL; 
	string _ = ""; 
END
/*
CASE(5)
	int d = ; 
	int n = ; 
	string seed = ; 
	long long k = LL; 
	string _ = ; 
END
CASE(6)
	int d = ; 
	int n = ; 
	string seed = ; 
	long long k = LL; 
	string _ = ; 
END
*/
}
// END CUT HERE