Cod sursa(job #492847)

Utilizator hadesgamesTache Alexandru hadesgames Data 16 octombrie 2010 09:10:52
Problema ADN Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.87 kb
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <algorithm>
#include <utility>
#include <string>
#include <functional>
#include <sstream>
#include <fstream>
#include <iostream>
using namespace std;
#define FOR(i,a,b) for (i=a;i<=b;i++)
#define fori(it,v) for (it=(v).begin();it!=(v).end();it++)
#define pb push_back
#define mp make_pair
#define fs first
#define ss second
#define all(c) c.begin(),c.end()
#define pf push_front
#define popb pop_back
#define popf pop_front
ifstream in("adn.in");
ofstream out("adn.out");
vector<string> a;
vector<bool>ver;
int d[20][20];
int pi[40000];
int b[1<<18][19];
int main()
{
	int i,n,j,t,k,maxv;
	in>>n;
	a.resize(n+1);
	FOR(i,1,n)
	{
		in>>a[i];
	}
	ver.resize(n+3,1);
	FOR(i,1,n)
	{
		FOR(j,1,n)
			if(i!=j)
			{
				k=0;
				pi[0]=0;
				bool narghilea=1;
				FOR(t,1,a[i].size()-1)
					pi[t]=0;
				//cout<<a[i];
				FOR(t,1,a[i].size()-1)
				{
					while (k>0&&a[i][k]!=a[i][t])
						k=pi[k-1];
					if (a[i][k]==a[i][t])
						k++;	
					pi[t]=k;
				}
				//FOR(t,0,a[i].size()-1)
				//	cout<<pi[t]<<' ';
			//	cout<<'\n';
				k=0;
				if (a[i][0]==a[j][0])
					k=1;
				FOR(t,1,a[j].size()-1)
				{
					while(k>0&&(a[i][k]!=a[j][t]))
						k=pi[k-1];
					if (a[i][k]==a[i][t])
						k++;
					cout<<k<<' ';
					if (k==a[i].size())
					{
						ver[i]=0;
						n--;
						narghilea=0;
						break;
					}

				}	
				cout<<'\n';
				if (narghilea)
				{
					d[i][j]=k;
				}
				else
				{
					break;
				}
			}
	}
	FOR(i,1,n)
	{
		FOR(j,1,n)
			cout<<d[i][j]<<' ';
		cout<<'\n';
	}
	int aux=(1<<n)-1;
	FOR(i,1,aux)
	{
		FOR(j,1,n)
		if (!(i&(1<<(j-1)))&&ver[j])
		{
			b[i][j]=0;
			FOR(t,1,n)
				if(ver[t]&&(1&(1<<(t-1))))
					b[i][j]=max(b[i][j],b[i-(1<<(t-1))][t]);

		}
	}
	maxv=-1;
	FOR(i,1,n)
		maxv=max(maxv,b[(1<<n)-1-(1<<(i-1))][i]);
	out<<maxv;
	in.close();
	out.close();
	return 0;
}