思路:理解题意:两个字符串可以配对(既移动相对位置),找出最多的相同字符。
代码如下:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <vector>
using namespace std;
int g(int m, int n) {
while (n != 0) {
int r = m % n;
m = n;
n = r;
}
return m;
}
int f(int x, int y, char a[100], char b[100])
{
char temp1[100], temp2[100];
int c[100];
int len1, len2;
int i,j;
int t1, t2;
int r;
len1 = strlen(a);
len2 = strlen(b);
t1 = t2 = 0;
r = 0;
for (i = x,j=y; i < len1,j<len2; i++,j++)
{
if (a[i] == b[j])
r++;
}
return r;
}
int main()
{
char a[100], b[100];
char temp1[100],temp2[100];
int c[100];
int len1, len2;
int len;
int i,j;
int r;
int max;
int r1, r2;
while (cin >> a && strcmp(a, "-1"))
{
cin >> b;
len1 = strlen(a);
len2 = strlen(b);
max = 0;
for (i = 0; i < len1; i++)
{
for (j = 0; j < len2; j++)
{
if (a[i] == b[j])
{
r=f(i, j, a, b);
if (r > max)
{
max = r;
}
}
}
}
max *= 2;
r1 = max / g(max, len1 + len2);
r2 = (len1 + len2) / g(max, len1 + len2);
if (r1 == 0)
printf("appx(%s,%s) = 0\n", a, b);
else if (r1 == r2 && r1)
printf("appx(%s,%s) = 1\n",a,b);
else if(r1!=r2)
printf("appx(%s,%s) = %d/%d\n", a, b,r1,r2);
}
system("pause");
return 0;
}