NOIP 2000 单词接龙 解题报告

  数据不大,直接枚举……嗯,然后就是,比如:1212和1212接龙之后是121212,而不是12121212,就这个注意的,代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char str[20][1001];
char tmp[1001];
int f[20][20];
int used[20];
int ans;
int n;

void srch(int k, int len)
{
	int i;
	if(len > ans){
		ans = len;
	}
	used[k]++;
	for(i = 0; i < n; i++){
		if(f[k][i] > 0 && used[i] < 2){
			srch(i, len + f[k][i]);
		}
	}
	used[k]--;
}

int main(int argc, char **argv)
{
	int i, j;
	char *p, c;
	scanf("%d", &n);
	for(i = 0; i < n; i++){
		scanf("%s\n", str[i]);
	}
	for(i = 0; i < n; i++){
		for(j = 0; j < n; j++){
			strcpy(tmp, str[i]);
			p = &tmp[strlen(tmp) - 1];
			while(p != tmp){
				if(*p == str[j][0] && strncmp(p, str[j], strlen(p)) == 0){
					f[i][j] = strlen(str[j]) - strlen(p);
					break;
				}
				p--;
			}
		}
	}
	scanf("%c", &c);
	for(i = 0; i < n; i++){
		if(str[i][0] == c){
			srch(i, strlen(str[i]));
		}
	}
	printf("%d\n", ans);
	return 0;
}

  

posted @ 2011-08-17 17:06  zqynux  阅读(1614)  评论(0编辑  收藏  举报