poj 1580

#include <stdio.h>
#include <string.h>
#define M 100010
char sa[M], sb[M];
int la, lb;
int solve() {
	la = strlen(sa);
	lb = strlen(sb);
	int maxlen = 0;
	for (int i = 0; i < la; ++ i) {
		for (int j = 0; j < lb; ++ j) {
			int len = 0;
			for (int i1 = i, j1 = j; i1 < la && j1 < lb; ++ i1, ++ j1) {
				if (sa[i1] == sb[j1]) {
					++ len;
				}
			}
			if (maxlen < len) {
				maxlen = len;
			}
		}
	}
	return maxlen<<1;
}
int gcd(int a, int b) {
	return b?gcd(b,a%b):a;
}
void print() {
	int res = solve();
	int len = la+lb;
	printf("appx(%s,%s) = ",sa,sb);
	if (!res) {
		puts("0");
	}
	else if (res == len) {
		puts("1");
	}
	else {
		int g = gcd(res, len);
		printf("%d/%d\n", res/g, len/g);
	}

}
int main() {
	while (scanf("%s", sa) && strcmp(sa,"-1") && scanf("%s", sb)) {
		print();
	}
	return 0;
}

  

posted @ 2011-09-01 09:45  张兰云  阅读(246)  评论(0编辑  收藏  举报