Description
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya has two strings a and b of the same length n. The strings consist only of lucky digits. Petya can perform operations of two types:
- replace any one digit from string a by its opposite (i.e., replace 4 by 7 and 7 by 4);
- swap any pair of digits in string a.
Petya is interested in the minimum number of operations that are needed to make string a equal to string b. Help him with the task.
Input
The first and the second line contains strings a and b, correspondingly. Strings a and b have equal lengths and contain only lucky digits. The strings are not empty, their length does not exceed 105.
Output
Print on the single line the single number — the minimum number of operations needed to convert string a into string b.
Sample Input
47
74
1
774
744
1
777
444
3
题目意思:给你两个字符串a,b,a,b都是由4和7组成的。a串中的4和7可以通过交换位置或者替换(4替换成7,7替换成4),
问最少多少步就能得到b串。
解题思路:这算是一道找规律的题了,其实可以这样做,统计a串和b串不同的字符在b串中表现是4还是7,记录一下个数,
4的个数和7的个数可以相互抵消来表示交换,剩下不能抵消的可以用来替换。抵消的次数加上替换的次数就是需要的步数。
上代码:
#include<stdio.h> #include<string.h> int main() { char a[100001],b[100001]; int k,i,count_7=0,count_4=0,count=0; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); gets(a); gets(b); k=strlen(a); for(i=0; i<k; i++) { if(a[i]==b[i]) continue; else { if(b[i]=='7') count_7++; else count_4++; } } if(count_7>=count_4) count=count_4+count_7-count_4; else if(count_7<count_4) count=count_7+count_4-count_7; printf("%d ",count); return 0; }