POJ 2817 WordStack (暴力)

WordStack
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2940   Accepted: 1021

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

Source


先预处理 ,在next_permutation=暴力计算

代码:

#include <iostream>
#include <string>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

int n,score[20][20],ans;
char str[20][20];
bool visited[20];

int lcc(int a,int b){
	int c=0,len1=strlen(str[a]),len2=strlen(str[b]);
	for(int i=0;i<len1;i++){
		for(int j=0;j<len2;j++){
			int tmp=0,t1=i,t2=j;
			while(t1<len1 && t2<len2){
				if(str[a][t1]==str[b][t2]) tmp++;
				t1++;t2++;
			}
			if(tmp>c) c=tmp;
		}
	}
	return c;
}

void initial(){
	for(int i=0;i<n;i++) visited[i]=false;
	for(int i=0;i<=n;i++)
	for(int j=0;j<=n;j++)
	score[i][j]=0;
}

void input(){
	for(int i=0;i<n;i++) scanf("%s",str[i]);
	for(int i=0;i<n;i++){
		for(int j=i+1;j<n;j++){
			score[i][j]=lcc(i,j);
			score[j][i]=score[i][j];
			//cout<<str[i]<<" "<<str[j]<<" "<<score[i][j]<<endl; 
		}
	}
}

void computing(){
	ans=0;
	vector <int> v;
	for(int i=0;i<n;i++) v.push_back(i);
	do{
		int tmp=0;
		for(int i=0;i<n-1;i++){
			tmp+=score[v[i]][v[i+1]];
		}
		if(tmp>ans) ans=tmp;
	}while( next_permutation(v.begin(),v.end()) );
	printf("%d\n",ans);
}

int main(){
	while(scanf("%d",&n)!=EOF && n>0){
		initial();
		input();
		computing();
	}
	return 0;
}


回溯法超时,不知道why,代码:

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

int n,score[20][20],ans;
char str[20][20];
bool visited[20];

int lcs(int a,int b){
	int c=0,len1=strlen(str[a]),len2=strlen(str[b]);
	for(int i=0;i<len1;i++){
		for(int j=0;j<len2;j++){
			int tmp=0,t1=i,t2=j;
			while(t1<len1 && t2<len2){
				if(str[a][t1]==str[b][t2]) tmp++;
				t1++;t2++;
			}
			if(tmp>c) c=tmp;
		}
	}
	return c;
}

void initial(){
	for(int i=0;i<n;i++) visited[i]=false;
	for(int i=0;i<=n;i++)
	for(int j=0;j<=n;j++)
	score[i][j]=0;
}

void input(){
	for(int i=0;i<n;i++) scanf("%s",str[i]);
	for(int i=0;i<n;i++){
		for(int j=i+1;j<n;j++){
			score[i][j]=lcs(i,j);
			score[j][i]=score[i][j];
			//cout<<str[i]<<" "<<str[j]<<" "<<score[i][j]<<endl; 
		}
	}
}

void backtracking(int s,int sum,int k){
	if(k>=n){
		//cout<<sum<<endl;
		if(sum>ans) ans=sum;
		return ;
	}
	for(int i=0;i<n;i++){
		if( !visited[i] ){
			visited[i]=true;
			//cout<<str[i]<<"->";
			backtracking(i,sum+score[s][i],k+1);
			visited[i]=false;
		}
	}
}

void computing(){
	ans=0;
	for(int i=0;i<n;i++){
		visited[i]=true;
		//cout<<str[i]<<"->";
		backtracking(i,0,1);
		visited[i]=false;
	}
	printf("%d\n",ans);
}

int main(){
	while(scanf("%d",&n)!=EOF && n>0){
		initial();
		input();
		computing();
	}
	return 0;
}


坑爹,回溯稍微减一下 坑爹的 枝就过了,下面代码:

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

int n,score[20][20],ans;
char str[20][20];
bool visited[20];

int lcs(int a,int b){
	int c=0,len1=strlen(str[a]),len2=strlen(str[b]);
	for(int i=0;i<len1;i++){
		for(int j=0;j<len2;j++){
			int tmp=0,t1=i,t2=j;
			while(t1<len1 && t2<len2){
				if(str[a][t1]==str[b][t2]) tmp++;
				t1++;t2++;
			}
			if(tmp>c) c=tmp;
		}
	}
	return c;
}

void initial(){
	for(int i=0;i<n;i++) visited[i]=false;
	for(int i=0;i<=n;i++)
	for(int j=0;j<=n;j++)
	score[i][j]=0;
}

void input(){
	for(int i=0;i<n;i++) scanf("%s",str[i]);
	for(int i=0;i<n;i++){
		for(int j=i+1;j<n;j++){
			score[i][j]=lcs(i,j);
			score[j][i]=score[i][j];
			//cout<<str[i]<<" "<<str[j]<<" "<<score[i][j]<<endl; 
		}
	}
}

void backtracking(int s,int sum,int k){
	if(sum+(n-k)*n < ans) return;//此处减一下傻逼的支
	if(k>=n){
		//cout<<sum<<endl;
		if(sum>ans) ans=sum;
		return ;
	}
	for(int i=0;i<n;i++){
		if( !visited[i] ){
			visited[i]=true;
			//cout<<str[i]<<"->";
			backtracking(i,sum+score[s][i],k+1);
			visited[i]=false;
		}
	}
}

void computing(){
	ans=0;
	for(int i=0;i<n;i++){
		visited[i]=true;
		//cout<<str[i]<<"->";
		backtracking(i,0,1);
		visited[i]=false;
	}
	printf("%d\n",ans);
}

int main(){
	while(scanf("%d",&n)!=EOF && n>0){
		initial();
		input();
		computing();
	}
	return 0;
}




posted @ 2013-10-06 16:26  炒饭君  阅读(225)  评论(0编辑  收藏  举报