Pagini recente » Cod sursa (job #2776952) | Cod sursa (job #210818) | Cod sursa (job #1716620) | Cod sursa (job #496630) | Cod sursa (job #2433718)
#include <iostream>
#include<fstream>
using namespace std;
ifstream in("adn.in");
ofstream out("adn.out");
string string_list[18];
int numberofstrings=0;
void read()
{
in>>numberofstrings;
for(int i=0; i<numberofstrings; i++)
{
in>>string_list[i];
}
}
string merge_strings(string a,string b)
{
int cursor=0;
for(int i=0; i<a.length(); i++)
{
if(cursor==b.length())
{
cursor=0;
}
if(a[i]==b[cursor])
{
cursor++;
}
else
{
cursor=0;
if(a[i]==b[0])
cursor++;
}
}
string substring=a.substr(0,a.length()-cursor);
substring+=b;
return substring;
}
bool used[18] {false};
string minimum_string="";
void backtrack(int depth,string to_merge)
{
if(depth==numberofstrings)
{
if(to_merge.length()<minimum_string.length() ||minimum_string=="")
minimum_string=to_merge;
return;
}
for(int i=0; i<numberofstrings; i++)
{
if(!used[i])
{
used[i]=true;
backtrack(depth+1,merge_strings(to_merge,string_list[i]));
used[i]=false;
}
}
}
int main()
{
read();
backtrack(0,"");
out<<minimum_string;
}