2017-07-13 3 views
0

Pour énoncé du problème, s'il vous plaît voir this.problème concernant la mauvaise sortie avec une déclaration HackerRank sur anagrammes

(j'omis les bibliothèques.)

Mon code fonctionne bien (lorsque les tests juge en ligne le code), sauf pour ce cas:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

J'aimerais savoir pourquoi la sortie il attend d'être 104 WHE n intersection est zéro?

std::string string_intersection; 

int number_needed(string a, string b) { 

int y = a.length(); 
int z = b.length(); 

sort(a.begin(), a.end()); 
sort(b.begin(), b.end()); 

std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(string_intersection)); 

int num, x = string_intersection.length(); 

if(x==0) 
    num = 0; 

else 

num = y + z - 2*x; 

return num; 


} 

    int main(){ 
    string a; 
    cin >> a; 
    string b; 
    cin >> b; 
    cout << number_needed(a, b) << endl; 
    return 0; 
} 

Répondre

0

il suffit de changer

if(x==0) 
    num = y+z; //as everything needs to be deleted 

else 

    num = y + z - 2*x; 

Check for your self in hacker rank

+0

La partie vitale que je manquais. Merci! – stoicGoyal