CHEFSTLT - Editorial

PROBLEM LINK:

Practice
Contest

Author: Roman Furko
Testers: Pushkar Mishra and Sergey Kulik
Editorialist: Pawel Kacprzak
Russian Translator: Sergey Kulik
Mandarian Translator: Gedi Zheng

DIFFICULTY:

Cakewalk

PREREQUISITES:

Implementation

PROBLEM:

You are given two strings S1, S2 with equal lengths. Both strings, on any position, can contain a lowercase latin letter or a question mark β€˜?’.

A question mark can be equal to any of lowercase latin letters. We define the difference between the strings as the number of positions i, such that the strings are different on this position. Your task is to compute the minimal and the maximal difference between the strings.

QUICK EXPLANATION:

Let’s consider any position i in the strings. Since a question mark can be any letter, if at least one of the strings have a question mark on the i-th position, we can always change it to match the letter in the other string or we can always change it to a letter which produces a mismatch. Therefore, the minimal difference is the number of positions for which none of the strings contain a question mark and there is a mismatch between them on this position. On the other hand, the maximal difference is the length of the strings reduced by the number of positions for which none of the strings contain a question mark and there is a match between them on this position.

EXPLANATION:

We define a strong match as the position i, such that none of the strings contain a question mark at the i-th position, and the letters in the strings at the i-th position match.

We define a strong mismatch as the position i, such that none of the strings contain a question mark at the i-th position, and the letters in the strings at the i-th position do not match.

Let’s consider any position i in both strings. If at least one string contain a question mark on the i-th position, we can create a match or a mismatch between the strings at this position.

There are basically two cases to consider:

  1. If only one string contains a
    question mark on the i-th position.

    Without loss of the generality,
    let’s assume that S1[i] = β€˜?’ and
    S2[i] = β€˜a’. Since the latin
    alphabet has 26 letters, we can
    produce a mismatch by setting up
    S1[i] to any letter different by β€˜a’
    and we can produce a match by
    setting up S1[i] to β€˜a’.

  2. Both strings contain a question mark
    on the i-th position

    Since the latin alphabet has 26
    letters, we can produce a match by
    setting up S1[i] = S2[i] = β€˜a’ and
    we can produce a mismatch by setting
    up S1[i] = β€˜a’ and S2[i] = β€˜b’.

Based on these two facts, we for any position which is not a strong match or a strong mismatch, we can make it a match or a mismatch.

Therefore, the minimal difference is the number of strong mismatches. On the other hand, the maximal difference is the length of the strings reduced by the number of strong matches.

Time complexity:

Linear in terms of the length of input strings.

AUTHOR’S AND TESTER’S SOLUTIONS:

Tester

2 Likes

#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
int t,i,x,y;
char s1[102],s2[102];
scanf("%d",&t);
while(t–)
{
i=0;
x=0;
y=0;
scanf("%s",&s1);
scanf("%s",&s2);
while(s1[i])
{
if(s1[i]==’?’ || s2[i]==’?’)
x++;
else if(s1[i] != s2[i])
y++;
i++;
}
printf("%d %d\n",y,x+y);
}
return 0;
}

#include<stdio.h>
int main()
{
int t,minimal,maximal,count,i;
char s1[105],s2[105];
scanf("%d",&t);
while(t–)
{
scanf("%*c%s%s",s1,s2);
i=0;
count=minimal=maximal=0;
while(s1[i]!=’\0’)
{
if(s1[i]==’?’||s2[i]==’?’)
count++;
else
{
if(s1[i]!=s2[i])
minimal++;
}
i++;
}
maximal=minimal+count;
printf("%d %d\n",minimal,maximal);
}
return 0;
}

#include<stdio.h>
#include<string.h>
int main(){
char S1[100]={1};
char S2[100];

int n = 0;
int min = 0,max = 0;
scanf("%d",&n);
for(int k = 1; k <= n ; k++){
	scanf("%s",S1);
	scanf("%s",S2);
	char arr3[100];
    char arr4[100];
	strcpy(arr3,S1);
	strcpy(arr4,S2);
	for(int i = 0;i < strlen(S1);i++){
	if (S1[i] == '?'){
		S1[i] = 'a';
	}
	if(S2[i] == '?'){
		S2[i] = 'a';
	}
	if (S1[i] != S2[i]){
		min++;
	}
}
for(int j = 0 ; j < strlen(arr3);j++){
	if (arr3[j] == '?'){
		arr3[j] = 'x';
	}
}
for(int j = 0; j < strlen(arr4);j++){
	if (arr4[j] == '?'){
		arr4[j] = 'y';
	}
	if (arr3[j] != arr4[j]){
		max++;
	}
}
printf("%d %d\n",min,max);
min = 0;
max = 0;
}

return 0;

}

why the subtask 1 and 3 always wrong, someone help me!!

Simple Python Code following the idea described in Explaination

t = int(input())
while(t>0):
    s1 = input()
    s2 = input()
    min_count = 0
    max_count = 0
    for i,j in zip(s1, s2):
        if(i == '?' and j == '?'):
            max_count += 1
        elif((i == '?' and j != '?') or (i != '?' and j == '?')):
            max_count += 1
        else:
            if(i != j):
                min_count += 1
                max_count += 1

    print(min_count, max_count)
    
    t -= 1

Can anyone have a look? This logic seems sound, but something is wrong cant really put a finger on it.

import random
import string

def diff(s1,s2):
    count = 0
    for i in range(len(s1)):
        if s1[i] != s2[i]:
            count = count + 1
    return count


for i in range(int(input())):
    
    s1 = input()
    s2 = input()
    mini  = 0
    
    for j in range(len(s1)):
        if s1[j] == '?' or s2[j] == '?':
            if s1[j] != s2[j]:
                mini = mini+1

    d3= ''
    d4= ''

    for i in range(len(s1)):
        if s1[i] == '?':
            d3 = d3 + random.choice(string.ascii_letters)
        else:
            d3 = d3 + s1[i]

        if s2[i] =='?':
            d4 = d4 + str(random.randint(1,10))
        else:
            d4 = d4 + s2[i]
        
    diff2 = diff(d3,d4)

    print(mini,diff2)

can anyone tell me why my solution isnt working?
#include
using namespace std;

int main() {
int t;
cin>>t;
while(t>0)
{
string s1;
string s2;
cin.ignore(256, β€˜\n’);
getline(cin,s1);
getline(cin,s2);
int m=s1.length();
int maxdiff=0;
int mindiff=0;
int i=0;
while(i<m)
{
if((s2[i]==’?’) && (s1[i]==’?’))
{
maxdiff++;
}
else if((s1[i]!=’?’ && s2[i]==’?’)|| (s1[i]==’?’ && s2[i]!=’?’))
{
maxdiff++;
}
else if(s1[i]!=s2[i])
{
mindiff++;
maxdiff++;
}
i++;
}
cout<<mindiff<<" "<<maxdiff<<endl;
t–;

}
return 0;

}

in your condition you are not moving i.
write in every condition i++.

my approach

#include <stdio.h>

int main(void) {
// your code goes here
int n;scanf("%d",&n);

char str1[101];
char str2[101];

for(int i=0;i<n;i++){
    scanf("%s",str1);
    scanf(" %s",str2);
    int j=0;
    int count=0;
    int unknown=0;
    while(str1[j]!=0){
        if((str1[j]!='?'&&str2[j]!='?')&&(str1[j]!=str2[j])){
            count++;
        }
        if(str1[j]=='?'||str2[j]=='?'){
            unknown++;
        }
        j++;
    }
    printf("%d %d\n",count,unknown+count);
    
       
    
   
}



return 0;

}