Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

LCS动态规划做的,总是TLE,各种精简,最后1985ms险过,继续精简~

Posted by sunchy at 2014-08-13 16:05:40 on Problem 1035
//http://poj.org/problem?id=1035
#include <stdio.h>
#include <string.h>
char dic[10010][16] = { 0 };
char dichash[10010] = { 0 };
char C[16][16];
int matchedflag = 0;
char semimatched[10010][16] = { 0 };
int matchedcnt = 0;

void LCS(char *a, char *b, int al, int bl);

int Matched(char *a, char *b){
	int al = strlen(a);
	int bl = strlen(b);
	LCS(a, b, al, bl);
	int matchedlength = C[al][bl];
	matchedflag = 0;
	if (matchedlength == al && matchedlength == bl){
		matchedflag = 1;
	}
	else{
		if ((matchedlength == al && matchedlength == bl - 1) || (matchedlength == bl && matchedlength == al - 1)){
			strcpy(semimatched[matchedcnt++], b);
		}
		else if (matchedlength == al - 1 && matchedlength == bl - 1){
			int dismatchedcnt = 0;
			for (int j = 0; j < al; j++){
				if (a[j] != b[j])
					dismatchedcnt++;
			}
			if (dismatchedcnt == 1){
				strcpy(semimatched[matchedcnt++], b);
			}
		}
	}
	return matchedflag;
}

int main(){
	char temp[16] = { 0 };
	int cnt = 0;
	while (scanf("%s", temp) && (strcmp(temp, "#"))){
		strcpy(dic[cnt], temp);
		dichash[cnt] = strlen(temp);
		cnt++;
	}
	while (scanf("%s", temp) && (strcmp(temp, "#"))){
		memset(semimatched, 0, sizeof(semimatched));
		matchedcnt = 0;
		int templenth = strlen(temp);
		for (int i = 0; i < cnt; i++){
			if (dichash[i] == templenth - 1 || dichash[i] == templenth || dichash[i] == templenth + 1)
				if (Matched(temp, dic[i])){
					printf("%s is correct\n", temp);
					break;
				}
		}
		if (!matchedflag){
			printf("%s:", temp);
			for (int j = 0; j < matchedcnt; ++j){
				printf(" %s", semimatched[j]);
			}
			putchar('\n');
		}
	}
	return 0;
}


void LCS(char *a, char *b, int al, int bl){
	memset(C, 0, sizeof(C));
	for (int i = 1; i <= al; i++){
		for (int j = 1; j <= bl; j++){
			if (a[i - 1] == b[j - 1]){
				C[i][j] = C[i - 1][j - 1] + 1;
			}
			else if (C[i - 1][j] >= C[i][j - 1]){
				C[i][j] = C[i - 1][j];
			}
			else{
				C[i][j] = C[i][j - 1];
			}
		}
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator