Language: C++

#include <algorithm>
#include <string>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;

#define reps(i,f,n) for(int i=f; i<int(n); ++i)
#define rep(i,n) reps(i,0,n)

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1001001001;

int n;
int array[10];
vector<string> str;
string ans;

void dfs(int depth, char tmp[], int length)
{
	if(depth == n){
		if(ans.empty() || ans.size() > length || (ans.size()==length && ans>string(tmp)))
			ans = tmp;
		return;
	}
	
	reps(i, depth, n){
		int pre = length;
		swap(array[i], array[depth]);
		string& s = str[array[depth]];
		reps(j, max(0u, length-s.size()), length+1){
			bool ok = true;
			rep(k, length-j){
				if(tmp[j+k] != s[k]){
					ok = false;
					break;
				}
			}
			if(ok){
				rep(k, s.size()-length+j){
					tmp[length+k] = s[length-j+k];
				}
				length = s.size() + j;
				break;
			}
		}
		
		dfs(depth+1, tmp, length);
		swap(array[i], array[depth]);
		length = pre;
	}
}

int main()
{
	while(scanf("%d", &n), n){
		ans = "";
		str.resize(n);
		rep(i, n){
			char s[128];
			scanf("%s", s);
			str[i] = s;
		}
		rep(i, n)
			array[i] = i;
		
		char tmp[128];
		dfs(0, tmp, 0);
		puts(ans.c_str());
	}
}

Compiler output:
a.cc: In function 'void dfs(int, char*, int)':
a.cc:43:22: error: reference to 'array' is ambiguous
   43 |                 swap(array[i], array[depth]);
      |                      ^~~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:29:5: note:                 'int array [10]'
   29 | int array[10];
      |     ^~~~~
a.cc:43:32: error: reference to 'array' is ambiguous
   43 |                 swap(array[i], array[depth]);
      |                                ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:29:5: note:                 'int array [10]'
   29 | int array[10];
      |     ^~~~~
a.cc:44:33: error: reference to 'array' is ambiguous
   44 |                 string& s = str[array[depth]];
      |                                 ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:29:5: note:                 'int array [10]'
   29 | int array[10];
      |     ^~~~~
a.cc:45:28: error: no matching function for call to 'max(unsigned int, std::__cxx11::basic_string<char>::size_type)'
   45 |                 reps(j, max(0u, length-s.size()), length+1){
      |                         ~~~^~~~~~~~~~~~~~~~~~~~~
a.cc:18:31: note: in definition of macro 'reps'
   18 | #define reps(i,f,n) for(int i=f; i<int(n); ++i)
      |                               ^
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:45:28: note:   deduced conflicting types for parameter 'const _Tp' ('unsigned int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'})
   45 |                 reps(j, max(0u, length-s.size()), length+1){
      |                         ~~~^~~~~~~~~~~~~~~~~~~~~
a.cc:18:31: note: in definition of macro 'reps'
   18 | #define reps(i,f,n) for(int i=f; i<int(n); ++i)
      |                               ^
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:45:28: note:   mismatched types 'std::initializer_list<_Tp>' and 'unsigned int'
   45 |                 reps(j, max(0u, length-s.size()), length+1){
      |                         ~~~^~~~~~~~~~~~~~~~~~~~~
a.cc:18:31: note: in definition of macro 'reps'
   18 | #define reps(i,f,n) for(int i=f; i<int(n); ++i)
      |                               ^
a.cc:63:22: error: reference to 'array' is ambiguous
   63 |                 swap(array[i], array[depth]);
      |                      ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:29:5: note:                 'int array [10]'
   29 | int array[10];
      |     ^~~~~
a.cc:63:32: error: reference to 'array' is ambiguous
   63 |                 swap(array[i], array[depth]);
      |                                ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:29:5: note:                 'int array [10]'
   29 | int array[10];
      |     ^~~~~
a.cc: In function 'int main()':
a.cc:79:25: error: reference to 'array' is ambiguous
   79 |                         array[i] = i;
      |                         ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:29:5: note:                 'int array [10]'
   29 | int array[10];
      |     ^~~~~