#include using namespace std; typedef signed long long ll; #undef _P #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x,to) for(x=0;x<(to);x++) #define FORR(x,arr) for(auto& x:arr) #define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++) #define ALL(a) (a.begin()),(a.end()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) //------------------------------------------------------- int N,K; string A[101]; int B[101]; set S; unordered_set memo[101]; ll mo[2]={1000000007,1000000021}; void dfs(int id,string& cur,string& M,ll H1,ll H2,ll ma) { if(S.size()>K+1000) { return; if(cursubstr(0,cur.size())) return; } ll H3=(H1<<32)|H2; H3^=ma; if(memo[id].count(H3)) return; memo[id].insert(H3); if(id==N) { S.insert(cur); //if(S.size()>K) S.erase(S.begin()); return; } int i; FOR(i,N) if(M[i]=='0') { if(i&&A[i]==A[i-1]&&M[i-1]=='0') continue; M[i]='1'; cur+=A[i]; ll G1,G2; if(B[i]<10) { G1=H1*10+B[i]; G2=H2*10+B[i]; } else { G1=H1*100+B[i]; G2=H2*100+B[i]; } dfs(id+1,cur,M,G1%mo[0],G2%mo[1],ma|(1LL<<(i%50))); cur.resize(cur.size()-A[i].size()); M[i]='0'; } } void solve() { int i,j,k,l,r,x,y; string s; cin>>N>>K; FOR(i,N) cin>>A[i]; FOR(i,N) { FOR(j,N-1) { if(A[j]+A[j+1]