Language: C++


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


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

const int N = 11;

int n;
vector<string> in;

bool input(){
  cin>>n;
  if(n==0)return false;
  in.clear();
  rep(i,n){
    string a;
    cin>>a;
    in.push_back(a);
  }
  return true;
}


string ans;
void update(string& a){
  if(ans=="")ans=a;
  else if(ans.size()>a.size())ans = a;
  else if(ans.size()==a.size() && ans>a)ans=a;
}

string connect(string& a, string& b){
  reps(len, 1, min(a.size(),b.size())+1){
    bool ok = true;    
    rep(j,len){
      if(a[a.size()-len+j]!=b[j])ok=false;
    }
    if(ok){
      return a + b.substr(len);
    }
  }
  return "";
}

int index[N];
int match[N][N];
void saiki(int depth){

  /*
  printf("detph=%d ",depth);
  rep(i,depth)printf("%d ",index[i]);puts("");
  */

  if(depth==n){
    string rs = in[index[0]];
    bool ok = true;
    reps(i,1,n){
      if(match[index[i-1]][index[i]]==0)ok=false;;
      rs = connect(rs, in[index[i]]);
    }
    //rep(i,n)printf("%d ",index[i]);
    //cout<<rs<<endl;
    if(ok)update(rs);
    return;
  }

  rep(i,n){
    bool ok = true;
    rep(j,depth){
      if(index[j]==i)ok=false;
    }
    if(depth!=0 && match[index[depth-1]][i]==0)ok = false;
    if(ok){
      index[depth]=i;
      saiki(depth+1);
    }
  }
}

string solve(){
  ans = "";

  rep(i,n){
    rep(j,n){
      if(i==j){
	match[i][j]=0;
	continue;
      }
      string s = connect(in[i],in[j]);
      //cout<<in[i]<<" "<<in[j]<<" "<<s<<" "<<endl;
      if(s.size()==0)match[i][j]=0;
      else match[i][j]=in[i].size()+in[j].size()-s.size();
    }
  }

  
  rep(i,n){
    //rep(j,n)printf("%d ",match[i][j]);puts("");
  }

  saiki(0);
  return ans;
}


int main(){while(input())cout<<solve()<<endl;}

Compiler output:
a.cc:47:12: error: 'int index [11]' redeclared as different kind of entity
   47 | int index[N];
      |            ^
In file included from /usr/include/string.h:462,
                 from /usr/include/c++/14/cstring:43,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:121,
                 from a.cc:2:
/usr/include/strings.h:50:20: note: previous declaration 'const char* index(const char*, int)'
   50 | extern const char *index (const char *__s, int __c)
      |                    ^~~~~
a.cc: In function 'void saiki(int)':
a.cc:57:25: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   57 |     string rs = in[index[0]];
      |                         ^
a.cc:60:21: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   60 |       if(match[index[i-1]][index[i]]==0)ok=false;;
      |                     ^
a.cc:60:33: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   60 |       if(match[index[i-1]][index[i]]==0)ok=false;;
      |                                 ^
a.cc:61:32: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   61 |       rs = connect(rs, in[index[i]]);
      |                                ^
a.cc:72:15: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   72 |       if(index[j]==i)ok=false;
      |               ^
a.cc:74:31: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   74 |     if(depth!=0 && match[index[depth-1]][i]==0)ok = false;
      |                               ^
a.cc:76:12: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   76 |       index[depth]=i;
      |            ^