// Given a string and a non - empty substring sub, compute recursively the largest substring which starts and ends with sub and return its length. int strDist( string str, string sub ) { if( str.length() < sub.length() ) return 0; if( str.substr( str.length() - sub.length(), sub.length() ) == sub ) return str.length() - str.find( sub, 0 ); return strDist( str.substr( 0, str.length() - 1 ), sub ); }
vector<vector<neighbor> > generateGraph(vector<string> &strVec) { auto n = strVec.size(); // the graph is saved by an adjacency list vector<vector<neighbor> > adjList(n, vector<neighbor>()); if(strVec.size() == 0) return adjList; for(int i=0; i<n-1; i++) { for(int j=i+1; j<n; j++) { if(strDist(strVec[i], strVec[j]) == 1) { adjList[i].push_back(neighbor(j, 1)); adjList[j].push_back(neighbor(i, 1)); } } } return adjList; }