Language: C++

#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
#define INF (1<<29)
typedef unsigned __int64 ull;

//#define MOD 1000000003
#define FIND(h) (binary_search(piece.begin(),piece.end(),h))
const int B=103;
vector<ull> piece;
int pLen;
vector<string> ans;
const char C[27]=".ABCDEFGHIJKLMNOPQRSTUVWXYZ";

bool match(string &s){
	ull h=0,b=1;
	for(int i=0;i<pLen;i++){
		h = (h*B+s[i]);
		b*=B;
	}
	if(!FIND(h))return false;
	int cover=pLen-1;
	for(int i=pLen;i<s.size();i++){
		h = (h*B-(s[i-pLen])*b+s[i]);
		if(FIND(h))cover = i;
		else if(pLen==i-cover)return false;
	}
	return cover==s.size()-1;
}
bool check(string &s){
	ull h=0,b=1;
	for(int i=0;i<pLen;i++){
		h = (h*B+s[i]);
		b*=B;
	}
	if(!FIND(h))return false;
	int cover=pLen-1;
	int minp=INF,maxp=-1;
	for(int i=pLen;i<s.size();i++){
		h = (h*B-(s[i-pLen])*b+s[i]);
		if(FIND(h))cover = i;
		else if(pLen==i-cover){
			maxp=max(maxp,i);
			if(minp==INF)minp=i;
		}
	}
	return maxp-minp+1 < 2*pLen;
}
void search(string &s,int d){
	if(d==0){
		//cout<<s<<endl;
		if(match(s))ans.push_back(s);
		return;
	}
	if(d==1 && !check(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.begin(),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(){
	int d,n;
	while(cin>>d>>n,d|n){
		string s;
		cin>>s;
		piece.clear();
		for(int i=0;i<n;i++){
			string t;
			cin>>t;
			ull h=0;
			for(int j=0;j<t.size();j++){
				h = (h*B+t[j]);
			}
			piece.push_back(h);
			pLen=t.size();
		}
		sort(piece.begin(),piece.end());
		ans.clear();
		search(s,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;
		}
	}
	return 0;
}

Compiler output:
a.cc:9:26: error: expected initializer before 'ull'
    9 | typedef unsigned __int64 ull;
      |                          ^~~
a.cc:14:8: error: 'ull' was not declared in this scope
   14 | vector<ull> piece;
      |        ^~~
a.cc:14:11: error: template argument 1 is invalid
   14 | vector<ull> piece;
      |           ^
a.cc:14:11: error: template argument 2 is invalid
a.cc:17:18: error: initializer-string for 'const char [27]' is too long [-fpermissive]
   17 | const char C[27]=".ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      |                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc: In function 'bool match(std::string&)':
a.cc:20:9: error: 'ull' was not declared in this scope
   20 |         ull h=0,b=1;
      |         ^~~
a.cc:22:17: error: 'h' was not declared in this scope
   22 |                 h = (h*B+s[i]);
      |                 ^
a.cc:23:17: error: 'b' was not declared in this scope
   23 |                 b*=B;
      |                 ^
a.cc:12:38: error: request for member 'begin' in 'piece', which is of non-class type 'int'
   12 | #define FIND(h) (binary_search(piece.begin(),piece.end(),h))
      |                                      ^~~~~
a.cc:25:13: note: in expansion of macro 'FIND'
   25 |         if(!FIND(h))return false;
      |             ^~~~
a.cc:12:52: error: request for member 'end' in 'piece', which is of non-class type 'int'
   12 | #define FIND(h) (binary_search(piece.begin(),piece.end(),h))
      |                                                    ^~~
a.cc:25:13: note: in expansion of macro 'FIND'
   25 |         if(!FIND(h))return false;
      |             ^~~~
a.cc:25:18: error: 'h' was not declared in this scope
   25 |         if(!FIND(h))return false;
      |                  ^
a.cc:12:58: note: in definition of macro 'FIND'
   12 | #define FIND(h) (binary_search(piece.begin(),piece.end(),h))
      |                                                          ^
a.cc:28:17: error: 'h' was not declared in this scope
   28 |                 h = (h*B-(s[i-pLen])*b+s[i]);
      |                 ^
a.cc:28:38: error: 'b' was not declared in this scope
   28 |                 h = (h*B-(s[i-pLen])*b+s[i]);
      |                                      ^
a.cc:12:38: error: request for member 'begin' in 'piece', which is of non-class type 'int'
   12 | #define FIND(h) (binary_search(piece.begin(),piece.end(),h))
      |                                      ^~~~~
a.cc:29:20: note: in expansion of macro 'FIND'
   29 |                 if(FIND(h))cover = i;
      |                    ^~~~
a.cc:12:52: error: request for member 'end' in 'piece', which is of non-class type 'int'
   12 | #define FIND(h) (binary_search(piece.begin(),piece.end(),h))
      |                                                    ^~~
a.cc:29:20: note: in expansion of macro 'FIND'
   29 |                 if(FIND(h))cover = i;
      |                    ^~~~
a.cc: In function 'bool check(std::string&)':
a.cc:35:9: error: 'ull' was not declared in this scope
   35 |         ull h=0,b=1;
      |         ^~~
a.cc:37:17: error: 'h' was not declared in this scope
   37 |                 h = (h*B+s[i]);
      |                 ^
a.cc:38:17: error: 'b' was not declared in this scope
   38 |                 b*=B;
      |                 ^
a.cc:12:38: error: request for member 'begin' in 'piece', which is of non-class type 'int'
   12 | #define FIND(h) (binary_search(piece.begin(),piece.end(),h))
      |                                      ^~~~~
a.cc:40:13: note: in expansion of macro 'FIND'
   40 |         if(!FIND(h))return false;
      |             ^~~~
a.cc:12:52: error: request for member 'end' in 'piece', which is of non-class type 'int'
   12 | #define FIND(h) (binary_search(piece.begin(),piece.end(),h))
      |                                                    ^~~
a.cc:40:13: note: in expansion of macro 'FIND'
   40 |         if(!FIND(h))return false;
      |             ^~~~
a.cc:40:18: error: 'h' was not declared in this scope
   40 |         if(!FIND(h))return false;
      |                  ^
a.cc:12:58: note: in definition of macro 'FIND'
   12 | #define FIND(h) (binary_search(piece.begin(),piece.end(),h))
      |                                                          ^
a.cc:44:17: error: 'h' was not declared in this scope
   44 |                 h = (h*B-(s[i-pLen])*b+s[i]);
      |                 ^
a.cc:44:38: error: 'b' was not declared in this scope
   44 |                 h = (h*B-(s[i-pLen])*b+s[i]);
      |                                      ^
a.cc:12:38: error: request for member 'begin' in 'piece', which is of non-class type 'int'
   12 | #define FIND(h) (binary_search(piece.begin(),piece.end(),h))
      |                                      ^~~~~
a.cc:45:20: note: in expansion of macro 'FIND'
   45 |                 if(FIND(h))cover = i;
      |                    ^~~~
a.cc:12:52: error: request for member 'end' in 'piece', which is of non-class type 'int'
   12 | #define FIND(h) (binary_search(piece.begin(),piece.end(),h))
      |                                                    ^~~
a.cc:45:20: note: in expansion of macro 'FIND'
   45 |                 if(FIND(h))cover = i;
      |                    ^~~~
a.cc: In function 'void search(std::string&, int)':
a.cc:81:17: error: no matching function for call to 'std::__cxx11::basic_string<char>::assign(std::__cxx11::basic_string<char>::iterator, int, std::__cxx11::basic_string<char>::size_type)'
   81 |         t.assign(s.begin(),1,s.size()-1);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/string:54,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/basic_string.h:1728:9: note: candidate: 'template<class _InputIterator, class> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::assign(_InputIterator, _InputIterator) [with <template-parameter-2-2> = _InputIterator; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]'
 1728 |         assign(_InputIterator __first, _InputIterator __last)
      |         ^~~~~~
/usr/include/c++/14/bits/basic_string.h:1728:9: note:   candidate expects 2 arguments, 3 provided
/usr/include/c++/14/bits/basic_string.h:1787:9: note: candidate: 'template<class _Tp> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_If_sv<_Tp, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::assign(const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]'
 1787 |         assign(const _Tp& __svt)
      |         ^~~~~~
/usr/include/c++/14/bits/basic_string.h:1787:9: note:   candidate expects 1 argument, 3 provided
/usr/include/c++/14/bits/basic_string.h:1803:9: note: candidate: 'template<class _Tp> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_If_sv<_Tp, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::assign(const _Tp&, size_type, size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]'
 1803 |         assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
      |         ^~~~~~
/usr/include/c++/14/bits/basic_string.h:1803:9: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/14/bits/move.h:37,
                 from /usr/include/c++/14/bits/exception_ptr.h:41,
                 from /usr/include/c++/14/exception:166,
                 from /usr/include/c++/14/ios:41:
/usr/include/c++/14/type_traits: In substitution of 'template<bool _Cond, class _Tp> using std::enable_if_t = typename std::enable_if::type [with bool _Cond = false; _Tp = std::__cxx11::basic_string<char>&]':
/usr/include/c++/14/bits/basic_string.h:149:8:   required by substitution of 'template<class _CharT, class _Traits, class _Alloc> template<class _Tp, class _Res> using std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_If_sv = std::enable_if_t<((bool)std::__and_<std::is_convertible<const _Tp&, std::basic_string_view<_CharT, _Traits> >, std::__not_<std::is_convertible<const _Tp*, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>*> >, std::__not_<std::is_convertible<const _Tp&, const _CharT*> > >::value), _Res> [with _Tp = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; _Res = std::__cxx11::basic_string<char>&; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]'
  149 |         using _If_sv = enable_if_t<
      |               ^~~~~~
/usr/include/c++/14/bits/basic_string.h:1803:2:   required by substitution of 'template<class _Tp> std::__cxx11::basic_string<char>::_If_sv<_Tp, std::__cxx11::basic_string<char>&> std::__cxx11::basic_string<char>::assign(const _Tp&, size_type, size_type) [with _Tp = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >]'
 1803 |         assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
      |         ^~~~~~
a.cc:81:10:   required from here
   81 |         t.assign(s.begin(),1,s.size()-1);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/type_traits:2711:11: error: no type named 'type' in 'struct std::enable_if<false, std::__cxx11::basic_string<char>&>'
 2711 |     using enable_if_t = typename enable_if<_Cond, _Tp>::type;
      |           ^~~~~~~~~~~
/usr/include/c++/14/bits/basic_string.h:1588:7: note: candidate: 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::assign(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]'
 1588 |       assign(const basic_string& __str)
      |       ^~~~~~
/usr/include/c++/14/bits/basic_string.h:1588:7: note:   candidate expects 1 argument, 3 provided
/usr/include/c++/14/bits/basic_string.h:1634:7: note: candidate: 'std::__cxx11: