void MarkMap(const char* szStrs[], int n, int nBegIndex, bool chrMap[26][26])
{
assert(szStrs && nBegIndex >= 0 && n >= 0);
if (n <= 1) return;
int nDiv = -1;
for (int i = 0; i < n-1; i++)
{
char tmp1 = *(szStrs[i] + nBegIndex);
char tmp2 = *(szStrs[i+1] + nBegIndex);
assert(((tmp1 >= 'a' && tmp1 <= 'z') || tmp1 == 0));
assert(((tmp2 >= 'a' && tmp2 <= 'z') || tmp2 == 0));
if (tmp1 != 0 && nDiv < 0)
nDiv = i;
if (tmp1 == 0 || tmp1 == tmp2)
continue;
MarkMap(szStrs + nDiv, i + 1 - nDiv, nBegIndex + 1, chrMap);
nDiv = i+1;
chrMap[tmp1 - 'a'][tmp2 - 'a'] = true;
}
MarkMap(szStrs + nDiv, n - nDiv, nBegIndex + 1, chrMap);
}
void PrintComb(bool bRec[26], bool chrMap[26][26], char szTmp[26], int nBeg)
{
if (nBeg == 26)
{
for (int i = 0; i < 26; i++)
cout<cout<return;
}
for (int i = 0; i < 26; i++)
{
if (bRec[i])
continue;
int j = 0;
for (; j < 26; j++)
{
if (!bRec[j] && chrMap[j][i])
break;
}
if (j >= 26)
{
bRec[i] = true;
szTmp[nBeg] = 'a' + i;
PrintComb(bRec, chrMap, szTmp, nBeg + 1);
bRec[i] = false;
}
}
}
void PrintAllPossible(const char* szStrs[], int n)
{
assert(szStrs && n > 0);
bool chrMap[26][26];
memset(chrMap, 0, sizeof(chrMap));
MarkMap(szStrs, n, 0, chrMap);
bool bRec[26];
memset(bRec, 0, sizeof(bRec));
char szTmp[26];
PrintComb(bRec, chrMap, szTmp, 0);
}