#include < iostream > #include < string > #include < assert.h > #include < iterator > using namespace std; int pow( int num, int n) ... { int result=num; while(--n) ...{ result*=num; } return result;} string GetWord( const string & str, int len, int id) ... { assert(id<=pow(str.size(),len)); string result; result.resize(len+1); for(int i=len;i>0;--i,id/=str.size())...{ result[i]=(str[id%str.size()]); } return result;} template < typename InputIterator > void OutPutAllSubString( const string & str,InputIterator result) // except NULL ... { for(int i=1;i<=str.size();++i)...{//form 1 to n for(int j=0;j<pow(str.size(),i);++j)...{//all possible substr with len=i *result++=GetWord(str,i,j); } }} int main() ... { OutPutAllSubString("abcde",ostream_iterator<string>(cout," ")); return 0;} // output like this: /**/ /*abcdeaaab...*/