Problem:
Problem F:
Old Memories
In 4272 A.D., Master of Programming Literature, Dr. Isaac Cornell Panther-Carol,
who has miraculously survived through the three World Computer Virus Wars and reached 90 years old this year,
won a Nobel Prize for Literature.
Media reported every detail of his life.
However, there was one thing they could not report — that is, an essay written by him when he was an elementary school boy.
Although he had a copy and was happy to let them see it, 
the biggest problem was that his copy of the essay was infected by a computer virus several times during the World Computer Virus War III
and therefore the computer virus could have altered the text.
Further investigation showed that his copy was altered indeed. Why could we know that?
More than 80 years ago his classmates transferred the original essay to their brain before infection.
With the advent of Solid State Brain, one can now retain a text perfectly over centuries once it is transferred to his or her brain.
No one could remember the entire text due to the limited capacity,
but we managed to retrieve a part of the text from the brain of one of his classmates; sadly, it did not match perfectly to the copy at hand.
It would not have happened without virus infection.
At the moment, what we know about the computer virus is that each time the virus infects an essay it does one of the following:
the virus inserts one random character to a random position in the text. (e.g., "ABCD" → "ABCZD")
the virus picks up one character in the text randomly, and then changes it into another character. (e.g., "ABCD" → "ABXD")
the virus picks up one character in the text randomly, and then removes it from the text. (e.g., "ABCD" → "ACD")
You also know the maximum number of times the computer virus infected the copy, because you could deduce it from the amount of the intrusion log.
Fortunately, most of his classmates seemed to remember at least one part of the essay (we call it a
piece
hereafter).
Considering all evidences together, the original essay might be reconstructed.
You, as a journalist and computer scientist, would like to reconstruct the original essay by writing a computer program to calculate the possible original text(s) that fits to the given pieces and the altered copy at hand.
Input
The input consists of multiple datasets. The number of datasets is no more than 100.
Each dataset is formatted as follows:
d n
the altered text
piece
1
piece
2
...
piece
n
The first line of a dataset contains two positive integers
d
(
d
≤ 2) and
n
(
n
≤ 30),
where
d
is the maximum number of times the virus infected the copy and
n
is the number of
pieces
.
The second line is the text of the altered copy at hand. We call it
the altered text
hereafter.
The length of the altered text is less than or equal to 40 characters.
The following
n
lines are
pieces
, each of which is a part of the original essay remembered by one of his classmates.
Each
piece
has at least 13 characters but no more than 20 characters
...(truncated)

Submitted solutions:

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int D, N;
long long val1[49], pow1[49];
long long val2[49], pow2[49];
string S;
string T[39];
string U = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.";
string Cand[1500009];

bool check(string str) {
	val1[0] = 0;
	val2[0] = 0;
	for (int i = 0; i < str.size(); i++) {
		long long chr = str[i];
		val1[i + 1] = (217LL * val1[i] + chr);
	}

	// Brute Force
	vector<int> used(str.size() + 1, 0);
	for (int i = 0; i < N; i++) {
		long long hash_val1 = 0;
		for (int j = 0; j < T[i].size(); j++) {
			long long chr = T[i][j];
			hash_val1 = (217LL * hash_val1 + chr);
		}
		for (int j = 0; j < (int)str.size() - (int)T[i].size() + 1; j++) {
			long long ret1 = (val1[j + T[i].size()] - pow1[T[i].size()] * val1[j]);
			if (ret1 != hash_val1) continue;
			used[j] += 1;
			used[j + T[i].size()] -= 1;
		}
	}

	// Final
	for (int i = 1; i <= str.size(); i++) used[i] += used[i - 1];
	for (int i = 0; i < str.size(); i++) {
		if (used[i] == 0) return false;
	}
	return true;
}

// ================================================================================= Helper Function =================================================================================
// 0: Insert
// 1: Change
// 2: Delete
int Match(string s1, string s2, vector<int> vec) {
	s1 += "$$"; s2 += "$$";
	int cur = 0, op = 0, Last1 = 0, Last2 = 0;
	while (cur < max(s1.size(), s2.size()) - 2 && cur < min(s1.size(), s2.size()) - 1) {
		if (s1[cur] == s2[cur]) { cur += 1; continue; }
		if (op == vec.size()) return -1;
		Last1 = cur;
		string new_s1 = "";
		if (vec[op] == 0) { new_s1 += s1.substr(0, cur); new_s1 += s2[cur]; new_s1 += s1.substr(cur + 0, s1.size() - (cur + 0)); Last2 = (int)s2[cur]; cur += 1; }
		if (vec[op] == 1) { new_s1 += s1.substr(0, cur); new_s1 += s2[cur]; new_s1 += s1.substr(cur + 1, s1.size() - (cur + 1)); Last2 = (int)s2[cur]; cur += 1; }
		if (vec[op] == 2) { new_s1 += s1.substr(0, cur); new_s1 += s1.substr(cur + 1, s1.size() - (cur + 1)); }
		s1 = new_s1;
		op += 1;
	}
	if (cur + 2 != s1.size()) return -1;
	if (cur + 2 != s2.size()) return -1;
	if (op != vec.size()) return -1;
	return Last1 * 1000 + Last2;
}

vector<string> initial_calculate2(string A, vector<int> vec) {
	int Len = A.size();
	for (int i = 0; i < vec.size(); i++) { if (vec[i] == 0) Len -= 1; }
	for (int i = 0; i < vec.size(); i++) { if (vec[i] == 2) Len += 1; }
	vector<string> Return;
	for (int i = 0; i <= (int)S.size() - Len; i++) {
		string target = S.substr(i, Len);
		if (target == A) continue;
		if (Match(target, A, vec) != -1) {
			string result = S.substr(0, i) + A + S.substr(i + Len, S.size() - (i + Len));
			Return.push_back(result);
		}
	}
	return Return;
}

string Mutate(string Moto, pair<int, int> Mutate1, pair<int, int> Mutate2) {
	// First Try
	int u1 = Mutate1.first / 1000;
	char v1 = Mutate1.first % 1000;
	if (Mutate1.second == 0) { string new_str = Moto.substr(0, u1); new_str += v1; new_str += Moto.substr(u1 + 0, Moto.size() - (u1 + 0)); Moto = new_str; }
	if (Mutate1.second == 1) { string new_str = Moto.substr(0, u1); new_str += v1; new_str += Moto.substr(u1 + 1, Moto.size() - (u1 + 1)); Moto = new_str; }
	if (Mutate1.second == 2) { string new_str = Moto.substr(0, u1); new_str += Moto.substr(u1 + 1, Moto.size() - (u1 + 1)); Moto = new_str; }

	// Second Try
	if (Mutate2.first != -1) {
		int u2 = Mutate2.first / 1000;
		char v2 = Mutate2.first % 1000;
		if (Mutate2.second == 1 && u2 == Moto.size()) return S;
		if (Mutate2.second == 2 && u2 == Moto.size()) return S;
		if (Mutate2.second == 0) { string new_str = Moto.substr(0, u2); new_str += v2; new_str += Moto.substr(u2 + 0, Moto.size() - (u2 + 0)); Moto = new_str; }
		if (Mutate2.second == 1) { string new_str = Moto.substr(0, u2); new_str += v2; new_str += Moto.substr(u2 + 1, Moto.size() - (u2 + 1)); Moto = new_str; }
		if (Mutate2.second == 2) { string new_str = Moto.substr(0, u2); new_str += Moto.substr(u2 + 1, Moto.size() - (u2 + 1)); Moto = new_str; }
	}
	return Moto;
}

// ================================================================================= Main Part =================================================================================
int main() {
	pow1[0] = 1; for (int i = 1; i <= 48; i++) pow1[i] = (217LL * pow1[i - 1]);

	while (true) {
		// Step 1. Input
		cin >> D >> N; if (D + N == 0) break;
		cin >> S;
		for (int i = 0; i < N; i++) cin >> T[i];

		// Step 2. Brute Force (2 Mutations)
		int Count = 0;
		Cand[Count] = S; Count += 1;
		if (D == 2) {
			for (int i = 0; i < N; i++) {
				for (int j = 0; j < 9; j++) {
					vector<string> ret = initial_calculate2(T[i], vector<int>{j / 3, j % 3});
					for (string k : ret) {
						Cand[Count] = k; Count += 1;
					}
				}
			}
		}

		// Step 3. Brute Force (1 Mutations / Phase 1)
		vector<pair<int, int>> Place;
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < 3; j++) {
				int Len = T[i].size(); if (j == 0) Len -= 1; if (j == 2) Len += 1;
				for (int k = 0; k <= (int)S.size() - Len; k++) {
					string target = S.substr(k, Len);
					int ret = Match(target, T[i], vector<int>{j});
					if (ret == -1) continue;
					Place.push_back(make_pair(ret + k * 1000, j));
				}
			}
		}
		sort(Place.begin(), Place.end());
		Place.erase(unique(Place.begin(), Place.end()), Place.end());

		// Step 4. Brute Force (1 Mutations / Phase 2)
		for (int i = 0; i < Place.size(); i++) {
			if (D == 2) {
				for (int j = 0; j < Place.size(); j++) {
					if (Place[i].first < Place[j].first) continue;
					string nex = Mutate(S, Place[i], Place[j]);
					Cand[Count] = nex; Count += 1;
				}
			}
			string nex = Mutate(S, Place[i], make_pair(-1, -1));
			Cand[Count] = nex; Count += 1;
		}

		// Step 5. Check
		sort(Cand, Cand + Count);
		vector<string> Answer;
		for (int i = 0; i < Count; i++) {
			if (i >= 1 && Cand[i] == Cand[i - 1]) continue;
			string str = Cand[i];
			if (check(str) == true) Answer.push_back(str);
		}

		// Step 6. Output
		cout << Answer.size() << endl;
		if (Answer.size() <= 5) {
			for (string str : Answer) cout << str << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 3830
memory_kb: 129860
final_rank: 20

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



#define SIZE 45

struct Edge{
	Edge(){

		to = 0;
		loc = 0;
	}
	Edge(int arg_to,int arg_loc){
		to = arg_to;
		loc = arg_loc;
	}
	int to,loc;
};

int DIST,N,len_T,len_P;
int num_ans;
int dp[SIZE][SIZE];
string T;
vector<string> P,ANS;
map<string,bool> MAP;
vector<Edge> G[SIZE];


bool is_OK(string line,int head,int tail){

	int cost;
	int minimum;

	for(int row = head; row <= tail; row++){

		minimum = BIG_NUM;

		for(int col = max(1,row-DIST); col <= min(len_T,row+DIST); col++){
			if(T[col-1] == line[row-1]){
				cost = 0;
			}else{
				cost = 1;
			}
			dp[row][col] = min(dp[row-1][col-1]+cost,min(dp[row-1][col]+1,dp[row][col-1]+1));
			minimum = min(minimum,dp[row][col]);
		}
		if(minimum > DIST){

			return false;
		}
	}
	return true;
}

void recursive(string line,int index){

	auto at = MAP.find(line);
	if(at != MAP.end())return;

	MAP[line] = true;

	if(dp[line.length()][len_T] <= DIST){

		num_ans++;
		if(num_ans <= 5){

			ANS.push_back(line);
		}
	}

	for(int i = 0; i < G[index].size(); i++){
		int next = G[index][i].to;
		int loc = G[index][i].loc;

		if(line.length()+(len_P-loc) > len_T+DIST){

			break;
		}
		string next_line = line+P[next].substr(loc);

		if(is_OK(next_line,line.length()+1,next_line.length())){

			recursive(next_line,next);
		}
	}
}

void func(){

	MAP.clear();
	P.clear();
	ANS.clear();
	for(int i = 0; i < N; i++){

		G[i].clear();
	}

	cin >> T;
	len_T = T.length();

	for(int loop = 0; loop < N; loop++){

		string tmp_str;
		cin >> tmp_str;
		P.push_back(tmp_str);
	}
	len_P = P[0].length();

	sort(P.begin(),P.end());
	P.erase(unique(P.begin(),P.end()),P.end());

	bool FLG;

	for(int i = 0; i < P.size(); i++){
		string from = P[i];
		for(int len = len_P-1; len >= 0; len--){ //一致長
			for(int k = 0; k < P.size(); k++){
				string to = P[k];

				FLG = true;
				for(int k = 0; k < len; k++){
					if(from[len_P-len+k] != to[k]){
						FLG = false;
						break;
					}
				}
				if(FLG){

					G[i].push_back(Edge(k,len));
				}
			}
		}
	}

	for(int row = 0; row <= len_T+DIST; row++){
		for(int col = 0; col <= len_T; col++){

			dp[row][col] = BIG_NUM;
		}
	}

	for(int row = 1; row <= len_T+DIST; row++){
		dp[row][0] = row;
	}

	for(int col = 0; col <= len_T; col++){
		dp[0][col] = col;
	}

	num_ans = 0;

	for(int i = 0; i < P.size(); i++){

		if(is_OK(P[i],1,len_P)){

			recursive(P[i],i);
		}
	}

	printf("%d\n",num_ans);

	if(num_ans <= 5){

		sort(ANS.begin(),ANS.end());

		for(int i = 0; i < ANS.size(); i++){

			printf("%s\n",ANS[i].c_str());
		}
	}
}

int main(){

	while(true){

		scanf("%d %d",&DIST,&N);
		if(DIST == 0 && N == 0)break;

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 1660
memory_kb: 22340
final_rank: 2

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

struct Edge {int from, to, weight;};
using Graph = vector<vector<Edge>>;

int D, N;
string TEXT;
vector<string> PIECE;

set<string> OK;

string text;

int DP[43][43];

bool Levenshtein(auto p) {
  for(auto i = p; i <= text.size(); ++i) {
    auto d = D + 1;
    for(auto j = 1; j <= TEXT.size(); ++j) {
      if(text[i-1] == TEXT[j-1]) DP[i][j] = DP[i-1][j-1];
      else                       DP[i][j] = 1 + min({DP[i-1][j], DP[i][j-1], DP[i-1][j-1]});
      d = min(d, DP[i][j]);
    }
    if(D < d) return false;
  }
  return true;
}

void dfs(const auto& G, auto& used, auto cur) {
  if(TEXT.size() + D < text.size()) return;

  if(!Levenshtein(text.size() - PIECE[cur].size() + 1)) return;

  if(used.count(text)) return;
  used.emplace(text);

  if(DP[text.size()][TEXT.size()] <= D) OK.emplace(text);

  auto dup = text;
  for(auto e: G[cur]) {
    text = dup + PIECE[e.to].substr(e.weight);
    dfs(G, used, e.to);
  }
  text = dup;
}

void solve() {
  Graph G(N);
  for(auto i = 0; i < N; ++i) for(auto j = 0; j < N; ++j) {
    auto lhs = PIECE[i];
    auto rhs = PIECE[j];
    for(auto k = 0; k < lhs.size(); ++k) if(lhs.substr(lhs.size() - k) == rhs.substr(0, k)) G[i].push_back({i, j, k});
  }
 
  unordered_set<string> used;
  for(auto i = 0; i < N; ++i) {
    text = PIECE[i];
    dfs(G, used, i);
  }
}

int main() {
  for(auto i = 0; i < 43; ++i) DP[i][0] = DP[0][i] = i;

  while(cin >> D >> N, D | N) {
    cin >> TEXT;
    PIECE = vector<string>(N);
    for(auto& i: PIECE) cin >> i;
 
    sort(begin(PIECE), end(PIECE));
    PIECE.erase(unique(begin(PIECE), end(PIECE)), end(PIECE));
    N = PIECE.size();
 
    OK.clear();
    solve();
    cout << OK.size() << endl;
    if(OK.size() <= 5) for(auto i: OK) cout << i << endl;
  }
}
accuracy: 1
time_ms: 7390
memory_kb: 26128
final_rank: 17

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

struct Edge {int from, to, weight;};
using Graph = vector<vector<Edge>>;

int D, N;
string TEXT;
vector<string> PIECE;

set<string> OK;

string text;

int DP[45][45];

bool Levenshtein(auto p) {
  for(auto i = p; i <= text.size(); ++i) {
    auto d = D + 1;
    for(auto j = 1; j <= TEXT.size(); ++j) {
      if(text[i-1] == TEXT[j-1]) DP[i][j] = DP[i-1][j-1];
      else                       DP[i][j] = 1 + min({DP[i-1][j], DP[i][j-1], DP[i-1][j-1]});
      d = min(d, DP[i][j]);
    }
    if(D < d) return false;
  }
  return true;
}

void dfs(const auto& G, auto& used, auto cur) {
  if(TEXT.size() + D < text.size()) return;

  if(!Levenshtein(text.size() - PIECE[cur].size() + 1)) return;

  if(used.count(text)) return;
  used.emplace(text);

  if(DP[text.size()][TEXT.size()] <= D) OK.emplace(text);

  auto dup = text;
  for(auto e: G[cur]) {
    text = dup + PIECE[e.to].substr(e.weight);
    dfs(G, used, e.to);
  }
  text = dup;
}

void solve() {
  Graph G(N);
  for(auto i = 0; i < N; ++i) for(auto j = 0; j < N; ++j) {
    auto lhs = PIECE[i];
    auto rhs = PIECE[j];
    for(auto k = 0; k < lhs.size(); ++k) if(lhs.substr(lhs.size() - k) == rhs.substr(0, k)) G[i].push_back({i, j, k});
  }
 
  unordered_set<string> used;
  for(auto i = 0; i < N; ++i) {
    text = PIECE[i];
    dfs(G, used, i);
  }
}

int main() {
  for(auto i = 0; i < 45; ++i) DP[i][0] = DP[0][i] = i;

  while(cin >> D >> N, D | N) {
    cin >> TEXT;
    PIECE = vector<string>(N);
    for(auto& i: PIECE) cin >> i;
 
    sort(begin(PIECE), end(PIECE));
    PIECE.erase(unique(begin(PIECE), end(PIECE)), end(PIECE));
    N = PIECE.size();
 
    OK.clear();
    solve();
    cout << OK.size() << endl;
    if(OK.size() <= 5) for(auto i: OK) cout << i << endl;
  }
}
accuracy: 1
time_ms: 7770
memory_kb: 26260
final_rank: 19

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

struct Edge {int from, to, weight;};
using Graph = vector<vector<Edge>>;

int D, N;
string TEXT;
vector<string> PIECE;

set<string> OK;

string text;

int DP[45][45];

bool Levenshtein(auto b, auto e) {
  for(auto j = b; j <= e; ++j) {
    int d = D + 1;
    for(auto i = 1; i <= TEXT.size(); ++i) {
      if(TEXT[i-1] == text[j-1]) DP[i][j] = DP[i-1][j-1];
      else                       DP[i][j] = 1 + min({DP[i-1][j], DP[i][j-1], DP[i-1][j-1]});
      d = min(d, DP[i][j]);
    }
    if(D < d) return false;
  }
  return true;
}

void dfs(const auto& G, auto& used, auto cur) {
  if(TEXT.size() + D < text.size()) return;

  if(!Levenshtein(text.size() - PIECE[cur].size() + 1, text.size())) return;

  if(used.count(text)) return;
  used.emplace(text);

  if(DP[TEXT.size()][text.size()] <= D) OK.emplace(text);

  auto dup = text;
  for(auto e: G[cur]) {
    text = dup + PIECE[e.to].substr(e.weight);
    dfs(G, used, e.to);
    text = dup;
  }
}

void solve() {
  Graph G(N);
  for(auto i = 0; i < N; ++i) for(auto j = 0; j < N; ++j) {
    auto lhs = PIECE[i];
    auto rhs = PIECE[j];
    for(auto k = 0; k < lhs.size(); ++k) if(lhs.substr(lhs.size() - k) == rhs.substr(0, k)) G[i].push_back({i, j, k});
  }
 
  unordered_set<string> used;
  for(auto i = 0; i < N; ++i) {
    text = PIECE[i];
    dfs(G, used, i);
  }
}

int main() {
  for(auto i = 0; i < 45; ++i) DP[i][0] = DP[0][i] = i;

  while(cin >> D >> N, D | N) {
    cin >> TEXT;
    PIECE = vector<string>(N);
    for(auto& i: PIECE) cin >> i;
 
    sort(begin(PIECE), end(PIECE));
    PIECE.erase(unique(begin(PIECE), end(PIECE)), end(PIECE));
    N = PIECE.size();
 
    OK.clear();
    solve();
    cout << OK.size() << endl;
    if(OK.size() <= 5) for(auto i: OK) cout << i << endl;
  }
}
accuracy: 1
time_ms: 7760
memory_kb: 26228
final_rank: 18

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

using namespace std;

int D, N;
string S;
vector< string > p, pat;
set< string > used;
vector< int > graph[45][45];
int dp[45][45];
string now;
 
bool Levenshtein_Distance(int B, int E) {
  for(int i = B + 1; i <= E; i++) {
    int Dist = 1 << 30;
    for(int j = max(1, i - D); j <= min< int >(S.size(), i + D); j++) {
      dp[i][j] = min(min(dp[i - 1][j], dp[i][j - 1]) + 1, dp[i - 1][j - 1] + (now[i - 1] != S[j - 1]));
      Dist = min(Dist, dp[i][j]);
    }
    if(Dist > D) return(false);
  }
  return(true);
}
  
void rec(int idx)
{
  if(used.count(now)) return;
  used.insert(now);
  if(dp[now.size()][S.size()] <= D) {
    if(pat.size() > 5) pat.push_back("~");
    else pat.push_back(now);
  } 
  int nowSize = now.size();
  for(int i = p[0].size() - 1; i >= 0 && nowSize + p[0].size() - i <= S.size() + D; i--) {
    now += " ";
    for(int j = 0; j < graph[idx][i].size(); j++) {
      bool connect = true;
      for(int k = i; k < p[0].size(); k++) {
        now[nowSize + k - i] = p[graph[idx][i][j]][k];
        if(!Levenshtein_Distance(nowSize + k - i, nowSize + k - i + 1)) {
          connect = false;
          break;
        }
      }
      if(connect) rec(graph[idx][i][j]);
    }
  }
  now.resize(nowSize);
}
  
int main(){
    
  while(cin >> D >> N, D||N) {
    p.clear();
    used.clear();
    pat.clear();
 
    cin >> S;
    for(int i = 0; i < N; i++) {
      string pi;
      cin >> pi;
      p.push_back(pi);
    }
    sort(p.begin(), p.end());
    p.erase(unique(p.begin(), p.end()), p.end());
    for(int i = 0; i < 45; i++) {
      dp[i][0] = dp[0][i] = i;
    }
    for(int i = 1; i < 45; i++) {
      for(int j = 1; j < 45; j++) {
        if(abs(i - j) > D) dp[i][j] = 1 << 30;
      }
    }
    N = p.size();
 
    for(int i = 0; i < N; i++) {
      for(int j = 0; j < p[i].size(); j++) {
        graph[i][j].clear();
        for(int k = 0; k < N; k++) {
          if(p[i].substr(p[i].size() - j) == p[k].substr(0, j)) {
            graph[i][j].push_back(k);
          }
        }
      }
    }
     
    for(int i = 0; i < p.size(); i++) {
      now = p[i];
      if(!Levenshtein_Distance(0, p[0].size())) continue;
      rec(i);
    }
    cout << pat.size() << endl;
    if(pat.size() <= 5) {
      sort(pat.begin(), pat.end());
      for(vector< string >::iterator it = pat.begin(); it != pat.end(); it++) {
        cout << *it << endl;
      }
    }
  }
}
accuracy: 1
time_ms: 1140
memory_kb: 22724
final_rank: 1

code_snippet: #include<stdio.h>
#include<algorithm>
#include<queue>
#include<string>
#include<set>
#include<vector>
#include<string.h>
using namespace std;
char str[50];
char word[35][30];
vector<string>ans;
struct wolf{
	int flag;
	int par;
	int chi[27];
	int dep;
	int val;
	int fail;
	wolf(){
		flag=dep=0;
		val=-1;
		par=-1;
		fail=0;
		for(int i=0;i<27;i++)chi[i]=-1;
	}
};
int LIM=5;
wolf pool[1100];
typedef unsigned long long nt;
nt key=1000000007;
set<nt>hash;
int len;
int sz;
int ret;
inline int chk(string t,int lc){
	int at=0;
	int last=0;
	for(int i=0;i<t.size();i++){
		while(1){
			if(pool[at].chi[t[i]-'@']!=-1){
				at=pool[at].chi[t[i]-'@'];
				break;
			}
			if(at==0){
				break;
			}
			at=pool[at].fail;
		}
		if(pool[at].flag)last=i+1;
		if(pool[at].dep<=i-last)return 0;
	}
	
	if(lc==0||last==t.size()){
	//	printf("%s\n",t.c_str());
		return 1;
	}
	return 0;
}
void dfs(int a,int b,string now,nt val,nt ks){
	if(chk(now,0)==0)return;
	if(a==len){
		if(hash.count(val)==0){
			hash.insert(val);
			if(chk(now,1)){
				ret++;
				if(ans.size()<LIM){
					ans.push_back(now);
				}
			}
		}
	}
	if(a<len){
		now.push_back(str[a]);
		dfs(a+1,b,now,val+ks*(str[a]-'@'+1),ks*key);
		now.erase(now.size()-1);
		if(b){
			dfs(a+1,b-1,now,val,ks);
			for(int i=0;i<27;i++){
				now.push_back('@'+i);
				dfs(a+1,b-1,now,val+ks*(i+1),ks*key);
				now.erase(now.size()-1);
			}
		}
	}
	if(b){
		for(int i=0;i<27;i++){
			now.push_back('@'+i);
			dfs(a,b-1,now,val+ks*(i+1),ks*key);
			now.erase(now.size()-1);
		}
	}
}
/*void dfs(int a,string b){
	//printf("%d %s\n",a,b.c_str());
	
	nt t=0;
	for(int i=0;i<b.size();i++){
		t*=key;
		t+=b[i]-'@'+1;
	}
	if(!hash.count(t)){
		hash.insert(t);
		if(chk(b)){
			ret++;
			if(ans.size()<LIM){
				ans.push_back(b);
			}
		}
	}
	if(a==0)return;
	
	for(int i=0;i<27;i++){
		for(int j=0;j<=b.size();j++){
			string c=b;
			c.insert(c.begin()+j,'@'+i);
			dfs(a-1,c);
		}
		for(int j=0;j<b.size();j++){
			char p=b[j];
			b[j]='@'+i;
			dfs(a-1,b);
			b[j]=p;
		}
	}
	for(int i=0;i<b.size();i++){
		string c=b;
		c.erase(c.begin()+i);
		dfs(a-1,c);
	}
}*/
int main(){
	int a,b;
	while(scanf("%d%d",&a,&b),b){
		scanf("%s",str);
		for(int i=0;str[i];i++)if(str[i]=='.')str[i]='@';
		for(int i=0;i<b;i++){
			scanf("%s",word+i);
			for(int j=0;word[i][j];j++)if(word[i][j]=='.')word[i][j]='@';
		}
		ans.clear();
		len=strlen(str);
		ret=0;
		sz=1;
		pool[0]=wolf();
		hash.clear();
		for(int i=0;i<b;i++){
			int at=0;
			for(int j=0;word[i][j];j++){
				if(pool[at].chi[word[i][j]-'@']==-1){
					pool[sz]=wolf();
					pool[sz].val=word[i][j]-'@';
					pool[sz].par=at;
					pool[at].chi[word[i][j]-'@']=sz;
					pool[sz].dep=j+1;
					at=sz;
					sz++;
				}else{
					at=pool[at].chi[word[i][j]-'@'];
				}
			}
			pool[at].flag=1;
		}
		
		queue<int>Q;
		Q.push(0);
		while(Q.size()){
			int now=Q.front();Q.pop();
			for(int i=0;i<27;i++){
				if(~pool[now].chi[i])Q.push(pool[now].chi[i]);
			}
			if(now==0||pool[now].par==0){
				pool[now].fail=0;continue;
			}
			int at=pool[now].par;
			while(1){
				if(at==0){
					pool[now].fail=0;break;
				}
				at=pool[at].fail;
				if(pool[at].chi[pool[now].val]!=-1){
					pool[now].fail=pool[at].chi[pool[now].val];
					if(pool[pool[now].fail].flag)pool[now].flag=1;
					break;
				}
			}
		}
		string T=str;
		dfs(0,a,"",0,1);
		printf("%d\n",ret);
		if(ret<=LIM){
			std::sort(ans.begin(),ans.end());
			for(int i=0;i<ret;i++){
				for(int j=0;j<ans[i].size();j++){
					if(ans[i][j]=='@')printf(".");
					else printf("%c",ans[i][j]);
				}
				printf("\n");
			}
		}
	}
}
accuracy: 1
time_ms: 7000
memory_kb: 14728
final_rank: 13

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <unordered_set>
#include <algorithm>
#include <bitset>

using namespace std;
typedef unsigned int ull;
static const ull HASH_BASE = 1000000007ull;
static const char CHARACTERS[] = ".ABCDEFGHIJKLMNOPQRSTUVWXYZ";

bool is_covered(
	const string &t, int m, const vector<bitset<65536> *> &pieces)
{
	const int n = t.size();
	if(m > n){ return false; }
	ull hash_remove = 1ull, h = 0ull;
	for(int i = 0; i < m; ++i){
		hash_remove *= HASH_BASE;
		h = (h * HASH_BASE) + t[i];
	}
	int tail = 0;
	for(int i = m; i <= n; ++i){
		if(pieces[h >> 16] != nullptr && (*pieces[h >> 16])[h & 0xffff]){
			tail = i;
		}
		//if(pieces.find(h) != pieces.end()){ tail = i; }
		if(tail == i - m){ return false; }
		h = (h * HASH_BASE) + t[i];
		h -= (hash_remove * t[i - m]);
	}
	return tail == n;
}

int main(){
	ios_base::sync_with_stdio(false);
	while(true){
		int d, n;
		cin >> d >> n;
		if(d == 0 && n == 0){ break; }
		string s;
		cin >> s;
		vector<string> pieces(n);
		for(int i = 0; i < n; ++i){ cin >> pieces[i]; }
		const int m = pieces[0].size();
		//unordered_set<ull> hashes(n);
		vector<bitset<65536> *> hashes(65536, nullptr);
		for(int i = 0; i < n; ++i){
			ull hash = 0;
			for(int j = 0; j < m; ++j){
				hash = (hash * HASH_BASE) + pieces[i][j];
			}
			//hashes.insert(hash);
			if(hashes[hash >> 16] == nullptr){
				hashes[hash >> 16] = new bitset<65536>();
			}
			hashes[hash >> 16]->set(hash & 0xffff);
		}
		n = s.size();
		ull hash_remove = 1;
		for(int i = 0; i < m; ++i){ hash_remove *= HASH_BASE; }
		vector<string> solutions;
		if(is_covered(s, m, hashes)){ solutions.push_back(s); }
		if(d >= 1){
			// insert
			for(int i = 0; i <= n; ++i){
				string t(n + 1, ' ');
				for(int j = 0; j < n; ++j){
					t[j >= i ? j + 1 : j] = s[j];
				}
				for(int x = 0; x < 27; ++x){
					t[i] = CHARACTERS[x];
					if(is_covered(t, m, hashes)){ solutions.push_back(t); }
				}
			}
			// replace
			for(int i = 0; i < n; ++i){
				string t(s);
				for(int x = 0; x < 27; ++x){
					t[i] = CHARACTERS[x];
					if(is_covered(t, m, hashes)){ solutions.push_back(t); }
				}
			}
			// remove
			for(int i = 0; i < n; ++i){
				string t(n - 1, ' ');
				for(int j = 0, k = 0; j < n; ++j){
					if(i == j){ continue; }
					t[k++] = s[j];
				}
				if(is_covered(t, m, hashes)){ solutions.push_back(t); }
			}
		}
		if(d >= 2){
			// insert
			for(int i = 0; i <= n; ++i){
				string t1(n + 1, ' ');
				for(int j = 0; j < n; ++j){
					t1[j >= i ? j + 1 : j] = s[j];
				}
				// insert-insert
				for(int j = 0; j <= n + 1; ++j){
					string t2(n + 2, ' ');
					for(int k = 0; k <= n; ++k){
						t2[k >= j ? k + 1 : k] = t1[k];
					}
					for(int a = 0; a < 27; ++a){
						t2[i] = CHARACTERS[a];
						for(int b = 0; b < 27; ++b){
							t2[j] = CHARACTERS[b];
							if(is_covered(t2, m, hashes)){
								solutions.push_back(t2);
							}
						}
					}
				}
				// insert-replace
				for(int j = 0; j <= n; ++j){
					if(i == j){ continue; }
					string t2(t1);
					for(int a = 0; a < 27; ++a){
						t2[i] = CHARACTERS[a];
						for(int b = 0; b < 27; ++b){
							t2[j] = CHARACTERS[b];
							if(is_covered(t2, m, hashes)){
								solutions.push_back(t2);
							}
						}
					}
				}
				// insert-remove
				for(int j = 0; j <= n; ++j){
					if(i == j){ continue; }
					string t2(n, ' ');
					for(int k = 0, p = 0; k <= n; ++k){
						if(k == j){ continue; }
						t2[p++] = t1[k];
					}
					const int p = (i >= j ? i - 1 : i);
					for(int a = 0; a < 27; ++a){
						t2[p] = CHARACTERS[a];
						if(is_covered(t2, m, hashes)){
							solutions.push_back(t2);
						}
					}
				}
			}
			// replace
			for(int i = 0; i < n; ++i){
				// replace-replace
				for(int j = i + 1; j < n; ++j){
					string t(s);
					for(int a = 0; a < 27; ++a){
						t[i] = CHARACTERS[a];
						for(int b = 0; b < 27; ++b){
							t[j] = CHARACTERS[b];
							if(is_covered(t, m, hashes)){
								solutions.push_back(t);
							}
						}
					}
				}
				// replace-remove
				for(int j = 0; j < n; ++j){
					if(i == j){ continue; }
					string t(n - 1, ' ');
					for(int k = 0, p = 0; k < n; ++k){
						if(k == j){ continue; }
						t[p++] = s[k];
					}
					const int p = (i >= j ? i - 1 : i);
					for(int a = 0; a < 27; ++a){
						t[p] = CHARACTERS[a];
						if(is_covered(t, m, hashes)){
							solutions.push_back(t);
						}
					}
				}
			}
			// remove-remove
			string t(n - 2, ' ');
			for(int i = 0; i < n; ++i){
				for(int j = i + 1; j < n; ++j){
					for(int k = 0, p = 0; k < n; ++k){
						if(k == i || k == j){ continue; }
						t[p++] = s[k];
					}
					if(is_covered(t, m, hashes)){
						solutions.push_back(t);
					}
				}
			}
		}
		sort(solutions.begin(), solutions.end());
		solutions.erase(
			unique(solutions.begin(), solutions.end()), solutions.end());
		cout << solutions.size() << endl;
		if(solutions.size() <= 5){
			for(const auto &sol : solutions){ cout << sol << endl; }
		}
		for(auto ptr : hashes){ delete ptr; }
	}
	return 0;
}
accuracy: 1
time_ms: 2710
memory_kb: 17268
final_rank: 8

code_snippet: #include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <ctime>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <deque>
#include <complex>
#include <string>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <valarray>
#include <unordered_set>
#include <iterator>
#include <assert.h>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define REPS(i,x) for(int i=1;i<=(int)(x);i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RREPS(i,x) for(int i=((int)(x));i>0;i--)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)
#define ALL(container) (container).begin(), (container).end()
#define RALL(container) (container).rbegin(), (container).rend()
#define SZ(container) ((int)container.size())
#define mp(a,b) make_pair(a, b)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os;
}
template<class T> ostream& operator<<(ostream &os, const set<T> &t) {
os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os;
}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}
template<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}
template<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}

const int INF = 1<<28;
const double EPS = 1e-8;
const int MOD = 1000000007;

struct AhoCorasick{
	struct State{
		int next[256], failure;
		int accepts;
		State():failure(0),accepts(0){memset(next, 0, sizeof(next));}
	};
	vector<State> states;
	int pats;
	
	AhoCorasick(){}
	AhoCorasick(vector<string> patterns):states(1),pats(patterns.size()){
		REP(i, pats){	// make trie
			int p = 0;
			FOR(c, patterns[i]){
				if(states[p].next[*c] <= 0){
					states[p].next[*c] = states.size();
					states.emplace_back();
				}
				p = states[p].next[*c];
			}
			states[p].accepts |= 1<<i;
		}
		
		queue<int> q;	// make failure link
		q.push(0);
		while(!q.empty()){
			const int p = q.front();q.pop();
			REP(i, 256){
				const int dst = states[p].next[i];
				if(!dst){
					// 大量に呼び出す時はnextを全部埋めとくといい
					int q=p;
					while(q && !states[q].next[i]) q = states[q].failure;
					states[p].next[i] = states[q].next[i];
				}else{
					q.push(dst);
					int f = states[p].failure;
					while(f && states[f].next[i] <= 0) f = states[f].failure;
					if(p){
						states[dst].failure = states[f].next[i];
						states[dst].accepts |= states[states[f].next[i]].accepts;
					}
				}
			}
		}
	}
	
	int go_next(int p, const int &c){
		// nextが埋まってるならここはいらない
		// while(p && !states[p].next[c]) p = states[p].failure;
		return states[p].next[c];
	}
	pair<int, int> apply(string s, int p=0){
		int res = 0;
		REP(i, s.size()){
			p = go_next(p, s[i]);
			res |= states[p].accepts;
		}
		return make_pair(p, res);
	}
	pair<int, int> apply(char c, int p=0){
		int res = 0;
		p = go_next(p, c);
		res |= states[p].accepts;
		return make_pair(p, res);
	}
} ac;
int d, n, m, ansn;
string s;
unordered_set<ull> hashes;
priority_queue<string> ans;

const string db=".ABCDEFGHIJKLMNOPQRSTUVWXYZ";

void dfs(string &t, int i, int p, ull rh, int last, int d, int previnserted=0){
	if(d < 0 || i > s.size() || last + m <= t.size()) return;
	if(i == s.size()){
		if(last == t.size() && hashes.find(rh) == hashes.end()){
			hashes.insert(rh);
			ans.push(t);
			ansn ++;
			if(ans.size() > 5) ans.pop();
		}
	}
	if(d){
		if(!previnserted) dfs(t, i+1, p, rh, last, d-1);	// delete
		FOR(c, db){
			t.push_back(*c);
			pii res = ac.apply(*c, p);
			dfs(t, i, res.first, rh*MOD+*c, res.second ? t.size() : last, d-1, 1);		// insert
			if(s[i]!=*c)
				dfs(t, i+1, res.first, rh*MOD+*c, res.second ? t.size() : last, d-1);	// modify
			t.resize(t.size()-1);
		}
	}
	t.push_back(s[i]);
	pii res = ac.apply(s[i], p);
	dfs(t, i+1, res.first, rh*MOD+s[i], res.second ? t.size() : last, d);
	t.resize(t.size()-1);
}

int main(int argc, char *argv[]){
	ios::sync_with_stdio(false);
	while(cin >> d >> n, n){
		cin >> s;
		vector<string> t(n);
		REP(i, n) cin >> t[i];
		m = t[0].size();
		ac = AhoCorasick(t);
		ansn = 0;
		string tmp="";
		dfs(tmp, 0, 0, 0, 0, d);
		cout << ansn << endl;
		if(ansn <= 5){
			vector<string> ss;
			REP(i, ansn){
				ss.push_back(ans.top()); ans.pop();
			}
			RREP(i, ansn) cout << ss[i] << endl;
		}
		while(!ans.empty()) ans.pop();
		hashes.clear();
	}
	return 0;
}
accuracy: 1
time_ms: 2990
memory_kb: 3352
final_rank: 5

code_snippet: #include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <ctime>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <deque>
#include <complex>
#include <string>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <valarray>
#include <unordered_set>
#include <iterator>
#include <assert.h>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define REPS(i,x) for(int i=1;i<=(int)(x);i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RREPS(i,x) for(int i=((int)(x));i>0;i--)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)
#define ALL(container) (container).begin(), (container).end()
#define RALL(container) (container).rbegin(), (container).rend()
#define SZ(container) ((int)container.size())
#define mp(a,b) make_pair(a, b)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os;
}
template<class T> ostream& operator<<(ostream &os, const set<T> &t) {
os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os;
}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}
template<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}
template<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}

const int INF = 1<<28;
const double EPS = 1e-8;
const int MOD = 1000000007;

struct AhoCorasick{
	struct State{
		int next[256], failure;
		int accepts;
		State():failure(0),accepts(0){memset(next, 0, sizeof(next));}
	};
	vector<State> states;
	int pats;
	
	AhoCorasick(){}
	AhoCorasick(vector<string> patterns):states(1),pats(patterns.size()){
		REP(i, pats){	// make trie
			int p = 0;
			FOR(c, patterns[i]){
				if(states[p].next[*c] <= 0){
					states[p].next[*c] = states.size();
					states.emplace_back();
				}
				p = states[p].next[*c];
			}
			states[p].accepts |= 1<<i;
		}
		
		queue<int> q;	// make failure link
		q.push(0);
		while(!q.empty()){
			const int p = q.front();q.pop();
			REP(i, 256){
				const int dst = states[p].next[i];
				if(!dst){
					// 大量に呼び出す時はnextを全部埋めとくといい
					int q=p;
					while(q && !states[q].next[i]) q = states[q].failure;
					states[p].next[i] = states[q].next[i];
				}else{
					q.push(dst);
					int f = states[p].failure;
					while(f && states[f].next[i] <= 0) f = states[f].failure;
					if(p){
						states[dst].failure = states[f].next[i];
						states[dst].accepts |= states[states[f].next[i]].accepts;
					}
				}
			}
		}
	}
	
	int go_next(int p, const int &c){
		// nextが埋まってるならここはいらない
		// while(p && !states[p].next[c]) p = states[p].failure;
		return states[p].next[c];
	}
	pair<int, int> apply(string s, int p=0){
		int res = 0;
		REP(i, s.size()){
			p = go_next(p, s[i]);
			res |= states[p].accepts;
		}
		return make_pair(p, res);
	}
	pair<int, int> apply(char c, int p=0){
		int res = 0;
		p = go_next(p, c);
		res |= states[p].accepts;
		return make_pair(p, res);
	}
} ac;
int d, n, m, ansn;
string s;
unordered_set<ull> hashes;
priority_queue<string> ans;

const string db=".ABCDEFGHIJKLMNOPQRSTUVWXYZ";

void dfs(string &t, int i, int p, ull rh, int last, int d){
	if(d < 0 || i > s.size() || last + m <= t.size()) return;
	if(i == s.size()){
		if(last == t.size() && hashes.find(rh) == hashes.end()){
			hashes.insert(rh);
			ans.push(t);
			ansn ++;
			if(ans.size() > 5) ans.pop();
		}
	}
	if(d){
		dfs(t, i+1, p, rh, last, d-1);	// delete
		FOR(c, db){
			t.push_back(*c);
			pii res = ac.apply(*c, p);
			dfs(t, i, res.first, rh*MOD+*c, res.second ? t.size() : last, d-1);		// insert
			if(s[i]!=*c)
				dfs(t, i+1, res.first, rh*MOD+*c, res.second ? t.size() : last, d-1);	// modify
			t.resize(t.size()-1);
		}
	}
	t.push_back(s[i]);
	pii res = ac.apply(s[i], p);
	dfs(t, i+1, res.first, rh*MOD+s[i], res.second ? t.size() : last, d);
	t.resize(t.size()-1);
}

int main(int argc, char *argv[]){
	ios::sync_with_stdio(false);
	while(cin >> d >> n, n){
		cin >> s;
		vector<string> t(n);
		REP(i, n) cin >> t[i];
		m = t[0].size();
		ac = AhoCorasick(t);
		ansn = 0;
		string tmp="";
		dfs(tmp, 0, 0, 0, 0, d);
		cout << ansn << endl;
		if(ansn <= 5){
			vector<string> ss;
			REP(i, ansn){
				ss.push_back(ans.top()); ans.pop();
			}
			RREP(i, ansn) cout << ss[i] << endl;
		}
		while(!ans.empty()) ans.pop();
		hashes.clear();
	}
	return 0;
}
accuracy: 1
time_ms: 3010
memory_kb: 3356
final_rank: 6

code_snippet: #include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<vector>
#include<algorithm>
#include<queue>
#include<ctime>
using namespace std;
#define INF (1<<29)



class PMA{//Aho-Corasick
	struct Node{
		bool match;
		int failure;
		int len;
		int next[256];
		Node(int len):failure(-1),len(len),match(false){memset(next,-1,sizeof(next));}
	};
	vector<Node> node;

	void precompute(){
		for(int i=1;i<(int)node.size();i++){
			for(int j=0;j<256;j++){
				node[i].next[j]=_step(i,j);
			}
		}
	}
	int _step(int t,char c)const{
		while(node[t].next[c]==-1)t=node[t].failure;
		return node[t].next[c];
	}
public:
	PMA(){}
	~PMA(){
		node.clear();
	}
	int size()const{return node.size();}
	bool matched(int id)const{return node[id].match;}
	void build(const vector<string> &p){
		node.clear();
		node.push_back(Node(0));
		for(int i=0;i<(int)p.size();i++){
			const string &s=p[i];
			int t=0;
			for(int j=0;j<(int)s.size();j++){
				if(node[t].next[s[j]]==-1){
					node[t].next[s[j]]=node.size();
					node.push_back(Node(j+1));				
				}
				t=node[t].next[s[j]];
			}
			node[t].match=true;
		}
		queue<int> q;
		for(int i=0;i<256;i++){
			Node &root=node[0];
			if(root.next[i]!=-1){
				q.push(root.next[i]);
				node[root.next[i]].failure=0;
			}else root.next[i]=0;
		}
		while(!q.empty()){
			int t=q.front();q.pop();
			for(int i=0;i<256;i++){
				if(node[t].next[i]!=-1){
					q.push(node[t].next[i]);
					int r=_step(node[t].failure,i);
					int v=node[t].next[i];
					node[v].failure = r;
					if(r!=-1)node[v].match |= node[r].match;
				}
			}
		}
		precompute();
	}
	int step(int t,char c)const{
		return node[t].next[c];
	}
	int length(int t)const{
		return node[t].len;
	}
};




PMA pma;
vector<string> piece;
int pLen;
vector<string> ans;
const char C[28]=".ABCDEFGHIJKLMNOPQRSTUVWXYZ";


bool match(string &s){
	int node=0;
	int cover=-1;
	for(int i=0;i<s.size();i++){
		node=pma.step(node,s[i]);
		if(pma.matched(node))cover = i;
		else if(i-cover>pma.length(node))return false;
		//else if(pLen==i-cover)return false;
	}
	return cover==s.size()-1;
}
void search(string &s,int d){
	if(d==0){
		//cout<<s<<endl;
		if(match(s))ans.push_back(s);
		return;
	}
	//search(s,d-1);
	string t(s.begin(),s.end());
	t+=' ';
	for(int i=s.size();;i--){//insert
		for(int j=0;j<27;j++){
			t[i]=C[j];
			search(t,d-1);
		}
		if(i==0)break;
		t[i]=t[i-1];
	}
	for(int i=0;i<s.size();i++){//replace
		for(int j=0;j<27;j++){
			char c=s[i];
			s[i]=C[j];
			search(s,d-1);
			s[i]=c;
		}
	}
	t.assign(s,1,s.size()-1);
	char c = s[0];
	for(int i=0;i<t.size();i++){//remove
		search(t,d-1);
		swap(c,t[i]);
	}
	search(t,d-1);
}
int main(){
	//clock_t t=clock();
	int d,n;
	while(cin>>d>>n,d|n){
		string alter;
		cin>>alter;
		piece.clear();
		for(int i=0;i<n;i++){
			string t;
			cin>>t;
			piece.push_back(t);
			pLen=t.size();
		}
		pma.build(piece);
		ans.clear();
		search(alter,d);
		sort(ans.begin(),ans.end());
		ans.erase(unique(ans.begin(),ans.end()),ans.end());
		cout<<ans.size()<<endl;
		if(ans.size()<=5){
			for(int i=0;i<ans.size();i++)cout<<ans[i]<<endl;
		}
	}
	//cout<<((double)(clock()-t)/CLOCKS_PER_SEC)<<endl;
	return 0;
}
accuracy: 1
time_ms: 6280
memory_kb: 34704
final_rank: 15

code_snippet: #include <algorithm>
#include <cstdlib>
#include <iostream>
#include <unordered_set>
#include <unordered_map>
#include <vector>
using namespace std;

typedef unsigned int hash_t;

constexpr int MAX_LEN = 40 + 2;
constexpr hash_t base = 1000000007;
hash_t pows[MAX_LEN + 1];

class rolling_hash {
private:
	vector<hash_t> hashes;

public:
	rolling_hash(const string &s):hashes(s.size() + 1) {
		hashes[0] = 0;
		for(unsigned i = 0; i < s.size(); ++i) {
			hashes[i + 1] = hashes[i] * base + s[i];
		}
	}

	hash_t hash(int l, int r) const {
		return hashes[r] - hashes[l] * pows[r - l];
	}

	static hash_t hash(const string &s) {
		long long res = 0;
		for(const auto &c : s) {
			res *= base;
			res += c;
		}
		return res;
	}
};

constexpr int MAX_N = 30;
int l;
int n, d;
vector<string> ans;
string pieces[MAX_N];
unordered_set<hash_t> hashes[MAX_N];
unordered_map<int, int> visited;
unordered_set<hash_t> checked;

inline void init() {
	l = pieces[0].size();
	checked.clear();

	for(int i = 0; i < n; ++i) {
		hashes[i].clear();
		hashes[i].insert(rolling_hash::hash(pieces[i]));

		for(int j = 0; j < l; ++j) {
			const char c = pieces[i][j];
			pieces[i][j] = '?';
			hashes[i].insert(rolling_hash::hash(pieces[i]));

			if(d == 2) {
				for(int k = j + 1; k < l; ++k) {
					const char t = pieces[i][k];
					pieces[i][k] = '?';
					hashes[i].insert(rolling_hash::hash(pieces[i]));
					pieces[i][k] = t;
				}
			}

			pieces[i][j] = c;
		}
	}
}

void rec(int idx, int ok, string &s, const vector<int> &question, const rolling_hash &rh) {
	if(ok >= s.size()) {
		ans.emplace_back(s);
		return;
	}
	if(idx + l > s.size()) return;

	int state = 0;
	for(const auto &pos : question) {
		state = state * 1000 + s[pos];
	}

	if(visited.count(state) && visited[state] >= idx) return;
	visited[state] = idx;

	for(int i = 0; i < n; ++i) {
		if(hashes[i].count(rh.hash(idx, idx + l))) {
			string tmp = "";
			bool change = false;
			for(const auto &pos : question) {
				tmp += s[pos];
				if(s[pos] == '?' && idx <= pos && pos < idx + l) {
					s[pos] = pieces[i][pos - idx];
					change = true;
				}
			}
			if(change) {
				rec(idx + 1, idx + l, s, question, rolling_hash(s));
				for(int j = 0; j < question.size(); ++j) {
					s[question[j]] = tmp[j];
				}
			}
			else {
				rec(idx + 1, idx + l, s, question, rh);
				return;
			}
		}
	}

	if(ok > idx) {
		rec(idx + 1, ok, s, question, rh);
	}
}

void check(string &s) {
	const int m = s.size();
	const rolling_hash rh(s);

	if(checked.count(rh.hash(0, m))) return;
	checked.insert(rh.hash(0, m));

	vector<int> question;
	for(int i = 0; i < m; ++i) {
		if(s[i] == '?') question.emplace_back(i);
	}

	visited.clear();
	rec(0, 0, s, question, rh);
}

void dfs(int rest, string &s) {
	check(s);
	if(rest == 0) return;

	const int n = s.size();

	// erase
	for(int i = 0; i < n; ++i) {
		string tmp(s);
		tmp.erase(i, 1);
		dfs(rest - 1, tmp);
	}

	// change
	for(int i = 0; i < n; ++i) {
		const char t = s[i];
		s[i] = '?';
		dfs(rest - 1, s);
		s[i] = t;
	}

	// insert
	for(int i = 0; i <= n; ++i) {
		s.insert(i, 1, '?');
		dfs(rest - 1, s);
		s.erase(i, 1);
	}
}

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

	pows[0] = 1;
	for(int i = 1; i <= MAX_LEN; ++i) {
		pows[i] = pows[i - 1] * base;
	}

	while(cin >> d >> n && (d | n)) {
		string sentence;
		cin >> sentence;

		for(int i = 0; i < n; ++i) {
			cin >> pieces[i];
		}

		init();

		ans.clear();
		dfs(d, sentence);

		sort(ans.begin(), ans.end());
		ans.erase(unique(ans.begin(), ans.end()), ans.end());

		const int c = ans.size();
		cout << c << endl;
		if(c <= 5) {
			for(const auto &e : ans) {
				cout << e << endl;
			}
		}
	}

	return EXIT_SUCCESS;
}
accuracy: 1
time_ms: 4660
memory_kb: 11928
final_rank: 11

code_snippet: #include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

int d,n;
int meslen;
int len;
string mes;
string piece[30];
string now;

int dp[51][51];
bool levenshtein(int m) {
  for (int i=1; i<=m; ++i) {
    int mi = INF;
    for (int j=max(1,i-d); j<=min(meslen,i+d); ++j) {
      if (now[i-1] == mes[j-1]) dp[i][j]=dp[i-1][j-1];
      else {
        dp[i][j] = min(min(dp[i-1][j],  // a deletion
                           dp[i][j-1]), // an insertion
                       dp[i-1][j-1]) + 1; // a substituion
      }
      mi = min(mi, dp[i][j]);
    }
    if (mi > d) return 0;
  }
  return 1;
}

// int dp[50][50][50];
// void calcEditDist() {
// }

bool levenshtein(const string &s, const string &t) {
  int n = s.size();
  int m = t.size();
  REP(i,n) dp[i][0]=i;
  REP(i,m) dp[0][i]=i;
  for (int i=1; i<=n; ++i) {
    for (int j=1; j<=m; ++j) {
      dp[i][j] = min(min(dp[i-1][j] + 1,  // a deletion
                        dp[i][j-1] + 1), // an insertion
                    dp[i-1][j-1] + (s[i-1] != t[j-1])); // a substituion
    }
  }
  return dp[n][m]<=d;
}

vector<pii> next[30][50];
vector<string> nextstr[30][50];

void calcNext() {
  REP(a,n) {
    REP(i,meslen+2) {
      next[a][i].clear();
      nextstr[a][i].clear();
    }
    REP(b,n) {
      for (int i=1; i<=len; ++i) {
        bool ok = 1;
        REP(j,len-i) if (piece[a][i+j]!=piece[b][j]) ok = 0;
        if (ok) {
          string t = piece[a] + piece[b].substr(len-i);
          for (int j=0; j+len+i<=meslen+2; ++j) {
            // bool ok = 0;
            // for (int k=max(0,j-2); k<=j+2&&k+len+i<=meslen+2; ++k) {
            //   if (levenshtein(t,mes.substr(k,len+i))) {
            //     ok = 1;
            //     break;
            //   }
            // }
            // if (ok) {
            //   if (j==0 && b
            //   nextstr[a][j].push_back(piece[b].substr(len-i));
            //   next[a][j].push_back(pii(b,i));              
            // }
            nextstr[a][j].push_back(piece[b].substr(len-i));
            next[a][j].push_back(pii(b,i));
          }
        }
      }
    }
  }
}

vector<string> ans;

ll hs(const string &s) {
  ll res = 0;
  REP(i,s.size()) {
    res = res*1000000009 + s[i];
  }
  return res;
}

map<ll,int> mp[50];

int cnt, cnt1, cnt2;
void dfs(int pos, int id) {
  cnt2++;
  
  // if (mp[pos+len].count(now)) return;
  // mp[pos+len][now] = 1;
  
  if (!levenshtein(pos+len)) return;
  // cout << now << endl;
  if (dp[pos+len][meslen] <= d) {
    ans.push_back(now);
    // cout << now << endl;
  }

  string tmp = now;
  REP(i,next[id][pos].size()) {
    int b = next[id][pos][i].first;
    int p = next[id][pos][i].second;
    if (pos+p+len > meslen+2) continue;
    now = tmp + nextstr[id][pos][i];
    ll hash = hs(now);
    if (mp[pos+p].count(hash)) continue;
    mp[pos+p][hash] = 1;
    // if (!levenshtein(pos+p+len)) continue;
    // if (dp[min(meslen,pos+p+len)][pos+p+len] > d) continue;
    dfs(pos+p, b);
  }
  now = tmp;
}

int main() {
  int N;
  while(scanf("%d%d",&d,&N),d||N) {
    REP(i,50) mp[i].clear();
    cin >> mes;
    meslen = mes.size();
    n = 0;
    REP(i,N) {
      cin >> piece[i];
    }
    sort(piece, piece+N);
    n = unique(piece, piece+N) - piece;
    // REP(i,n) {
    //   cout << piece[i] << endl;
    // }
    // cout << endl;
    len = piece[0].size();
    calcNext();
    ans.clear();
    cnt = 0,cnt1=0,cnt2=0;
    
    REP(i,50) dp[i][0]=dp[0][i]=i;
    for (int i=1; i<=meslen+2; ++i)
      for (int j=1; j<=meslen+2; ++j)
        if (abs(i-j)>d) dp[i][j]=INF;
    REP(j,n) {
      now = piece[j];
      dfs(0,j);
    }
    //printf("%d %d %d\n",cnt,cnt1,cnt2);
    sort(ALL(ans));
    cout << ans.size() << endl;
    if (ans.size() <= 5) {
      REP(i,ans.size()) {
        cout << ans[i] << endl;
      }
    }
  }
}
accuracy: 1
time_ms: 4470
memory_kb: 63276
final_rank: 14

code_snippet: #include <cstdio>
#include <vector>
#include <set>
#include <string>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <string.h>
using namespace std;

typedef unsigned long long ul;

const ul base = 1000000007;
vector<ul> bs;
set<string> ans;
set<ul> ans2;
string line;
vector<ul> hs;
vector<string> piece;
set<string> d1, d2;
string plane;
bool precheck[0x100][0x100][0x100];
int N, d, L;
bool cut;
bool preok[50], postok[50];
bool preok2[50][0x100], postok2[50][0x100];
int prewidth[0x100], postwidth[0x100];
int precut, postcut, width;
bool oks[50], covered[50];

inline ul hash_func(string& str){
	ul ret = 0;
	for(int i=0; i<L; i++){
		ret += str[i] * bs[L-1-i];
	}
	return ret;
}

inline ul hash_func2(string& str){
	ul ret = 0;
	for(int i=0; i<str.length(); i++){
		ret += bs[i] * str[i];
	}
	return ret;
}

inline void add_ans(string& str){
	ans2.insert(hash_func2(str));
	if(ans.size()<=4){
		ans.insert(str);
	}
}

inline bool check(string& str){
	int n = str.length();
	if(n < L){
		return false;
	}
	ul h = hash_func(str);
	memset(oks,0,sizeof(oks));
	memset(covered,0,sizeof(covered));
	if(binary_search(hs.begin(), hs.end(), h)){
		oks[0] = true;
	}
	for(int i=0; i<n-L; i++){
		h = h*base - bs[L]*str[i] + str[i+L];
		if(binary_search(hs.begin(), hs.end(), h)){
			oks[i+1] = true;
		}
	}
	for(int i=0; i<n; i++)if(oks[i]){
		for(int j=0; j<L; j++){
			covered[i+j] = true;
		}
		if(!covered[i]){
			//if((rand()&0xfff)<10) cout << str << endl;
			return false;
		}
	}
	for(int i=0; i<n; i++)if(!covered[i]){
		//if((rand()&0xfff)<10) cout << str << endl;
		return false;
	}
	return true;
}

void generate(string& str, set<string>& dd){
	int difpos;
	bool phase2 = (&dd == &d2);
	if(cut){
		if(!phase2){
			dd.insert(str + string(1,line[0]));
			dd.insert(string(1,line[0]) + str);
		}
		else {
			string t = str + string(1,line[0]);
			if(check(t)) add_ans(t);
			t = string(1,line[0]) + str;
			if(check(t)) add_ans(t);
		}
		difpos = str.find_first_not_of(line[0]);
	}
	int n = str.length();
	//insert
	for(int i=0; i<n+1; i++){
		if(cut && difpos != string::npos){
				if(difpos<L-1 && !preok2[difpos][str[difpos]]){
					if(!(preok2[difpos+1][str[difpos]] && i<difpos)) continue;
				}
				if(n-1-difpos<L-1 && !postok2[n-1-difpos][str[difpos]]){
					if(!(postok2[n-difpos][str[difpos]] && i>=difpos)) continue;
				}
		}
		if(cut && (i<=precut-1 || n-i-1<=postcut-2)) continue;
		if(cut && difpos != string::npos && abs(difpos - i) <= width) continue;
		if(cut && difpos != string::npos && prewidth[str[difpos]] && difpos - i <= prewidth[str[difpos]] - 1 && i <= difpos) continue;
		if(cut && phase2 && i<L && !preok[i]) continue;
		if(cut && phase2 && n-i-1<L && !postok[n-i]) continue;
		if(cut && i < L && i>0 && !(preok[i-1] || preok[i])) continue;
		if(cut && n-1-i < L && i<n-1 && !(postok[n-i] || postok[n-i+1])) continue;
		string t;t.reserve(n+1);
		for(int j=0; j<n; j++){
			if(j==i) t.push_back('.');
			t.push_back(str[j]);
		}
		if(i==n) t.push_back('.');
		for(char c='.';c<='.';c++){
			if(cut && phase2 && i<L && !preok2[i][c]) continue;
			if(cut && phase2 && n-i-1<L && !postok2[n-i][c]) continue;
			if(cut && i<L-1 && i>0 && !(preok2[i-1][c] || preok2[i][c] || preok2[i+1][c])) continue;
			if(cut && n-i-1<L-1 && n-i>0 && !(postok2[n-i-1][c] || postok2[n-i][c] || postok2[n-i+1][c])) continue;
			if(i==0 || i==n || (precheck[t[i-1]][t[i]][t[i+1]])){
				if(phase2){
					if(check(t)) add_ans(t);
				}
				else{
					dd.insert(t);
				}
			}
		}
		for(char c='A'; c<='Z'; c++){
			t[i] = c;
			if(cut && phase2 && i<L && !preok2[i][c]) continue;
			if(cut && phase2 && n-i-1<L && !postok2[n-i][c]) continue;
			if(cut && i<L-1 && i>0 && !(preok2[i-1][c] || preok2[i][c] || preok2[i+1][c])) continue;
			if(cut && n-i-1<L-1 && n-i>0 && !(postok2[n-i-1][c] || postok2[n-i][c] || postok2[n-i+1][c])) continue;
			if(i==0 || i==n || (precheck[t[i-1]][t[i]][t[i+1]])){
				if(phase2){
					if(check(t)) add_ans(t);
				}
				else{
					dd.insert(t);
				}
			}
		}
	}

	//replace
	if(!(cut && difpos != string::npos &&
			((difpos<L && !preok2[difpos][str[difpos]]) || (n-1-difpos<L && !postok2[n-1-difpos][str[difpos]]))))
	for(int i=0; i<n; i++){
		if(cut && (i<=precut-1 || n-i-1<=postcut-1)) continue;
		if(cut && difpos != string::npos && abs(difpos - i) <= width) continue;
		if(cut && difpos != string::npos && prewidth[str[difpos]] && difpos - i <= prewidth[str[difpos]] - 1 && i <= difpos) continue;
		if(cut && phase2 && i<L && !preok[i]) continue;
		if(cut && phase2 && n-1-i<L && !postok[n-i-1]) continue;
		if(cut && i < L && i>0 && !(preok[i-1] || preok[i])) continue;
		if(cut && n-1-i < L && i<n-1 && !(postok[n-i-2] || postok[n-i-1] )) continue;
		string t = str;
		for(char c='.';c<='.';c++){
			t[i] = c;
			if(cut && phase2 && i<L && !preok2[i][c]) continue;
			if(cut && phase2 && n-i-1<L && !postok2[n-i-1][c]) continue;
			if(cut && i<L-1 && i>0 && !(preok2[i-1][c] || preok2[i][c] || preok2[i+1][c])) continue;
			if(cut && n-i-1<L-1 && n-i-1>0 && !(postok2[n-i-2][c] || postok2[n-i-1][c] || postok2[n-i][c])) continue;
			if(i==0 || i==n-1 || (precheck[t[i-1]][t[i]][t[i+1]])){
				if(phase2){
					if(check(t)) add_ans(t);
				}
				else{
					dd.insert(t);
				}
			}
		}
		for(char c='A'; c<='Z'; c++){
			t[i] = c;
			if(cut && phase2 && i<L && !preok2[i][c]) continue;
			if(cut && phase2 && n-i-1<L && !postok2[n-i-1][c]) continue;
			if(cut && i<L-1 && i>0 && !(preok2[i-1][c] || preok2[i][c] || preok2[i+1][c])) continue;
			if(cut && n-i-1<L-1 && n-i-1>0 && !(postok2[n-i-2][c] || postok2[n-i-1][c] || postok2[n-i][c])) continue;
			if(i==0 || i==n-1 || (precheck[t[i-1]][t[i]][t[i+1]])){
				if(phase2){
					if(check(t)) add_ans(t);
				}
				else{
					dd.insert(t);
				}
			}
		}
	}

	//delete
	for(int i=0; i<n; i++){
		string t;t.reserve(n);
		for(int j=0; j<n; j++){
			if(i!=j) t.push_back(str[j]);
		}
		if(phase2){
			if(check(t)) add_ans(t);
		}
		else{
			dd.insert(t);
		}
	}
}

inline void add(string& t, int ind){
	if(check(t)) add_ans(t);
	for(char c='A';c<='Z';c++){
		t[ind] = c;
		if(check(t)) add_ans(t);
	}
	t[ind] = '.';
	for(char c='A';c<='Z';c++){
		t[ind+1] = c;
		if(check(t)) add_ans(t);
	}
	for(char c='A';c<='Z';c++){
		for(char cc='A';cc<='Z';cc++){
			t[ind] = c;
			t[ind+1] = cc;
			if(check(t)) add_ans(t);
		}
	}
}

void subsolve(){
	//insert-insert
	int n = line.length();
	for(int i=0; i<n+1; i++){
		string t;
		for(int j=0; j<n; j++){
			if(i==j) t += "..";
			t.push_back(line[j]);
		}
		if(i==n) t += "..";
		add(t,i);
	}
	//insert-replace
	for(int i=0; i<n; i++){
		string t;
		for(int j=0; j<n; j++){
			if(i==j) t.push_back('.');
			t.push_back(line[j]);
		}
		t[i+1] = '.';
		add(t,i);
	}
	//replace-insert
	for(int i=0; i<n; i++){
		string t;
		for(int j=0; j<n; j++){
			if(i+1==j) t.push_back('.');
			t.push_back(line[j]);
		}
		if(i==n-1) t.push_back('.');
		t[i] = '.';
		add(t,i);
	}
	//replace-replace
	for(int i=0; i<n-1; i++){
		string t = line;
		t[i] = t[i+1] = '.';
		add(t,i);
	}
}

void solve(){
	cut = true;
	for(int i=0; i<line.length()-1; i++){
		if(line[i] != line[i+1]) cut = false;
	}
	if(cut){
		vector<string> cats;
		cats.reserve(30*30*20*20);
		for(int i=0; i<N; i++){
			for(int j=0; j<N; j++){
				cats.push_back(piece[i] + piece[j]);
				for(int k=0; k<L; k++){
					string pre = piece[i].substr(k);
					for(int l=0; l<L; l++){
						string post = piece[j].substr(0,pre.length());
						if(pre == post){
							cats.push_back(piece[i] + piece[j].substr(pre.length()));
						}
					}
				}
			}
		}
		sort(cats.begin(), cats.end());
		cats.erase(unique(cats.begin(), cats.end()), cats.end());
		width = 1<<10;
		for(typeof(cats.begin()) itr = cats.begin(); itr != cats.end(); itr++){
			string t = *itr;
			for(int i=0; i<t.length(); i++)if(t[i] != line[0]){
				preok[i] = true;
				preok2[i][t[i]] = true;
			}
			for(int i=0; i<t.length(); i++)if(t[t.length()-1-i] != line[0]){
				postok[i] = true;
				postok2[i][t[t.length()-1-i]] = true;
			}
			int pre = t.find_first_not_of(line[0]), post = t.find_last_not_of(line[0]);
			if(pre != post){
				width = min(width, post - pre - 1);
				prewidth[t[post]] = max(prewidth[t[post]], post - pre - 1);
				postwidth[t[pre]] = max(postwidth[t[pre]], post - pre - 1);
			}
		}
		if(width == 1<<10) width = 0;
		precut = L;
		for(int i=0; i<N; i++){
			for(int j=0; j<L; j++)if(piece[i][j] != line[0]){
				precut = min(precut, j);
				break;
			}
		}
		postcut = L;
		for(int i=0; i<N; i++){
			for(int j=0; j<L; j++)if(piece[i][L-1-j] != line[0]){
				postcut = min(postcut, j);
				break;
			}
		}
		if(0){
			cout << width << endl;
			cout << precut << endl;
			cout << postcut << endl;
		}
	}
	for(int i=0; i<N; i++){
		for(int j=1; j<L-1; j++){
			precheck[piece[i][j-1]][piece[i][j]][piece[i][j+1]] = true;
		}
	}
	for(int i=0; i<N; i++){
		for(int k=0; k<N; k++){
			precheck[piece[i][L-2]][piece[i][L-1]][piece[k][0]] = true;
			precheck[piece[k][L-1]][piece[i][0]][piece[i][1]] = true;
		}
	}
	for(int i=0; i<N; i++){
		hs.push_back(hash_func(piece[i]));
	}
	sort(hs.begin(), hs.end());
	if(check(line)){
		add_ans(line);
	}
	if(d<1) return ;
	generate(line, d1);
	for(typeof(d1.begin()) itr = d1.begin(); itr != d1.end(); itr++){
		string tmp = *itr;
		if(check(tmp)) add_ans(tmp);
	}
	if(d<2) return ;
	for(typeof(d1.begin()) itr = d1.begin(); itr != d1.end(); itr++){
		string tmp = *itr;
		generate(tmp, d2);
	}
	if(!cut || width==0)subsolve();
}

void print(){
	printf("%d\n", (int)ans2.size());
	if(ans2.size() <= 5){
		for(typeof(ans.begin()) itr = ans.begin(); itr != ans.end(); itr++){
			puts(itr->c_str());
			//cout << *itr << endl;
		}
	}
}

void init(){
	cut = false;
	plane = "";
	d1.clear();
	d2.clear();
	ans.clear();
	ans2.clear();
	hs.clear();
	piece.clear();
	for(int i=0; i<50; i++) preok[i] = postok[i] = false;
	for(int i=0; i<0x100; i++) prewidth[i] = postwidth[i] = 0;
	for(int i=0; i<50; i++)for(int j=0; j<0x100; j++) preok2[i][j] = postok2[i][j] = false;
	for(int i=0; i<0x100; i++){
		for(int j=0; j<0x100; j++){
			for(int k=0; k<0x100; k++){
				precheck[i][j][k] = false;
			}
		}
	}
}


int main(){
	bs.push_back(1);
	for(int i=0; i<60; i++){
		bs.push_back(bs.back()*base);
	}
	while(scanf("%d%d ",&d,&N)){
		if(d==0 && N==0) break;
		init();
		getline(cin,line);
		piece.clear();
		for(int i=0; i<N; i++){
			string t;
			getline(cin,t);
			piece.push_back(t);
		}
		L = piece[0].size();
		solve();
		print();
	}
	fflush(stdout);
	return 0;
}
accuracy: 1
time_ms: 7460
memory_kb: 21000
final_rank: 16

code_snippet: // ACM-ICPCà\I2010 F. Ã¢L¯

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <cstring>

using namespace std;

const int INF = 100000000;

int d;
string mes, cur;
vector<string> piece;
vector<int> connect[30][20];
vector<string> ans;
set<string> mem[43];
int dp[43][43];
int cost[30][20];
int curdist;

int getMinedit(string s, string t){
	int n = s.size(), m = t.size();
	int cost[43], newcost[43];
	for(int i=0;i<=n;i++) cost[i] = 0;
	for(int j=1;j<=m;j++){
		newcost[0] = j;
		for(int i=1;i<=n;i++){
			newcost[i] = 1+min(newcost[i-1],cost[i]);
			newcost[i] = min(newcost[i],cost[i-1]+(s[i-1]!=t[j-1]));
		}
		memcpy(cost, newcost, sizeof(cost));
	}
	return *min_element(cost, cost+n+1);
}

bool editDist(int s, int e){
	for(int i=s+1;i<=e;i++){
		curdist = INF;
		for(int j=max(1,i-d);j<=min((int)mes.size(),i+d);j++){
			if(cur[i-1]==mes[j-1]) dp[i][j] = dp[i-1][j-1];
			else {
				dp[i][j] = dp[i-1][j-1]+1;
				dp[i][j] = min(dp[i][j], dp[i-1][j]+1);
				dp[i][j] = min(dp[i][j], dp[i][j-1]+1);
			}
			curdist = min(curdist, dp[i][j]);
		}
		if(curdist > d) return false;
	}
	return true;
}

void dfs(int pIdx, int p1, int p2){
	int len = piece[0].size();
	int csize = cur.size();
	if(dp[csize][mes.size()] <= d) ans.push_back(cur);
	int start = min(len-1, csize-p2-1);
	cur += string(len-1-start, ' ');
	int ccost = curdist;
	for(int i=start;i>=0;i--){
		int nsize = csize+len-i;
		if(nsize > mes.size() + d) break;
		cur += ' ';
		for(int j=0;j<connect[pIdx][i].size();j++){
			if(ccost + cost[connect[pIdx][i][j]][i] > d) continue;
			bool flag = true;
			for(int k=i;k<len;k++){
				cur[csize+k-i] = piece[connect[pIdx][i][j]][k];
				if(!editDist(csize+k-i, csize+k-i+1)){
					flag = false;
					break;
				}
			}
			if(!flag) continue;
			if(mem[nsize].count(cur)) continue;
			mem[nsize].insert(cur);
			dfs(connect[pIdx][i][j], nsize, p1);
		}
	}
	cur = cur.substr(0, csize);
}

int main(){
	int n, len;
	for(int i=0;i<=42;i++) dp[i][0] = dp[0][i] = i;
	while(cin >> d >> n, n){
		cin >> mes;
		piece.clear();
		for(int i=0;i<n;i++){
			string str; cin >> str;
			if(getMinedit(mes, str) > d) continue;
			piece.push_back(str);
		}
		sort(piece.begin(), piece.end());
		piece.erase(unique(piece.begin(), piece.end()), piece.end());
		len = piece[0].size();
		for(int i=1;i<=mes.size()+d;i++)
			for(int j=1;j<=mes.size()+d;j++)
				if(abs(i-j) > d) dp[i][j] = INF;
		for(int i=0;i<piece.size();i++){
			for(int j=0;j<len;j++){
				connect[i][j].clear();
				for(int k=0;k<piece.size();k++){
					if(piece[i].substr(len-j) == piece[k].substr(0,j)){
						connect[i][j].push_back(k);
						cost[k][j] = getMinedit(mes, piece[k].substr(j));
					}
				}
			}
		}
		for(int i=0;i<=mes.size()+d;i++)
			mem[i].clear();
		ans.clear();
		for(int i=0;i<piece.size();i++){
			cur = piece[i];
			if(!editDist(0, len)) continue;
			dfs(i, len, 0);
		}
		sort(ans.begin(), ans.end());
		cout << ans.size() << endl;
		if(ans.size() <= 5){
			for(int i=0;i<ans.size();i++)
				cout << ans[i] << endl;
		}
	}
}
accuracy: 1
time_ms: 2200
memory_kb: 15000
final_rank: 3

code_snippet: #include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;

int d, n;
string data;
vector<string> piece;

int L, M;
set<string> results;
vector< vector< pair<int,int> > > nexts;

void init() {
	L = piece[0].length();
	M = data.length();
	results.clear();
	nexts = vector< vector< pair<int,int> > >(n+1);
	for (int i = 0; i < n; ++ i) {
		nexts[n].push_back(make_pair(i,0));
		for (int k = L-1; k > 0; -- k) {
			for (int j = 0; j < n; ++ j) {
				for (int p = 0; p < k; ++ p) {
					if (piece[i][L-k+p] != piece[j][p]) goto next;
				}
				nexts[i].push_back(make_pair(j, k));
				next:;
			}
		}
		for (int j = 0; j < n; ++ j) {
			nexts[i].push_back(make_pair(j, 0));
		}
	}
}

char buf[100];
int dist[100][100];
set<string> visited;

void f(int prev, int pos, int a, int b) {
	buf[pos] = 0;
	if (!visited.insert(buf).second) return;
	if (a <= M && M <= b && dist[pos][M] <= d) results.insert(buf);
	if (pos == M+d) return;
	
	for (int i = 0; i < nexts[prev].size(); ++ i) {
		if (pos + L-nexts[prev][i].second > M+d) break;
		const string &s = piece[ nexts[prev][i].first ];
		int aa = a, bb = b;
		for (int j = 0; j + nexts[prev][i].second < L; ++ j) {
			buf[pos+j] = s[j + nexts[prev][i].second];
			int x = 1<<30, y = -1;
			if (aa-1 > 0) dist[pos+j+1][aa-1] = 1<<30;
			dist[pos+j][bb+1] = 1<<30;
			for (int k = max(0,aa-1); k < M && k <= bb; ++ k) {
				int cost = 1;
				if (buf[pos+j] == data[k]) cost = 0;
				dist[pos+j+1][k+1] = min(min(
					dist[pos+j][k+1] + 1,
					dist[pos+j+1][k] + 1),
					dist[pos+j][k] + cost
				);
				if (dist[pos+j+1][k+1] <= d) {
					x = min(x, k+1);
					y = max(y, k+1);
				}
			}
			if (x > y) goto next;
			aa = x; bb = y;
		}
		f(nexts[prev][i].first, pos + L-nexts[prev][i].second, aa, bb);
		next:;
	}
}

void calc() {
	init();
	memset(dist, 0, sizeof(dist));
	for (int i = 0; i <= M+d; ++ i) {
		dist[i][0] = i;
	}
	for (int i = 0; i <= M; ++ i) {
		dist[0][i] = i;
	}
	visited.clear();
	f(n, 0, 0, 2);
	cout << results.size() << endl;
	if (results.size() <= 5) {
		for (set<string>::iterator i = results.begin(); i != results.end(); ++ i) {
			cout << *i << endl;
		}
	}
}

int main() {
	for (;;) {
		cin >> d >> n;
		if (d == 0 && n == 0) break;
		cin >> data;
		piece = vector<string>(n);
		for (int i = 0; i < n; ++ i) {
			cin >> piece[i];
		}
		calc();
	}
}
accuracy: 1
time_ms: 4740
memory_kb: 16000
final_rank: 12

code_snippet: // ACM-ICPCà\I2010 F. Ã¢L¯

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <string.h>

using namespace std;

const int INF = 100000000;

int d;
string mes, cur;
vector<string> piece;
vector<int> connect[30][20];
vector<string> ans;
set<string> mem[43];
int dp[43][43];
int cost[30][20];
int curdist;

int getMinedit(string s, string t){
	int n = s.size(), m = t.size();
	int cost[43], newcost[43];
	for(int i=0;i<=n;i++) cost[i] = 0;
	for(int j=1;j<=m;j++){
		newcost[0] = j;
		for(int i=1;i<=n;i++){
			newcost[i] = 1+min(newcost[i-1],cost[i]);
			newcost[i] = min(newcost[i],cost[i-1]+(s[i-1]!=t[j-1]));
		}
		memcpy(cost, newcost, sizeof(cost));
	}
	return *min_element(cost, cost+n+1);
}

bool editDist(int s, int e){
	for(int i=s+1;i<=e;i++){
		curdist = INF;
		for(int j=max(1,i-d);j<=min((int)mes.size(),i+d);j++){
			if(cur[i-1]==mes[j-1]) dp[i][j] = dp[i-1][j-1];
			else {
				dp[i][j] = dp[i-1][j-1]+1;
				dp[i][j] = min(dp[i][j], dp[i-1][j]+1);
				dp[i][j] = min(dp[i][j], dp[i][j-1]+1);
			}
			curdist = min(curdist, dp[i][j]);
		}
		if(curdist > d) return false;
	}
	return true;
}

void dfs(int pIdx, int p1, int p2){
	int len = piece[0].size();
	int csize = cur.size();
	if(dp[csize][mes.size()] <= d) ans.push_back(cur);
	int start = min(len-1, csize-p2-1);
	cur += string(len-1-start, ' ');
	int ccost = curdist;
	for(int i=start;i>=0;i--){
		int nsize = csize+len-i;
		if(nsize > mes.size() + d) break;
		cur += ' ';
		for(int j=0;j<connect[pIdx][i].size();j++){
			if(ccost + cost[connect[pIdx][i][j]][i] > d) continue;
			bool flag = true;
			for(int k=i;k<len;k++){
				cur[csize+k-i] = piece[connect[pIdx][i][j]][k];
				if(!editDist(csize+k-i, csize+k-i+1)){
					flag = false;
					break;
				}
			}
			if(!flag) continue;
			if(mem[nsize].count(cur)) continue;
			mem[nsize].insert(cur);
			dfs(connect[pIdx][i][j], nsize, p1);
		}
	}
	cur = cur.substr(0, csize);
}

int main(){
	int n, len;
	for(int i=0;i<=42;i++) dp[i][0] = dp[0][i] = i;
	while(cin >> d >> n, n){
		cin >> mes;
		piece.clear();
		for(int i=0;i<n;i++){
			string str; cin >> str;
			if(getMinedit(mes, str) > d) continue;
			piece.push_back(str);
		}
		sort(piece.begin(), piece.end());
		piece.erase(unique(piece.begin(), piece.end()), piece.end());
		len = piece[0].size();
		for(int i=1;i<=mes.size()+d;i++)
			for(int j=1;j<=mes.size()+d;j++)
				if(abs(i-j) > d) dp[i][j] = INF;
		for(int i=0;i<piece.size();i++){
			for(int j=0;j<len;j++){
				connect[i][j].clear();
				for(int k=0;k<piece.size();k++){
					if(piece[i].substr(len-j) == piece[k].substr(0,j)){
						connect[i][j].push_back(k);
						cost[k][j] = getMinedit(mes, piece[k].substr(j));
					}
				}
			}
		}
		for(int i=0;i<=mes.size()+d;i++)
			mem[i].clear();
		ans.clear();
		for(int i=0;i<piece.size();i++){
			cur = piece[i];
			if(!editDist(0, len)) continue;
			dfs(i, len, 0);
		}
		sort(ans.begin(), ans.end());
		cout << ans.size() << endl;
		if(ans.size() <= 5){
			for(int i=0;i<ans.size();i++)
				cout << ans[i] << endl;
		}
	}
}
accuracy: 1
time_ms: 2200
memory_kb: 15000
final_rank: 3

code_snippet: // ACM-ICPCà\I2010 F. Ã¢L¯

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

const int INF = 100000000;

int d;
string mes, cur;
vector<string> piece;
vector<int> connect[30][20];
vector<string> ans;
set<string> mem[43];
int dp[43][43];

bool editDist(int s, int e){
	for(int i=s+1;i<=e;i++){
		int m = INF;
		for(int j=max(1,i-d);j<=min((int)mes.size(),i+d);j++){
			if(cur[i-1]==mes[j-1]) dp[i][j] = dp[i-1][j-1];
			else {
				dp[i][j] = dp[i-1][j-1]+1;
				dp[i][j] = min(dp[i][j], dp[i-1][j]+1);
				dp[i][j] = min(dp[i][j], dp[i][j-1]+1);
			}
			m = min(m, dp[i][j]);
		}
		if(m > d) return false;
	}
	return true;
}

void dfs(int pIdx, int p1, int p2){
	int len = piece[0].size();
	int csize = cur.size();
	if(dp[csize][mes.size()] <= d) ans.push_back(cur);
	if(len-1 != min(len-1, csize-p2-1))
		cur += string(len-1-(csize-p2-1), ' ');
	for(int i=min(len-1, csize-p2-1);i>=0;i--){
		int nsize = csize+len-i;
		if(nsize > mes.size() + d) break;
		cur += ' ';
		for(int j=0;j<connect[pIdx][i].size();j++){
			bool flag = true;
			for(int k=i;k<len;k++){
				cur[csize+k-i] = piece[connect[pIdx][i][j]][k];
				if(!editDist(csize+k-i, csize+k-i+1)){
					flag = false;
					break;
				}
			}
			if(!flag) continue;
			if(mem[nsize].count(cur)) continue;
			mem[nsize].insert(cur);
			dfs(connect[pIdx][i][j], nsize, p1);
		}
	}
	cur = cur.substr(0, csize);
}

int main(){
	int n, len;
	for(int i=0;i<=42;i++) dp[i][0] = dp[0][i] = i;
	while(cin >> d >> n, n){
		cin >> mes;
		piece.clear();
		for(int i=0;i<n;i++){
			string str; cin >> str;
			piece.push_back(str);
		}
		sort(piece.begin(), piece.end());
		piece.erase(unique(piece.begin(), piece.end()), piece.end());
		len = piece[0].size();
		for(int i=1;i<=mes.size()+d;i++)
			for(int j=1;j<=mes.size()+d;j++)
				if(abs(i-j) > d) dp[i][j] = INF;
		for(int i=0;i<piece.size();i++){
			for(int j=0;j<len;j++){
				connect[i][j].clear();
				for(int k=0;k<piece.size();k++){
					if(piece[i].substr(len-j) == piece[k].substr(0,j))
						connect[i][j].push_back(k);
				}
			}
		}
		for(int i=0;i<=mes.size()+d;i++)
			mem[i].clear();
		ans.clear();
		for(int i=0;i<piece.size();i++){
			cur = piece[i];
			if(!editDist(0, len)) continue;
			dfs(i, len, 0);
		}
		sort(ans.begin(), ans.end());
		cout << ans.size() << endl;
		if(ans.size() <= 5){
			for(int i=0;i<ans.size();i++)
				cout << ans[i] << endl;
		}
	}
}
accuracy: 1
time_ms: 2620
memory_kb: 15000
final_rank: 7

code_snippet: // ACM-ICPCà\I2010 F. Ã¢L¯

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

const int INF = 100000000;

int d;
string mes, cur;
vector<string> piece;
vector<int> connect[30][20];
vector<string> ans;
set<string> mem[43];
int dp[43][43];

bool editDist(int s, int e){
	for(int i=s+1;i<=e;i++){
		int m = INF;
		for(int j=max(1,i-d);j<=min((int)mes.size(),i+d);j++){
			if(cur[i-1]==mes[j-1]) dp[i][j] = dp[i-1][j-1];
			else {
				dp[i][j] = dp[i-1][j-1]+1;
				dp[i][j] = min(dp[i][j], dp[i-1][j]+1);
				dp[i][j] = min(dp[i][j], dp[i][j-1]+1);
			}
			m = min(m, dp[i][j]);
		}
		if(m > d) return false;
	}
	return true;
}

void dfs(int pIdx){
	int len = piece[0].size();
	int csize = cur.size();
	if(dp[csize][mes.size()] <= d) ans.push_back(cur);
	for(int j=len-1;j>=0;j--){
		int nsize = csize + len - j;
		if(nsize > mes.size() + d) break;
		cur += ' ';
		for(int i=0;i<connect[pIdx][j].size();i++){
			bool flag = true;
			for(int k=j;k<len;k++){
				cur[csize+k-j] = piece[connect[pIdx][j][i]][k];
				if(!editDist(csize+k-j, csize+k-j+1)){
					flag = false;
					break;
				}
			}
			if(!flag) continue;
			if(mem[nsize].count(cur)) continue;
			mem[nsize].insert(cur);
			dfs(connect[pIdx][j][i]);
		}
	}
	cur = cur.substr(0, csize);
}

int main(){
	int n, len;
	for(int i=0;i<=42;i++) dp[i][0] = dp[0][i] = i;
	while(cin >> d >> n, n){
		cin >> mes;
		piece.clear();
		for(int i=0;i<n;i++){
			string str; cin >> str;
			piece.push_back(str);
		}
		sort(piece.begin(), piece.end());
		piece.erase(unique(piece.begin(), piece.end()), piece.end());
		len = piece[0].size();
		for(int i=1;i<=mes.size()+d;i++)
			for(int j=1;j<=mes.size()+d;j++)
				if(abs(i-j) > d) dp[i][j] = INF;
		for(int i=0;i<piece.size();i++){
			for(int k=0;k<len;k++){
				connect[i][k].clear();
				for(int j=0;j<piece.size();j++){
					if(piece[i].substr(len-k) == piece[j].substr(0,k))
						connect[i][k].push_back(j);
				}
			}
		}
		for(int i=0;i<=mes.size()+d;i++)
			mem[i].clear();
		ans.clear();
		for(int i=0;i<piece.size();i++){
			cur = piece[i];
			if(!editDist(0, len)) continue;
			dfs(i);
		}
		sort(ans.begin(), ans.end());
		cout << ans.size() << endl;
		if(ans.size() <= 5){
			for(int i=0;i<ans.size();i++)
				cout << ans[i] << endl;
		}
	}
}
accuracy: 1
time_ms: 3880
memory_kb: 15000
final_rank: 9

code_snippet: // ACM-ICPCà\I2010 F. Ã¢L¯

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

const int INF = 100000000;

int d;
string mes, cur;
vector<string> piece;
vector<int> connect[30][20];
vector<string> ans;
set<string> mem[43];
int dp[43][43];
int cnt = 0;

bool editDist(int s, int e){
	for(int i=s+1;i<=e;i++){
		int m = INF;
		for(int j=max(1,i-d);j<=min((int)mes.size(),i+d);j++){
			if(cur[i-1]==mes[j-1]) dp[i][j] = dp[i-1][j-1];
			else {
				dp[i][j] = dp[i-1][j-1]+1;
				dp[i][j] = min(dp[i][j], dp[i-1][j]+1);
				dp[i][j] = min(dp[i][j], dp[i][j-1]+1);
			}
			m = min(m, dp[i][j]);
		}
		if(m > d) return false;
	}
	return true;
}

void solve(int pIdx){
	cnt++;
	int len = piece[0].size();
	int csize = cur.size();
	for(int j=len-1;j>=0;j--){
		int nsize = csize + len - j;
		if(nsize > mes.size() + d) break;
		cur += ' ';
		for(int i=0;i<connect[pIdx][j].size();i++){
			bool flag = true;
			for(int k=j;k<len;k++){
				cur[csize+k-j] = piece[connect[pIdx][j][i]][k];
				if(!editDist(csize+k-j, csize+k-j+1)){
					flag = false;
					break;
				}
			}
			if(!flag) continue;
			if(mem[nsize].count(cur)) continue;
			mem[nsize].insert(cur);
//			if(!editDist(csize, nsize)) continue;
			if(mes.size()-d <= nsize && nsize <= mes.size()+d && dp[nsize][mes.size()] <= d)
				ans.push_back(cur);
			solve(connect[pIdx][j][i]);
		}
	}
	cur = cur.substr(0, csize);
}

int main(){
	int n, len;
	for(int i=0;i<=42;i++) dp[i][0] = dp[0][i] = i;
	while(cin >> d >> n, n){
		cin >> mes;
		piece.clear();
		for(int i=0;i<n;i++){
			string str; cin >> str;
			piece.push_back(str);
		}
		sort(piece.begin(), piece.end());
		piece.erase(unique(piece.begin(), piece.end()), piece.end());
		len = piece[0].size();
		for(int i=1;i<=mes.size()+d;i++)
			for(int j=1;j<=mes.size()+d;j++)
				if(abs(i-j) > d) dp[i][j] = INF;
		for(int i=0;i<piece.size();i++){
			for(int k=0;k<len;k++){
				connect[i][k].clear();
				for(int j=0;j<piece.size();j++){
					if(piece[i].substr(len-k) == piece[j].substr(0,k))
						connect[i][k].push_back(j);
				}
			}
		}
		for(int i=0;i<=mes.size()+d;i++)
			mem[i].clear();
		ans.clear();
		cnt = 0;
		for(int i=0;i<piece.size();i++){
			cur = piece[i];
			if(!editDist(0, len)) continue;
			if(mes.size()-d <= cur.size() && cur.size() <= mes.size()+d && dp[cur.size()][mes.size()] <= d)
				ans.push_back(cur);
			solve(i);
		}
		sort(ans.begin(), ans.end());
		ans.erase(unique(ans.begin(), ans.end()), ans.end());
		cout << ans.size() << endl;
		if(ans.size() <= 5){
			for(int i=0;i<ans.size();i++)
				cout << ans[i] << endl;
		}
	}
}


/*
string mes;
vector<string> piece;
vector< pair<int,int> > connect[30];
vector<string> ans;
set<string> mem;
int dp[43][43];
int cnt = 0;

bool editDist(string cur, int s, int e, int d){
	for(int i=s+1;i<=e;i++){
		int m = INF;
		for(int j=max(1,i-d);j<=min((int)mes.size(),i+d);j++){
			if(cur[i-1]==mes[j-1]) dp[i][j] = dp[i-1][j-1];
			else {
				dp[i][j] = dp[i-1][j-1]+1;
				dp[i][j] = min(dp[i][j], dp[i-1][j]+1);
				dp[i][j] = min(dp[i][j], dp[i][j-1]+1);
			}
			m = min(m, dp[i][j]);
		}
		if(m > d) return false;
	}
	return true;
}

void solve(string cur, int d, int pIdx, int p1, int p2){
	cnt++;
	int len = piece[0].size();
	for(int i=0;i<connect[pIdx].size();i++){
		int nsize = cur.size() + len - connect[pIdx][i].second;
		if(nsize > mes.size() + d) continue;
//		string nxt = cur + piece[connect[pIdx][i].first].substr(connect[pIdx][i].second);
		if(mem.count(nxt)) continue;
		mem.insert(nxt);
		if(!editDist(nxt, cur.size(), nxt.size(), d)) continue;
		if(mes.size()-d <= nxt.size() && nxt.size() <= mes.size()+d && dp[nxt.size()][mes.size()] <= d)
			ans.push_back(nxt);
		solve(nxt, d, connect[pIdx][i].first, p2, nxt.size());
	}
}

int main(){
	int d, n, len;
	for(int i=0;i<=42;i++) dp[i][0] = dp[0][i] = i;
	while(cin >> d >> n, n){
		cin >> mes;
		piece.clear();
		for(int i=0;i<n;i++){
			string str; cin >> str;
			piece.push_back(str);
		}
		sort(piece.begin(), piece.end());
		piece.erase(unique(piece.begin(), piece.end()), piece.end());
		len = piece[0].size();
		for(int i=1;i<=mes.size()+d;i++)
			for(int j=1;j<=mes.size()+d;j++)
				if(abs(i-j) > d) dp[i][j] = INF;
		for(int i=0;i<piece.size();i++){
			connect[i].clear();
			for(int j=0;j<piece.size();j++){
				connect[i].push_back(make_pair(j,0));
				for(int k=1;k<len;k++){
					if(piece[i].substr(len-k) == piece[j].substr(0,k))
						connect[i].push_back(make_pair(j,k));
				}
			}
		}
		mem.clear();
		string cur;
		ans.clear();
		cnt = 0;
		for(int i=0;i<piece.size();i++){
			cur = piece[i];
			if(!editDist(cur, 0, len, d)) continue;
			if(mes.size()-d <= cur.size() && cur.size() <= mes.size()+d && dp[cur.size()][mes.size()] <= d)
				ans.push_back(cur);
			solve(cur, d, i, len, 0);
		}
		sort(ans.begin(), ans.end());
		ans.erase(unique(ans.begin(), ans.end()), ans.end());
		cout << ans.size() << endl;
		if(ans.size() <= 5){
			for(int i=0;i<ans.size();i++)
				cout << ans[i] << endl;
		}
	}
}
*/
accuracy: 1
time_ms: 4060
memory_kb: 15000
final_rank: 10