poj 2817(坑爹的回溯)

WordStack
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2948 Accepted: 1026

Description

As editor of a small-town newspaper, you know that a substantial number of your readers enjoy the daily word games that you publish, but that some are getting tired of the conventional crossword puzzles and word jumbles that you have been buying for years. You decide to try your hand at devising a new puzzle of your own.

Given a collection of N words, find an arrangement of the words that divides them among N lines, padding them with leading spaces to maximize the number of non-space characters that are the same as the character immediately above them on the preceding line. Your score for this game is that number.

Input

Input data will consist of one or more test sets.

The first line of each set will be an integer N (1 <= N <= 10) giving the number of words in the test case. The following N lines will contain the words, one word per line. Each word will be made up of the characters 'a' to 'z' and will be between 1 and 10 characters long (inclusive).

End of input will be indicated by a non-positive value for N .

Output

Your program should output a single line containing the maximum possible score for this test case, printed with no leading or trailing spaces.

Sample Input

5 
abc 
bcd 
cde 
aaa 
bfcde 
0

Sample Output

8

Hint

Note: One possible arrangement yielding this score is:
aaa 

abc 

 bcd

  cde 

bfcde
 
 
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;

const int maxn = 11;
int tem[maxn] , match[maxn][maxn] , vis[maxn];
string word[maxn];
int N , ans;

void initial(){
	for(int i = 0;i < maxn;i++){
		tem[i] = 0;
		word[i].clear();
		vis[i] = 0;
		for(int j = 0;j < maxn;j++){
			match[i][j] = 0;
		}
	}
	ans = 0;
}

void readcase(){
	for(int i = 0;i < N;i++){
		cin >> word[i];
	}
}

int get_match(int n , int m){
	int M = 0 , len1 = word[n].length() , len2 = word[m].length();
	for(int k = 0;k < len1;k++){
		int i = k , j = 0 , tem = 0;
		while(i < len1 && j < len2){
			if(word[n][i] == word[m][j]){
				tem++;
			}
			i++;
			j++;
		}
		M = max(M , tem);
	}
	for(int k = 0;k < len2;k++){
		int i = 0 , j = k , tem = 0;
		while(i < len1 && j < len2){
			if(word[n][i] == word[m][j]){
				tem++;
			}
			i++;
			j++;
		}
		M = max(M , tem);
	}
	return M;
}

void dfs(int i , int sum){
	if(sum+(N-i)*N <= ans){
		return;
	}//没这句话就超时!!
	if(i < N){
		for(int k = 0;k < N;k++){
			if(vis[k] == 0){
				vis[k] = 1;
				sum += match[tem[i-1]][k];
				tem[i] = k;
				dfs(i+1 , sum);
				sum -= match[tem[i-1]][k];
				vis[k] = 0;
			}
		}
	}
	ans = max(ans , sum);
}

void computing(){
	for(int i = 0;i < N;i++){
		for(int j = i+1;j < N;j++){
			match[i][j] = get_match(i , j);
			match[j][i] = match[i][j];
		}
	}
	for(int i = 0;i < N;i++){
		tem[0] = i;
		vis[i] = 1;
		dfs(1 , 0);
		vis[i] = 0;
	}
	printf("%d\n" , ans);
}


int main(){
	while(scanf("%d" , &N) && N > 0){
		initial();
		readcase();
		computing();
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值