My all test cases passed except when input is only dots"...", I want it to print "Valid".
It worked with code below:
int main()
{
int L;
cin >> L;
vector<char> vec(L);
//input
for (int i = 0; i < L; i++)
cin >> vec[i];
//removing dots
vector<char> pure;
for (int i = 0; i < L; i++)
{
if (vec[i] == 'H' || vec[i] == 'T')
pure.push_back(vec[i]);
}
int pureSize = pure.size() - 1;
// HERE
if (pure.empty())
cout << "Valid" << "\n";
else if (pure[0] == 'H' && pure[pureSize] == 'T')
{
int flag = 1;
for (int i = 0; i < pureSize - 1; i++)
{
if (pure[i] == pure[i + 1])
{
flag = 0;
break;
}
}
if (flag)
cout << "Valid"
<< "\n";
else
cout << "Invalid"
<< "\n";
}
else
cout << "Invalid"
<< "\n";
return 0;
}
BUT why this doesn't work/ breaks (below) when I interchange the positions of first two conditions? It should never enter in first 'if' if I enter only dots'...'?
int main()
{
int L;
cin >> L;
vector<char> vec(L);
//input
for (int i = 0; i < L; i++)
cin >> vec[i];
//removing dots
vector<char> pure;
for (int i = 0; i < L; i++)
{
if (vec[i] == 'H' || vec[i] == 'T')
pure.push_back(vec[i]);
}
//logic
int pureSize = pure.size() - 1;
// HERE
if (pure[0] == 'H' && pure[pureSize] == 'T')
{
int flag = 1;
for (int i = 0; i < pureSize - 1; i++)
{
if (pure[i] == pure[i + 1])
{
flag = 0;
break;
}
}
if (flag)
cout << "Valid"
<< "\n";
else
cout << "Invalid"
<< "\n";
}
else if (pure.empty())
cout << "Valid" << "\n";
else
cout << "Invalid"
<< "\n";
return 0;
}
Why the first code works but second doesn't? I mean in the second code it should jump to else if since first condition is never met, right? Since "..." is the input, there is no H or T. Please explain.
Aucun commentaire:
Enregistrer un commentaire