Lucky Conversion
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 524288/262144K (Java/Other)
Total Submission(s) : 42 Accepted Submission(s) : 11
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits4 and 7. For example, numbers47, 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 performoperations of two types:
- replace any one digit from string a by its opposite (i.e., replace4 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 stringa equal to string b. Help him with the task.
The first and the second line contains strings a andb, correspondingly. Strings a and b have equal lengths and contain only lucky digits. The strings are not empty, their length does not exceed105.
Print on the single line the single number the minimum number of operations needed to convert stringa into string b.
47 74 774 744
1 1
题意:用交换任意两个字符或替换与之对应的字符使两个字符串相等;
本题只需找出对应不相等的字符4和7的个数的最大值即为所求;
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char s1[100005],s2[100005];
while(cin>>s1>>s2)
{
int l=strlen(s1);
int k=0,sum=0,i;
for(i=0;i<l;i++)
{
if(s1[i]!=s2[i]&&s1[i]=='4')
k++;
if(s1[i]!=s2[i]&&s1[i]=='7')
sum++;
}
int ans=max(k,sum);
printf("%d\n",ans);
}
}