#include <stdio.h>#include<string.h>#include<string.h>Charstr1[ -],str2[ -];intLen;intCalChar*STR1,Char*str2) { intret=0, I; for(i=0; str1[i]&&str2[i];i++) { if(str1[i]==Str2[i]) ret++; } returnret;}intMaxintAintb) { intZ; Z= (a>b)?a:b; returnZ;}intgcdintAintb) { if(a==0) return 1; if(a%b==0) returnb; Else { returnGCD (b, (a%b)); }}/*int gcd (int a, int b) {if (a==0) return 1; while (1) {int r=a%b; if (r==0) return B; else {a=b; B=r; } }}*/intFindlargest (Char*STR1,Char*str2) { inti,j; intLen1=strlen (STR1);//len1 Len2 is placed in the calling function, saving time, not re intLen2=strlen (STR2); Len=len1+Len2; intret=cal (STR1,STR2); for(i=1; i<len1;i++) {ret=max (Ret,cal (str1+i,str2)); } for(j=1; j<len2;j++) {ret=max (Ret,cal (str1,str2+j)); } returnret;}intMain () {intans; while(SCANF ("%s", str1), strcmp (STR1,"-1"))//input-1, which is two characters in the array { intG; scanf ("%s", STR2); Ans=findlargest (STR1,STR2); Ans*=2; G=gcd (Ans,len); Ans/=G; Len/=G; if(ans==0) printf ("appx (%s,%s) =%d\n", STR1,STR2,0); Else if(len==1) printf ("appx (%s,%s) =%d\n", Str1,str2,ans); Elseprintf ("appx (%s,%s) =%d/%d\n", Str1,str2,ans,len); } return 0;}
Euclid's Algorithm, written by itself:
intgcdintAintb) { if(a==0) return 1; while(1) { intr=a%b; if(r==0) returnb; Else{a=b; b=R; } }}
The Recursive method writes:
int gcd (intint b) { if(a==0) return 1; if (a%b==0) return b; Else { return gcd (b, (a%b));} }
Euclidean algorithm is called greatest common divisor, used to seek the greatest common divisor greatest common divisor, GCD representative function name
Algorithm Description: 1. Set A%b=r, if r==0,b is greatest common divisor, return b,2 otherwise a<--b,b<--r, repeat the first step
It is possible that the gcd (int a, int b), the numerator is 0,return 1,o and any positive greatest common divisor is 1, the fraction numerator, if the molecule is 0, the direct output 0
If the denominator is 1, indicating that the molecular score is large, the output molecule on the line, the problem may only be used for the fraction value of 1 cases
Some Euclidean algorithm description requires if the A<B exchange value, in fact, in the above two algorithms, a=3,b=13
A%b=3! = 0, according to Euclidean algorithm description, a<--b,b<--r,a=13,b=3. The second of the loop, or the second of the recursion, does not actually change the value.
will also become 13%3, so Euclid's algorithm is suitable for large or high denominator situations.
The algorithm ends with a condition of a%b==0, which returns the value of B
Re several times, before the len1,len2 into a global variable, RE, then len1,len2, placed in the Findlargest function as a temporary variable, left Len a global
Should be to save a lot of space, visible: the use of global variables to be cautious
In addition to this algorithm, the idea:
The Cal (Char *str1, char *str2) starts the string from the incoming address in the left-aligned findlargest inside the first for loop:
Second
poj1580---Euclidean algorithm (Euclidean method)