标签:hdu1306
The input for your program will be a series of words, two per line, until the end-of-file flag of -1. Using the above technique, you are to calculate appx() for the pair of words on the line and print the result. For example: CAR CART TURKEY CHICKEN MONEY POVERTY ROUGH PESKY A A -1 The words will all be uppercase.
Print the value for appx() for each pair as a reduced fraction, like this: appx(CAR,CART) = 6/7 appx(TURKEY,CHICKEN) = 4/13 appx(MONEY,POVERTY) = 1/3 appx(ROUGH,PESKY) = 0 appx(A,A) = 1
#include <stdio.h> #include <string.h> #define maxn 1002 char s1[maxn], s2[maxn]; int len1, len2, ans, len; void appx(){ int num; int begin1 = 0, begin2 = len2 - 1; while(begin2 >= 0){ num = 0; int i = begin1, j = begin2; while(i < len1 && j < len2){ if(s1[i++] == s2[j++]) ++num; } if(num > ans) ans = num; --begin2; } begin2 = begin1 = 0; while(begin1 < len1){ num = 0; int i = begin1, j = begin2; while(i < len1 && j < len2){ if(s1[i++] == s2[j++]) ++num; } if(num > ans) ans = num; ++begin1; } } int gcd(int i, int j){ return !j ? i : gcd(j, i % j); } void huajian(){ int t = gcd(ans, len); ans /= t; len /= t; } int main(){ while(scanf("%s", s1), s1[0] != '-'){ scanf("%s", s2); len1 = strlen(s1); len2 = strlen(s2); ans = 0; appx(); len = len1 + len2; ans *= 2; printf("appx(%s,%s) = ", s1, s2); if(ans == 0 || ans == len){ printf("%d\n", ans / len); continue; } huajian(); printf("%d/%d\n", ans, len); } return 0; }
HDU1306 String Matching 【暴力】,布布扣,bubuko.com
标签:hdu1306
原文地址:http://blog.csdn.net/u012846486/article/details/27977819