#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define FOR_EACH_C(p,c) for(p=(c).begin(); p!=(c).end();p++) #define FOR_EACH(p,q,r) for(p=q;p!=r;p++) typedef vector ivec_t; typedef vector imat_t; #ifndef DEBUG #define cin fin ifstream fin("E.txt"); #endif #define debug cout vector g_names; bool operator<(pair a,pair b) { if(a.first < b.first)return true; if(a.first == b.first && a.second < b.second)return true; return false; } int GetEditDistance(string s1,string s2) { int n=s1.size(),m=s2.size(),i,j; imat_t mat(n+1,ivec_t(m+1,9)); mat[0][0] = 0; FOR_EACH(i,0,n){ mat[i+1][0] = i+1; } FOR_EACH(i,0,m){ mat[0][i+1] = i+1; } FOR_EACH(i,0,n){ FOR_EACH(j,0,m){ int r=i+1,c=j+1; mat[r][c] = min(mat[r-1][c] + 1,mat[r][c-1]+1); if(s1[i] == s2[j]) { mat[r][c] = min(mat[r-1][c-1],mat[r][c]); } else { mat[r][c] = min(mat[r-1][c-1]+1,mat[r][c]); } if(i>0 && j>0) { if(s1[i] == s2[j-1] && s1[i-1] == s2[j]) { if(mat[r-2][c-2]+1 < mat[r][c]) { mat[r][c] = mat[r-2][c-2]+1; } } } if(i > 1 && j) { if(s1[i] == s2[j-1] && s1[i-2] == s2[j]) { if(mat[r-3][c-2] +2 < mat[r][c]) { mat[r][c] = mat[r-3][c-2]+2; } } } if(i > 0 && j > 1) { if(s1[i] == s2[j-2] && s1[i-1] == s2[j]) { if(mat[r-2][c-3] +2 < mat[r][c]) { mat[r][c] = mat[r-2][c-3]+2; } } } } } // debug << s1 << ' ' << s2 << endl; // FOR_EACH(i,0,n+1){ // FOR_EACH(j,0,m+1){ // debug << mat[i][j] << ' ' ; // } // debug << endl; // } return mat[n][m]; } void Solve(int n,int d) { vector > ans; int i,j; FOR_EACH(i,0,n){ FOR_EACH(j,i+1,n){ int dist = GetEditDistance(g_names[i],g_names[j]); if(dist <= d) { ans.push_back(make_pair(g_names[i],g_names[j])); } } } sort(ans.begin(),ans.end()); vector > ::iterator p; FOR_EACH_C(p,ans){ cout << p->first << ',' << p->second << endl; } cout << ans.size() << endl; } int main() { int n,d,i; while(cin >> n , n ) { cin >> d; g_names.assign(n,string("")); FOR_EACH(i,0,n){ cin >> g_names[i]; } sort(g_names.begin(),g_names.end()); Solve(n,d); } return 0; }