Problem:
Score : 400 points

Problem Statement

In this problem, we only consider strings consisting of lowercase English letters.

Strings s and t are said to be isomorphic when the following conditions are satisfied:

|s| = |t| holds.

For every pair i, j, one of the following holds:

s_i = s_j and t_i = t_j.

s_i \neq s_j and t_i \neq t_j.

For example, abcac and zyxzx are isomorphic, while abcac and ppppp are not.

A string s is said to be in normal form when the following condition is satisfied:

For every string t that is isomorphic to s, s \leq t holds. Here \leq denotes lexicographic comparison.

For example, abcac is in normal form, but zyxzx is not since it is isomorphic to abcac, which is lexicographically smaller than zyxzx.

You are given an integer N.
Print all strings of length N that are in normal form, in lexicographically ascending order.

Constraints

1 \leq N \leq 10

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Assume that there are K strings of length N that are in normal form: w_1, \ldots, w_K in lexicographical order.
Output should be in the following format:

w_1
:
w_K

Sample Input 1

1

Sample Output 1

a

Sample Input 2

2

Sample Output 2

aa
ab

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vii vector<int>
#define vll vector<unsigned ll>
#define mp make_pair
#define F first
#define S second
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const unsigned ll l=1000000007;
const int MAX_=2000;
int T;

void print(string res, int i)
{
  printf("Case #%d: %s\n",T-i,res.c_str());
  // cout << res;
}
void print(ll res, int i)
{
  printf("Case #%d: %lld\n",T-i,res);
  // cout << res;
}

// ll ans(vll &a, int i)
// {
//   if(i == 0)
//     return 1;

//   int cnta=0,cntb=0;

//   for(auto x: a)
//   {
//     if(x & (1<<i) == 0)
//     {
//       cnta++;
//     }
//     else
//       cntb++;
//   }

//   if(cnta == a.size())
//   {
//     return ans(a,i-1);
//   }

//   if(cntb == a.size())
//   {
//     return 1<<i + ans(a,i-1);
//   }

//   ll toret1, toret2;

// }

string x = "abcdefghijklmnopqrstuvwxyz";

vector<string> ans;

void get(string &s, int i, int k)
{
  if(i >= s.size())
  {
    // cout << s << endl;
    ans.pb(s);
    return;
  }

  for(int j=0; j<=k; j++)
  {
    s[i] = x[j];
    get(s,i+1,j+1);
  }

}

void solve(int t)
{
  int n;
  cin >> n;
  string s(n,'a');
  get(s,0,0);
  sort(ans.begin(), ans.end());
  for(auto x: ans)
    cout << x << endl;
}

int main()
{
  IOS
  int t,testCases;
  testCases=1;
  #ifdef DEBUG
    freopen("input.txt","r",stdin);
    cin >> testCases;
    cout << "Running " << testCases << " testcases" << endl;
    freopen("output.txt","w",stdout);
    freopen("error.txt","w",stderr);
  #endif

  while(testCases--)
  {
    t=1;
    // cin >> t; //comment this in case of singular test case
    T = t;
    while(t--)
    {
      // cout << "HERE" << endl;
      solve(t);
    }
  }
  return 0;

}