Problem:
Problem E:
Confusing Login Names
Meikyokan University is very famous for its research and education in the area of computer
science. This university has a computer center that has advanced and secure computing facilities
including supercomputers and many personal computers connected to the Internet.
One of the policies of the computer center is to let the students select their own login names.
Unfortunately, students are apt to select similar login names, and troubles caused by mistakes
in entering or specifying login names are relatively common. These troubles are a burden on
the staff of the computer center.
To avoid such troubles, Dr. Choei Takano, the chief manager of the computer center, decided
to stamp out similar and confusing login names. To this end, Takano has to develop a program
that detects confusing login names.
Based on the following four operations on strings, the distance between two login names is
determined as the minimum number of operations that transforms one login name to the other.
Deleting a character at an arbitrary position.
Inserting a character into an arbitrary position.
Replacing a character at an arbitrary position with another character.
Swapping two adjacent characters at an arbitrary position.
For example, the distance between “
omura
” and “
murai
” is two, because the following sequence
of operations transforms “
omura
” to “
murai
”.
delete ‘o’        insert ‘i’
    omura      -->        mura      -->      murai
Another example is that the distance between “
akasan
” and “
kaason
” is also two.
swap ‘a’ and ‘k’         replace ‘a’ with ‘o’
    akasan        -->           kaasan       -->               kaason
Takano decided that two login names with a small distance are confusing and thus must be
avoided.
Your job is to write a program that enumerates all the confusing pairs of login names.
Beware that the rules may combine in subtle ways. For instance, the distance between “
ant
”
and “
neat
” is two.
swap ‘a’ and ‘n’      insert ‘e’
  ant         -->         nat      -->      neat
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n
d
name
1
name
2
...
name
n
The first integer
n
is the number of login names. Then comes a positive integer
d
. Two login
names whose distance is less than or equal to
d
are deemed to be confusing. You may assume
that 0 <
n
≤ 200 and 0 <
d
≤ 2. The
i
-th student’s login name is given by
name
i
, which is
composed of only lowercase letters. Its length is less than 16. You can assume that there are no
duplicates in
name
i
(1 ≤
i
≤
n
).
The end of the input is indicated by a line that solely contains a zero.
Output
For each dataset, your program should output all pairs of confusing login names, one pair per
line, followed by the total number of confusing pairs in the dataset.
In each pair, the two login names are to be separated only by a comma character (,), and the
login name that is alphabetically preceding the other should appear first.
...(truncated)

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

#define FF first
#define SS second
template<class S, class T>
istream& operator>>(istream& is, pair<S,T>& p){
  return is >> p.FF >> p.SS;
}
template<class S, class T>
ostream& operator<<(ostream& os, const pair<S,T>& p){
  return os << p.FF << " " << p.SS;
}
template<class T>
void maxi(T& x, T y){
  if(x < y) x = y;
}
template<class T>
void mini(T& x, T y){
  if(x > y) x = y;
}


const double EPS = 1e-10;
const double PI  = acos(-1.0);
const LL MOD = 1e9+7;

void change(const string& s, set<string>& dest){
  dest.insert(s);
  for(int i=0;i<SZ(s);++i){
	string tmp = s;
	tmp.erase(i, 1);
	dest.insert(tmp);
  }
  for(int i=0;i<=SZ(s);++i){
	string tmp = s;
	tmp.insert(i, ".");
	REP(c,26){
	  tmp[i] = c + 'a';
	  dest.insert(tmp);
	}
  }
  
  for(int i=0;i<SZ(s);++i){
	string tmp = s;
	REP(c,26){
	  tmp[i] = c + 'a';
	  dest.insert(tmp);
	}
  }
  for(int i=0;i+1<SZ(s);++i){
	string tmp = s;
	swap(tmp[i], tmp[i+1]);
	dest.insert(tmp);
  }
}

int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, D;
  while(cin >> N, N){
	cin >> D;
	VS vs(N);
	REP(i,N){
	  cin >> vs[i];
	}

	vector<set<string>> memo(N);
	map<string,VI> ids;
	REP(i,N){
	  change(vs[i], memo[i]);
	  for(auto& s: memo[i])
		ids[s].PB(i);
	}

	vector<pair<string,string>> ans;
	for(auto& p: ids){
	  if(SZ(p.SS) == 1) continue;
	  if(D == 2){
		REP(ii,SZ(p.SS)) REP(jj,ii){
		  int i = p.SS[ii], j = p.SS[jj];
		  if(vs[i] < vs[j])
			ans.EB(vs[i], vs[j]);
		  else
			ans.EB(vs[j], vs[i]);
		}
	  }
	  REP(ii,SZ(p.SS)) REP(jj,SZ(p.SS)){
		if(ii == jj) continue;
		int i = p.SS[ii], j = p.SS[jj];
		if(p.FF != vs[i] && p.FF != vs[j]) continue;
		
		if(vs[i] < vs[j])
		  ans.EB(vs[i], vs[j]);
		else
		  ans.EB(vs[j], vs[i]);
	  }
	}
	/*
	REP(i,N) REP(j,N){
	  if(i == j) continue;
	  for(auto& s: memo[i]){
		if((D == 2 && memo[j].count(s)) || s == vs[j]){
		  if(vs[i] < vs[j])
			ans.EB(vs[i], vs[j]);
		  else
			ans.EB(vs[j], vs[i]);
		  break;
		}
	  }
	}
	*/

	SORT(ans);
	UNIQ(ans);
	for(auto& p: ans)
	  cout << p.FF << "," << p.SS << endl;
	cout << SZ(ans) << endl;
  }

  return 0;
}
accuracy: 1
time_ms: 380
memory_kb: 34432
final_rank: 18

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll = long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define rep2(i,s,t) for(int i=s;i<t;i++)

void solve(int N){
    int d;
    cin>>d;
    map<string,vector<array<int,2>>> M;
    set<array<int,2>> S;
    vector<string> P(N);
    rep(i,N)cin>>P[i];
    sort(P.begin(),P.end());
    rep(i,N){
        for(auto v:M[P[i]])S.insert({v[0],i});
        M[P[i]].push_back({i,0});
        int PN=P[i].size();
        rep(j,PN+1){
            if(j<PN-1){
                int k=j+1;
                string Q=P[i];
                swap(Q[j],Q[k]);
                for(auto v:M[Q])if(v[1]+1<=d)S.insert({v[0],i});
                M[Q].push_back({i,1});
            }
            if(j<PN){
                string Q=P[i].substr(0,j);
                if(j!=PN-1)Q+=P[i].substr(j+1,PN-j-1);
                for(auto v:M[Q])if(v[1]+1<=d)S.insert({v[0],i});
                M[Q].push_back({i,1});
            }
            {
                rep(c,26){
                    string Q=P[i].substr(0,j);
                    Q.push_back(c+'a');
                    Q+=P[i].substr(j,PN-j);
                    for(auto v:M[Q])if(v[1]+1<=d)S.insert({v[0],i});
                    M[Q].push_back({i,1});
                }
            }
            if(j<PN){
                rep(c,26){
                    string Q=P[i];
                    Q[j]='a'+c;
                    for(auto v:M[Q])if(v[1]+1<=d)S.insert({v[0],i});
                    M[Q].push_back({i,1});
                }
            }
        }
    }
    set<array<int,2>> AN;
    for(auto p:S)if(p[0]!=p[1])AN.insert(p);
    swap(S,AN);
    
    for(auto p:S){
        cout<<P[p[0]]<<","<<P[p[1]]<<endl;
    }
    cout<<S.size()<<endl;
}
int main() {

    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int N;
    while(cin>>N,N!=0)solve(N);
}
accuracy: 1
time_ms: 340
memory_kb: 22456
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll = long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define rep2(i,s,t) for(int i=s;i<t;i++)

using vi = vector<int>;
using vvi=vector<vi>;

void solve(int N){
    int d;
    cin>>d;
    map<string,vector<array<int,2>>> M;
    set<array<int,2>> S;
    vector<string> P(N);
    rep(i,N){
        cin>>P[i];
    }
    sort(P.begin(),P.end());
    rep(i,N){
        // cout<<i<<" start"<<endl;
        for(auto v:M[P[i]]){
            S.insert({v[0],i});
        }
        M[P[i]].push_back({i,0});
        int PN=P[i].size();
        // cout<<i<<" step1"<<endl;
        rep(j,PN-1){
            int k=j+1;
            string Q=P[i];
            swap(Q[j],Q[k]);
            for(auto v:M[Q]){
                if(d==2)S.insert({v[0],i});
                else if(v[1]==0)S.insert({v[0],i});
            }
            M[Q].push_back({i,1});
        }
        // cout<<i<<" step2"<<endl;
        rep(j,PN){
            string Q=P[i].substr(0,j);
            if(j!=PN-1)Q+=P[i].substr(j+1,PN-j-1);
            for(auto v:M[Q]){
                if(d==2)S.insert({v[0],i});
                else if(v[1]==0)S.insert({v[0],i});
            }
            M[Q].push_back({i,1});
        }
        // cout<<i<<" step3"<<endl;
        rep(j,PN+1){
            rep(c,26){
                string Q=P[i].substr(0,j);
                Q.push_back(c+'a');
                Q+=P[i].substr(j,PN-j);
                for(auto v:M[Q]){
                    if(d==2)S.insert({v[0],i});
                    else if(v[1]==0)S.insert({v[0],i});
                }
                M[Q].push_back({i,1});
            }
        }
        rep(j,PN){
            rep(c,26){
                string Q=P[i];
                Q[j]='a'+c;
                for(auto v:M[Q]){
                    if(d==2)S.insert({v[0],i});
                    else if(v[1]==0)S.insert({v[0],i});
                }
                M[Q].push_back({i,1});
            }
        }
    }
    set<array<int,2>> AN;
    for(auto p:S)if(p[0]!=p[1])AN.insert(p);
    swap(S,AN);
    
    for(auto p:S){
        cout<<P[p[0]]<<","<<P[p[1]]<<endl;
    }
    cout<<S.size()<<endl;

}

int main() {

    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int N;
    while(cin>>N,N!=0)solve(N);

}
accuracy: 1
time_ms: 340
memory_kb: 22472
final_rank: 12

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rrep(i, a, b) for (ll i = (ll)(b)-1; i >= (ll)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

set<string> makeset(string S) {
    set<string> st;
    rep(i,0,S.size()) {
        string U = S.substr(0,i) + S.substr(i+1,S.size()-i-1);
        st.insert(U);
    }
    rep(i,0,S.size()+1) {
        rep(j,0,26) {
            string U = S.substr(0,i);
            U =  U + (char)('a'+j);
            U = U + S.substr(i,S.size()-i);
            st.insert(U);
        }
    }
    rep(i,0,S.size()) {
        char C = S[i];
        rep(j,0,26) {
            S[i] = 'a' + j;
            st.insert(S);
            S[i] = C;
        }
    }
    rep(i,0,S.size()-1) {
        swap(S[i],S[i+1]);
        st.insert(S);
        swap(S[i],S[i+1]);
    }
    return st;
}

int main() {
while(1) {
    int N, D;
    cin >> N >> D;
    if (N == 0) return 0;
    vector<string> S(N);
    rep(i,0,N) cin >> S[i];
    sort(ALL(S));
    vector<set<string>> ST(N);
    rep(i,0,N) ST[i] = makeset(S[i]);
    auto Solve = [&](int i, int j) -> bool {
        if (ST[i].count(S[j])) return true;
        if (D == 1) return false;
        for (auto it = ST[j].begin(); it != ST[j].end(); it++) {
            if (ST[i].count(*it)) return true;
        }
        return false;
    };
    int ANS = 0;
    rep(i,0,N) {
        rep(j,i+1,N) {
            if (Solve(i,j)) {
                cout << S[i] << ',' << S[j] << endl;
                ANS++;
            }
        }
    }
    cout << ANS << endl;
}
}
accuracy: 1
time_ms: 3690
memory_kb: 15540
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;

struct RollingHash {
    static constexpr int M = 2;
    static constexpr long long MODS[M] = {999999937, 1000000007};
    static constexpr long long BASE = 9973;
    vector<long long> powb[M], hash[M];
    int n;
    RollingHash() {}
    RollingHash(const string& str) { init(str); }
    void init(const string& str) {
        n = str.size();
        for (int k = 0; k < M; k++) {
            powb[k].resize(n + 1, 1);
            hash[k].resize(n + 1, 0);
            for (int i = 0; i < n; i++) {
                hash[k][i + 1] = (hash[k][i] * BASE + str[i]) % MODS[k];
                powb[k][i + 1] = powb[k][i] * BASE % MODS[k];
            }
        }
    }
    // get hash str[l,r)
    long long get(int l, int r, int k) {
        long long res = hash[k][r] - hash[k][l] * powb[k][r - l] % MODS[k];
        if (res < 0) res += MODS[k];
        return res;
    }
};

bool match(RollingHash& rh1, int l1, int r1, RollingHash& rh2, int l2, int r2) {
    bool res = true;
    for (int k = 0; k < RollingHash::M; k++) {
        res &= rh1.get(l1, r1, k) == rh2.get(l2, r2, k);
    }
    return res;
}

void solve(int n) {
    int D; cin >> D;
    vector<string> s(n);
    for(int i = 0; i < n; i++) cin >> s[i];
    sort(s.begin(), s.end());
    vector<pair<string, string>> ans;

    auto gen = [&](string s) {
        vector<string> res;
        const int len = s.size();
        // insert
        for(int i = 0; i <= len; i++) {
            string ss(len + 1, '?');
            for(int k = 0; k < i; k++) ss[k] = s[k];
            for(int k = i; k < len; k++) ss[k + 1] = s[k];
            for(char c = 'a'; c <= 'z'; c++) {
                ss[i] = c;
                res.push_back(ss);
            }
        }

        // erase
        for(int i = 0; i < len; i++) {
            string ss = "";
            for(int k = 0; k < len; k++) if(k != i) ss += s[k];
            res.push_back(ss);
        }

        // replace
        for(int i = 0; i < len; i++) {
            for(char c = 'a'; c <= 'z'; c++) {
                char p = s[i];
                s[i] = c;
                res.push_back(s);
                s[i] = p;
            }
        }

        // swap
        for(int i = 0; i + 1 < len; i++) {
            swap(s[i], s[i + 1]);
            res.push_back(s);
            swap(s[i], s[i + 1]);
        }

        return res;
    };
    vector<vector<string>> ss(n);
    for(int i = 0; i < n; i++) {
        ss[i] = gen(s[i]);
        sort(ss[i].begin(), ss[i].end());
    }
    
    for(int i = 0; i < n; i++) {
        for(int j = i + 1; j < n; j++) {
            bool ok = [&] {
                if(binary_search(ss[i].begin(), ss[i].end(), s[j])) return true;
                if(D == 2) {
                    for(const string& si : ss[i]) {
                        if(binary_search(ss[j].begin(), ss[j].end(), si)) return true;
                    }
                }
                return false;
            }();
            if(ok) ans.push_back({s[i], s[j]});
        }
    }

    for(auto& [u, v] : ans) cout << u << "," << v << "\n";
    cout << ans.size() << "\n";
    return;
}

int main() {
    while(true) {
        int n; cin >> n;
        if(n == 0) return 0;
        solve(n);
    }
}
accuracy: 1
time_ms: 3170
memory_kb: 9584
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;

const int inf = 1e9;
const int64_t inf64 = 1e18;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

int damerau_levenshtein_distance(const string &a, const string &b) {
  unordered_map<int, int> da;
  unordered_map<int, unordered_map<int, int>> d;
  int maxdist = a.size() + b.size();
  d[-1][-1] = maxdist;
  for (int i = 0; i <= a.size(); ++i) {
    d[i][-1] = maxdist;
    d[i][0] = i;
  }
  for (int i = 0; i <= b.size(); ++i) {
    d[-1][i] = maxdist;
    d[0][i] = i;
  }
  for (int i = 1; i <= a.size(); ++i) {
    int db = 0;
    for (int j = 1; j <= b.size(); ++j) {
      int k = da[b[j - 1]], l = db, cost;
      if (a[i - 1] == b[j - 1]) {
        cost = 0;
        db = j;
      } else
        cost = 1;
      d[i][j] = min({d[i - 1][j - 1] + cost, d[i][j - 1] + 1, d[i - 1][j] + 1,
                     d[k - 1][l - 1] + (i - k - 1) + 1 + (j - l - 1)});
    }
    da[a[i - 1]] = i;
  }
  return d[a.size()][b.size()];
}

void solve(int n) {
  int d;
  vector<string> name;
  name.reserve(n);
  cin >> d;
  for(int i = 0; i < n; ++i) {
    string temp;
    cin >> temp;
    name.push_back(temp);
  }
  sort(name.begin(), name.end());
  int count = 0;
  for(int i = 0; i < n; ++i) {
    for(int j = i + 1; j < n; ++j) {
      if (damerau_levenshtein_distance(name[i], name[j]) > d)
        continue;
      ++count;
      cout << name[i] << "," << name[j] << endl;
    }
  }
  cout << count << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  for (;;) {
    int n;
    cin >> n;
    if (!n)
      break;
    solve(n);
  }
  return 0;
}
accuracy: 1
time_ms: 2880
memory_kb: 3496
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif
const int inf = 1e9;
const int64_t inf64 = 1e18;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

int damerau_levenshtein_distance(const string &a, const string &b, const int alphabet_size) {
  vector<int> da(256, 0); 
  vector<vector<int>> d(a.size() + 2, vector<int>(b.size() + 2));
  int maxdist = a.size() + b.size();
  d[0][0] = maxdist;
  for (int i = 0; i <= a.size(); ++i) {
    d[i+1][1] = i;
    d[i+1][0] = maxdist;
  }
  for (int i = 0; i <= b.size(); ++i) {
    d[1][i+1] = i;
    d[0][i+1] = maxdist;
  }
  for (int i = 1; i <= a.size(); ++i) {
    int db = 0;
    for (int j = 1; j <= b.size(); ++j) {
      int k = da[b[j - 1]], l = db, cost;
      if (a[i - 1] == b[j - 1]) {
        cost = 0;
        db = j;
      } else
        cost = 1;
      d[i+1][j+1] = min({d[i][j] + cost, d[i+1][j] + 1, d[i][j+1] + 1, d[k][l] + (i - k - 1) + 1 + (j - l - 1)});
    }
    da[a[i - 1]] = i;
  }
  return d[a.size()+1][b.size()+1];
}

void solve(int n) {
  int d;
  vector<string> name(n);
  cin >> d;
  rep(i, 0, n) cin >> name[i];
  sort(name.begin(), name.end());
  int count = 0;
  rep(i, 0, n) {
    rep(j, i + 1, n) {
      if (damerau_levenshtein_distance(name[i], name[j], 26) > d)
        continue;
      ++count;
      cout << name[i] << "," << name[j] << endl;
    }
  }
  cout << count << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  for (;;) {
    int n;
    cin >> n;
    if (!n)
      break;
    solve(n);
  }
  return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 3164
final_rank: 1

code_snippet: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#define MIN(a,b,c,d) min(min(a,b),min(c,d))

using namespace std;

int damerau_levenshtein_distance(const string &a, const string &b) {
    vector<int> da(256);
    int sz_a = a.size(), sz_b = b.size();
    vector<vector<int> > d(sz_a+2, vector<int>(sz_b+2));
    int INF = sz_a + sz_b, i, j, db;
    d[0][0] = INF;
    for(i = 0; i <= sz_a; ++i) {
        d[i+1][1] = i;
        d[i+1][0] = INF;
    }
    for(j = 0; j <= sz_b; ++j) {
        d[1][j+1] = j;
        d[0][j+1] = INF;
    }
    for(i = 1; i <= sz_a; ++i) {
        db = 0;
        for(j = 1; j <= sz_b; ++j) {
            int i1 = da[b[j-1]], j1 = db, cost;
            if (a[i-1] == b[j-1]) {
                cost = 0;
                db = j;
            } else
                cost = 1;
            d[i+1][j+1] = MIN(d[i][j] + cost, d[i+1][j] + 1, d[i][j+1] + 1, d[i1][j1] + (i - i1 - 1) + 1 + (j - j1 - 1));
        }
        da[a[i-1]] = i;
    }
    return d[sz_a+1][sz_b+1];
}

void solve(int n) {
    int d;
    vector<string> name(n);
    cin >> d;
    for(int i = 0; i < n; ++i) cin >> name[i];
    sort(name.begin(), name.end());
    int count = 0;
    for(int i = 0; i < n; ++i) {
        for(int j = i + 1; j < n; ++j) {
            if (damerau_levenshtein_distance(name[i], name[j]) > d)
                continue;
            ++count;
            cout << name[i] << "," << name[j] << endl;
        }
    }
    cout << count << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.setf(ios::fixed);
    cout.precision(10);
    for (;;) {
        int n;
        cin >> n;
        if (!n)
            break;
        solve(n);
    }
    return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 3384
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;

const int MAX_SIZE = 1010;

int dp[MAX_SIZE][MAX_SIZE], last_occurrence[26];
string s, t;

int damerau_levenshtein_distance() {
  int m = s.size(), n = t.size();
  for (int i = 0; i <= m; i++)
    for (int j = 0; j <= n; j++)
      dp[i][j] = (i == 0 ? j : (j == 0 ? i : 1e9));
  
  memset(last_occurrence, -1, sizeof(last_occurrence));

  for (int i = 1; i <= m; i++) {
    int prev = -1;
    for (int j = 1; j <= n; j++) {
      int k = last_occurrence[t[j - 1] - 'a'];
      int l = prev;
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1];
        prev = j;
      } else
        dp[i][j] = min({dp[i][j], dp[i - 1][j - 1] + 1, dp[i][j - 1] + 1, dp[i - 1][j] + 1});
      if (k >= 0 && l >= 0)
        dp[i][j] = min(dp[i][j], dp[k - 1][l - 1] + (i - k - 1) + (j - l - 1) + 1);
    }
    last_occurrence[s[i - 1] - 'a'] = i;
  }

  return dp[m][n];
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, d;
  while (cin >> n && n != 0) {
    cin >> d;
    vector<string> name(n);
    for (string &str : name) cin >> str;
    sort(name.begin(), name.end());

    int count = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        s = name[i], t = name[j];
        if (damerau_levenshtein_distance() <= d) {
          cout << name[i] << "," << name[j] << "\n";
          count++;
        }
      }
    }
    cout << count << "\n";
  }

  return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 3368
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif
const int inf = 1e9;
const int64_t inf64 = 1e18;
const double eps = 1e-9;
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
int damerau_levenshtein_distance(const string &a, const string &b,
                                 const int alphabet_size) {
  unordered_map<int, int> da;
  map<int, unordered_map<int, int>> d;
  int maxdist = a.size() + b.size();
  d[-1][-1] = maxdist;
  for (int i = 0; i <= a.size(); ++i) {
    d[i][-1] = maxdist;
    d[i][0] = i;
  }
  for (int i = 0; i <= b.size(); ++i) {
    d[-1][i] = maxdist;
    d[0][i] = i;
  }
  for (int i = 1; i <= a.size(); ++i) {
    int db = 0;
    for (int j = 1; j <= b.size(); ++j) {
      int k = da[b[j - 1]], l = db, cost;
      if (a[i - 1] == b[j - 1]) {
        cost = 0;
        db = j;
      } else
        cost = 1;
      d[i][j] = min({d[i - 1][j - 1] + cost, d[i][j - 1] + 1, d[i - 1][j] + 1,
                     d[k - 1][l - 1] + (i - k - 1) + 1 + (j - l - 1)});
    }
    da[a[i - 1]] = i;
  }
  return d[a.size()][b.size()];
}
void solve(int n) {
  int d;
  vector<string> name(n);
  cin >> d;
  rep(i, 0, n) cin >> name[i];
  sort(name.begin(), name.end());
  int count = 0;
  rep(i, 0, n) {
    rep(j, i + 1, n) {
      if (damerau_levenshtein_distance(name[i], name[j], 26) > d)
        continue;
      ++count;
      cout << name[i] << "," << name[j] << endl;
    }
  }
  cout << count << endl;
}
int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  for (;;) {
    int n;
    cin >> n;
    if (!n)
      break;
    solve(n);
  }
  return 0;
}
accuracy: 1
time_ms: 2530
memory_kb: 3192
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif
const int inf = 1e9;
const int64_t inf64 = 1e18;
const double eps = 1e-9;
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
int damerau_levenshtein_distance(const string &a, const string &b,
                                 const int alphabet_size) {
  unordered_map<int, int> da;
  unordered_map<int, unordered_map<int, int>> d;
  int maxdist = a.size() + b.size();
  d[-1][-1] = maxdist;
  for (int i = 0; i <= a.size(); ++i) {
    d[i][-1] = maxdist;
    d[i][0] = i;
  }
  for (int i = 0; i <= b.size(); ++i) {
    d[-1][i] = maxdist;
    d[0][i] = i;
  }
  for (int i = 1; i <= a.size(); ++i) {
    int db = 0;
    for (int j = 1; j <= b.size(); ++j) {
      int k = da[b[j - 1]], l = db, cost;
      if (a[i - 1] == b[j - 1]) {
        cost = 0;
        db = j;
      } else
        cost = 1;
      d[i][j] = min({d[i - 1][j - 1] + cost, d[i][j - 1] + 1, d[i - 1][j] + 1,
                     d[k - 1][l - 1] + (i - k - 1) + 1 + (j - l - 1)});
    }
    da[a[i - 1]] = i;
  }
  return d[a.size()][b.size()];
}
void solve(int n) {
  int d;
  vector<string> name(n);
  cin >> d;
  rep(i, 0, n) cin >> name[i];
  sort(name.begin(), name.end());
  int count = 0;
  rep(i, 0, n) {
    rep(j, i + 1, n) {
      if (damerau_levenshtein_distance(name[i], name[j], 26) > d)
        continue;
      ++count;
      cout << name[i] << "," << name[j] << endl;
    }
  }
  cout << count << endl;
}
int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  for (;;) {
    int n;
    cin >> n;
    if (!n)
      break;
    solve(n);
  }
  return 0;
}
accuracy: 1
time_ms: 2900
memory_kb: 3356
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif
const int inf = 1e9;
const int64_t inf64 = 1e18;
const double eps = 1e-9;
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
int damerau_levenshtein_distance(const string &a, const string &b,
                                 const int alphabet_size) {
  map<int, int> da;
  map<int, map<int, int>> d;
  int maxdist = a.size() + b.size();
  d[-1][-1] = maxdist;
  for (int i = 0; i <= a.size(); ++i) {
    d[i][-1] = maxdist;
    d[i][0] = i;
  }
  for (int i = 0; i <= b.size(); ++i) {
    d[-1][i] = maxdist;
    d[0][i] = i;
  }
  for (int i = 1; i <= a.size(); ++i) {
    int db = 0;
    for (int j = 1; j <= b.size(); ++j) {
      int k = da[b[j - 1]], l = db, cost;
      if (a[i - 1] == b[j - 1]) {
        cost = 0;
        db = j;
      } else
        cost = 1;
      d[i][j] = min({d[i - 1][j - 1] + cost, d[i][j - 1] + 1, d[i - 1][j] + 1,
                     d[k - 1][l - 1] + (i - k - 1) + 1 + (j - l - 1)});
    }
    da[a[i - 1]] = i;
  }
  return d[a.size()][b.size()];
}
void solve(int n) {
  int d;
  vector<string> name(n);
  cin >> d;
  rep(i, 0, n) cin >> name[i];
  sort(name.begin(), name.end());
  int count = 0;
  rep(i, 0, n) {
    rep(j, i + 1, n) {
      if (damerau_levenshtein_distance(name[i], name[j], 26) > d)
        continue;
      ++count;
      cout << name[i] << "," << name[j] << endl;
    }
  }
  cout << count << endl;
}
int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  for (;;) {
    int n;
    cin >> n;
    if (!n)
      break;
    solve(n);
  }
  return 0;
}
accuracy: 1
time_ms: 2690
memory_kb: 3176
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
bool check(string a, string b, int d) {
  if (a.size() < b.size())
    swap(a, b);
  if (a == b)
    return 1;
  if (d == 0 || a.empty())
    return 0;
  for (int i = 0; i < a.size(); i++) {
    string t = a;
    t.erase(t.begin() + i);
    if (check(t, b, d - 1))
      return 1;
  }
  if (a.size() != b.size())
    return 0;
  for (int i = 0; i < (int)a.size() - 1; i++) {
    swap(a[i], a[i + 1]);
    if (check(a, b, d - 1))
      return 1;
    swap(a[i], a[i + 1]);
  }
  for (int i = 0; i < b.size(); i++) {
    char ch = a[i];
    if (a[i] != b[i])
      a[i] = b[i];
    if (check(a, b, d - 1))
      return 1;
    a[i] = ch;
  }
  return 0;
}
int main() {
  int n, d;
  while (cin >> n, n) {
    cin >> d;
    string str[201];
    for (int i = 0; i < n; i++)
      cin >> str[i];
    sort(str, str + n);
    int cnt = 0;
    for (int i = 0; i < n; i++)
      for (int j = i + 1; j < n; j++)
        if (check(str[i], str[j], d))
          cout << str[i] << "," << str[j] << endl, cnt++;
    cout << cnt << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 3480
memory_kb: 3184
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
bool check(string a, string b, int d) {
  if (a.size() < b.size())
    swap(a, b);
  if (a == b)
    return 1;
  if (d == 0)
    return 0;
  for (int i = 0; i < a.size(); i++) {
    string t = a;
    t.erase(t.begin() + i);
    if (check(t, b, d - 1))
      return 1;
  }
  if (a.size() != b.size())
    return 0;
  for (int i = 0; i < (int)a.size() - 1; i++) {
    if (a[i] == a[i + 1] || a[i + 1] != b[i])
      continue;
    swap(a[i], a[i + 1]);
    if (check(a, b, d - 1))
      return 1;
    swap(a[i], a[i + 1]);
  }
  int cnt = 0;
  for (int i = 0; i < a.size(); i++)
    cnt += a[i] != b[i];
  return cnt <= d;
}
int main() {
  int n, d;
  while (cin >> n, n) {
    cin >> d;
    string str[201];
    for (int i = 0; i < n; i++)
      cin >> str[i];
    sort(str, str + n);
    int cnt = 0;
    for (int i = 0; i < n; i++)
      for (int j = i + 1; j < n; j++)
        if (check(str[i], str[j], d))
          cout << str[i] << "," << str[j] << endl, cnt++;
    cout << cnt << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 1790
memory_kb: 3200
final_rank: 6

code_snippet: #include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;
int n, d;
string s[222];
set<string> st[222];
bool used[222][222];
set<string>::iterator it, it2;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> n, n) {
    memset(used, 0, sizeof(used));
    cin >> d;
    r(i, n) cin >> s[i];
    sort(s, s + n);
    r(i, n) st[i].clear();
    r(i, n) {
      r(j, s[i].size()) {
        string t = s[i];
        t.erase(t.begin() + j);
        st[i].insert(t);
      }
      r(j, s[i].size() + 1) {
        r(k, 26) {
          string t = s[i];
          t = t.substr(0, j) + (char)(k + 'a') + t.substr(j);
          st[i].insert(t);
        }
      }
      r(j, s[i].size() + 1) {
        r(k, 26) {
          string t = s[i];
          t[j] = (char)(k + 'a');
          st[i].insert(t);
        }
      }
      r(j, s[i].size() - 1) {
        string t = s[i];
        swap(t[j], t[j + 1]);
        st[i].insert(t);
      }
    }
    int ans = 0;
    if (d == 1) {
      r(i, n) for (int j = i + 1; j < n; j++) if (i != j && !used[i][j]) {
        {
          for (it = st[i].begin(); it != st[i].end(); it++) {
            if (s[j] == (*it)) {
              used[i][j] = used[j][i] = 1;
              cout << s[i] << ',' << s[j] << endl;
              ans++;
              break;
            }
          }
        }
      }
    } else {
      r(i, n) for (int j = i + 1; j < n; j++) if (i != j && !used[i][j]) {
        for (it = st[i].begin(); it != st[i].end(); it++) {
          if (st[j].count(*it)) {
            used[i][j] = used[j][i] = 1;
            cout << s[i] << ',' << s[j] << endl;
            ans++;
            break;
          }
        }
      }
    }
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 5020
memory_kb: 17596
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
ull B = 10000007;
int n, d, len;
char s[200][17];
ull t[200];
int G[200][200];
bool f[200][200];
map<ull, bool> mp[200];
struct data {
  int fi, se;
  bool operator<(const data &d) const {
    return (G[fi][d.fi] != 0 ? G[fi][d.fi] < 0 : G[se][d.se] < 0);
  }
};
vector<data> v;
ull Deleting(int x, char *ch) {
  ull res = 0;
  for (int i = 0; i < len; i++)
    if (i != x)
      res = res * B + ch[i];
  return res;
}
ull Inserting(int x, char y, char *ch) {
  ull res = 0;
  for (int i = 0; i <= len; i++) {
    if (i == x)
      res = res * B + y;
    if (i < len)
      res = res * B + ch[i];
  }
  return res;
}
ull Replacing(int x, char y, char *ch) {
  ull res = 0;
  for (int i = 0; i < len; i++) {
    if (i == x)
      res = res * B + y;
    else
      res = res * B + ch[i];
  }
  return res;
}
ull Swapping(int x, char *ch) {
  ull res = 0;
  for (int i = 0; i < len; i++) {
    if (i == x)
      res = res * B + ch[i + 1];
    else if (i == x + 1)
      res = res * B + ch[x];
    else
      res = res * B + ch[i];
  }
  return res;
}
void init() {
  for (int i = 0; i < 200; i++) {
    mp[i].clear();
  }
  v.clear();
  memset(f, false, sizeof(f));
}
int main() {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    init();
    scanf("%d", &d);
    for (int i = 0; i < n; i++) {
      scanf("%s", s[i]);
      len = strlen(s[i]);
      t[i] = Swapping(-10, s[i]);
      for (int j = 0; j < len; j++) {
        mp[i][Deleting(j, s[i])] = true;
      }
      for (int j = 0; j <= len; j++) {
        for (char k = 'a'; k <= 'z'; k++) {
          mp[i][Inserting(j, k, s[i])] = true;
        }
      }
      for (int j = 0; j < len; j++) {
        for (char k = 'a'; k <= 'z'; k++) {
          if (s[i][j] != k) {
            mp[i][Replacing(j, k, s[i])] = true;
          }
        }
      }
      for (int j = 0; j + 1 < len; j++) {
        mp[i][Swapping(j, s[i])] = true;
      }
    }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        G[i][j] = strcmp(s[i], s[j]);
    for (int i = 1; i < n; i++) {
      for (int j = 0; j < i; j++) {
        int a = i, b = j;
        if (G[a][b] > 0)
          swap(a, b);
        if (mp[a].count(t[b]) && !f[a][b]) {
          v.push_back((data){a, b});
          f[a][b] = true;
        }
        if (d == 1)
          continue;
        map<ull, bool>::iterator it;
        for (it = mp[b].begin(); it != mp[b].end(); it++) {
          if (mp[a].count(it->first) && !f[a][b]) {
            v.push_back((data){a, b});
            f[a][b] = true;
          }
        }
      }
    }
    sort(v.begin(), v.end());
    for (int i = 0; i < (int)v.size(); i++) {
      printf("%s,%s\n", s[v[i].fi], s[v[i].se]);
    }
    printf("%d\n", (int)v.size());
  }
  return 0;
}
accuracy: 1
time_ms: 3370
memory_kb: 12632
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
ull B = 10000007;
int n, d, len;
char s[200][17];
ull t[200];
int G[200][200];
bool f[200][200];
map<ull, bool> mp[200];
vector<ull> L[200];
struct data {
  int fi, se;
  bool operator<(const data &d) const {
    return (G[fi][d.fi] != 0 ? G[fi][d.fi] < 0 : G[se][d.se] < 0);
  }
};
vector<data> v;
ull Deleting(int x, char *ch) {
  ull res = 0;
  for (int i = 0; i < len; i++)
    if (i != x)
      res = res * B + ch[i];
  return res;
}
ull Inserting(int x, char y, char *ch) {
  ull res = 0;
  for (int i = 0; i <= len; i++) {
    if (i == x)
      res = res * B + y;
    if (i < len)
      res = res * B + ch[i];
  }
  return res;
}
ull Replacing(int x, char y, char *ch) {
  ull res = 0;
  for (int i = 0; i < len; i++) {
    if (i == x)
      res = res * B + y;
    else
      res = res * B + ch[i];
  }
  return res;
}
ull Swapping(int x, char *ch) {
  ull res = 0;
  for (int i = 0; i < len; i++) {
    if (i == x)
      res = res * B + ch[i + 1];
    else if (i == x + 1)
      res = res * B + ch[x];
    else
      res = res * B + ch[i];
  }
  return res;
}
void init() {
  for (int i = 0; i < 200; i++) {
    mp[i].clear();
    L[i].clear();
  }
  v.clear();
  memset(f, false, sizeof(f));
}
int main() {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    init();
    scanf("%d", &d);
    for (int i = 0; i < n; i++) {
      scanf("%s", s[i]);
      len = strlen(s[i]);
      t[i] = Swapping(-10, s[i]);
      ull key;
      for (int j = 0; j < len; j++) {
        key = Deleting(j, s[i]);
        mp[i][key] = true;
        L[i].push_back(key);
      }
      for (int j = 0; j <= len; j++) {
        for (char k = 'a'; k <= 'z'; k++) {
          key = Inserting(j, k, s[i]);
          mp[i][key] = true;
          L[i].push_back(key);
        }
      }
      for (int j = 0; j < len; j++) {
        for (char k = 'a'; k <= 'z'; k++) {
          if (s[i][j] != k) {
            key = Replacing(j, k, s[i]);
            mp[i][key] = true;
            L[i].push_back(key);
          }
        }
      }
      for (int j = 0; j + 1 < len; j++) {
        key = Swapping(j, s[i]);
        mp[i][key] = true;
        L[i].push_back(key);
      }
    }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        G[i][j] = strcmp(s[i], s[j]);
    for (int i = 1; i < n; i++) {
      for (int j = 0; j < i; j++) {
        int a = i, b = j;
        if (G[a][b] > 0)
          swap(a, b);
        if (mp[a].count(t[b]) && !f[a][b]) {
          v.push_back((data){a, b});
          f[a][b] = true;
        }
        if (d == 1)
          continue;
        vector<ull>::iterator it;
        for (it = L[b].begin(); it != L[b].end(); it++) {
          if (mp[a].count(*it) && !f[a][b]) {
            v.push_back((data){a, b});
            f[a][b] = true;
          }
        }
      }
    }
    sort(v.begin(), v.end());
    for (int i = 0; i < (int)v.size(); i++) {
      printf("%s,%s\n", s[v[i].fi], s[v[i].se]);
    }
    printf("%d\n", (int)v.size());
  }
  return 0;
}
accuracy: 1
time_ms: 2560
memory_kb: 14216
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
bool check(string a, string b, int d) {
  if (a.size() < b.size())
    swap(a, b);
  if (a == b)
    return 1;
  int A = a.size(), B = b.size();
  if (d == 0 || A - B > d)
    return 0;
  for (int i = 0; i < A; i++) {
    char ch = a[i];
    a.erase(a.begin() + i);
    if (check(a, b, d - 1))
      return 1;
    a.insert(a.begin() + i, ch);
  }
  if (A != B)
    return 0;
  for (int i = 0; i < A - 1; i++) {
    if (a[i] == a[i + 1] || a[i + 1] != b[i] || a[i] != b[i + 1])
      continue;
    swap(a[i], a[i + 1]);
    if (check(a, b, d - 1))
      return 1;
    swap(a[i], a[i + 1]);
  }
  int cnt = 0;
  for (int i = 0; i < A; i++)
    cnt += a[i] != b[i];
  return cnt <= d;
}
int main() {
  int n, d;
  while (cin >> n, n) {
    cin >> d;
    string str[201];
    for (int i = 0; i < n; i++)
      cin >> str[i];
    sort(str, str + n);
    int cnt = 0;
    for (int i = 0; i < n; i++)
      for (int j = i + 1; j < n; j++)
        if (check(str[i], str[j], d))
          cout << str[i] << "," << str[j] << endl, cnt++;
    cout << cnt << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 880
memory_kb: 3132
final_rank: 4

code_snippet: #include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define ALL(C) (C).begin(), (C).end()
#define FOR(it, o)                                                             \
  for (__typeof((o).begin()) it = (o).begin(); it != (o).end(); it++)
#define pb push_back
#define mp make_pair
typedef unsigned long long ull;
typedef pair<string, string> pss;
typedef vector<pss> vpss;
const ull tmp = 10007;
ull calchash(string &in) {
  ull ret = 0;
  rep(i, in.size()) { ret = ret * tmp + in[i]; }
  return ret;
}
bool isok[200][200];
void ed1(vector<string> &in, set<ull> *S) {
  rep(i, in.size()) {
    REP(j, i + 1, in.size()) {
      if (S[j].find(calchash(in[i])) != S[j].end()) {
        isok[i][j] = true;
      }
    }
  }
}
void ed2(vector<string> &in, set<ull> *S) {
  rep(i, in.size()) {
    REP(j, i + 1, in.size()) {
      if (isok[i][j])
        continue;
      FOR(it, S[i]) {
        if (S[j].find(*it) != S[j].end()) {
          isok[i][j] = true;
          break;
        }
      }
    }
  }
}
void del_string(string &in, set<ull> &S) {
  rep(i, in.size()) {
    string tmp = in;
    tmp.erase(tmp.begin() + i);
    S.insert(calchash(tmp));
  }
}
void insert_string(string &in, set<ull> &S) {
  rep(i, in.size() + 1) {
    rep(j, 26) {
      string tmp = in.substr(0, i);
      tmp += string(1, 'a' + j);
      tmp += in.substr(i);
      S.insert(calchash(tmp));
    }
  }
}
void replace_string(string &in, set<ull> &S) {
  rep(i, in.size()) {
    char tmp = in[i];
    rep(j, 26) {
      in[i] = 'a' + j;
      S.insert(calchash(in));
    }
    in[i] = tmp;
  }
}
void swap_string(string &in, set<ull> &S) {
  rep(i, (int)in.size() - 1) {
    swap(in[i], in[i + 1]);
    S.insert(calchash(in));
    swap(in[i], in[i + 1]);
  }
}
void make_string(vector<string> &in, set<ull> *S) {
  rep(i, in.size()) {
    del_string(in[i], S[i]);
    insert_string(in[i], S[i]);
    replace_string(in[i], S[i]);
    swap_string(in[i], S[i]);
  }
}
void solve(vector<string> in, set<ull> *S, int d) {
  int n = in.size();
  make_string(in, S);
  ed1(in, S);
  if (d == 2)
    ed2(in, S);
  int cnt = 0;
  rep(i, n) {
    rep(j, n) if (isok[i][j]) cnt++, cout << in[i] << "," << in[j] << endl;
  }
  cout << cnt << endl;
}
void test() {
  string in = "test";
  set<ull> S;
  insert_string(in, S);
}
main() {
  int n, d;
  while (cin >> n >> d && n) {
    vector<string> in;
    rep(i, n) rep(j, n) isok[i][j] = false;
    set<ull> S[n];
    rep(i, n) {
      string tmp;
      cin >> tmp;
      in.pb(tmp);
    }
    sort(ALL(in));
    rep(i, in.size()) S[i].insert(calchash(in[i]));
    solve(in, S, d);
  }
}
accuracy: 1
time_ms: 2610
memory_kb: 10044
final_rank: 14

code_snippet: #include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define ALL(C) (C).begin(), (C).end()
#define FOR(it, o)                                                             \
  for (__typeof((o).begin()) it = (o).begin(); it != (o).end(); it++)
#define pb push_back
#define mp make_pair
typedef unsigned long long ull;
typedef pair<string, string> pss;
typedef vector<pss> vpss;
const ull tmp = 10007;
ull calchash(string &in) {
  ull ret = 0;
  rep(i, (int)in.size()) { ret = ret * tmp + in[i]; }
  return ret;
}
bool isok[200][200];
int ed[200][200];
int cost[20][20];
int edit_distance(string tr, string tc) {
  string r, c;
  r = ' ';
  c = ' ';
  rep(i, tr.size()) r += tr[i];
  rep(i, tc.size()) c += tc[i];
  rep(i, r.size()) rep(j, c.size()) cost[i][j] = (1 << 21);
  rep(i, r.size()) cost[i][0] = i;
  rep(i, c.size()) cost[0][i] = i;
  REP(i, 1, r.size()) {
    REP(j, 1, c.size()) {
      int tmp = r[i] == c[j] ? 0 : 1;
      int opt[3];
      opt[0] = cost[i - 1][j - 1] + tmp;
      opt[1] = cost[i - 1][j] + 1;
      opt[2] = cost[i][j - 1] + 1;
      rep(k, 3) cost[i][j] = min(cost[i][j], opt[k]);
    }
  }
  return cost[r.size() - 1][c.size() - 1];
}
void ed1(vector<string> &in, set<ull> S[]) {
  rep(i, (int)in.size()) {
    REP(j, i + 1, (int)in.size()) {
      if (S[j].find(calchash(in[i])) != S[j].end()) {
        isok[i][j] = true;
      }
    }
  }
}
void ed2(vector<string> &in, set<ull> S[]) {
  rep(i, (int)in.size()) {
    REP(j, i + 1, (int)in.size()) {
      if (isok[i][j] || ed[i][j] > 4)
        continue;
      FOR(it, S[i]) {
        if (S[j].find(*it) != S[j].end()) {
          isok[i][j] = true;
          break;
        }
      }
    }
  }
}
void del_string(string &in, set<ull> &S) {
  rep(i, (int)in.size()) {
    string tmp = in;
    tmp.erase(tmp.begin() + i);
    S.insert(calchash(tmp));
  }
}
void insert_string(string &in, set<ull> &S) {
  rep(i, (int)in.size() + 1) {
    rep(j, 26) {
      string tmp = in.substr(0, i);
      tmp += 'a' + j;
      string tmp2 = in.substr(i);
      rep(k, tmp2.size()) tmp += (char)tmp2[k];
      S.insert(calchash(tmp));
    }
  }
}
void replace_string(string &in, set<ull> &S) {
  rep(i, (int)in.size()) {
    char tmp = in[i];
    rep(j, 26) {
      in[i] = 'a' + j;
      S.insert(calchash(in));
    }
    in[i] = tmp;
  }
}
void swap_string(string &in, set<ull> &S) {
  rep(i, (int)in.size() - 1) {
    swap(in[i], in[i + 1]);
    S.insert(calchash(in));
    swap(in[i], in[i + 1]);
  }
}
void make_string(vector<string> &in, set<ull> S[]) {
  rep(i, (int)in.size()) {
    del_string(in[i], S[i]);
    insert_string(in[i], S[i]);
    replace_string(in[i], S[i]);
    swap_string(in[i], S[i]);
  }
}
void solve(vector<string> in, set<ull> S[], int d) {
  int n = (int)in.size();
  rep(i, n) {
    REP(j, i + 1, n) {
      ed[i][j] = edit_distance(in[i], in[j]);
      if (ed[i][j] <= d)
        isok[i][j] = true;
    }
  }
  make_string(in, S);
  ed1(in, S);
  if (d == 2)
    ed2(in, S);
  int cnt = 0;
  rep(i, n) {
    rep(j, n) if (isok[i][j]) cnt++, cout << in[i] << "," << in[j] << endl;
  }
  cout << cnt << endl;
}
void test() {
  string in = "test";
  set<ull> S;
  insert_string(in, S);
}
main() {
  int n, d;
  while (cin >> n >> d && n) {
    vector<string> in;
    rep(i, n) rep(j, n) isok[i][j] = false, ed[i][j] = (1 << 21);
    set<ull> S[n];
    rep(i, n) {
      string tmp;
      cin >> tmp;
      in.pb(tmp);
    }
    sort(ALL(in));
    rep(i, (int)in.size()) S[i].insert(calchash(in[i]));
    solve(in, S, d);
  }
}
accuracy: 1
time_ms: 430
memory_kb: 10112
final_rank: 5