WA in NAMEVAL

why is this wrong?

for _ in range(int(input())):
	s=input()
	v=['a','e','i','o','u']
	l=""
	for i in range(len(s)):
		if(s[i] in v):
			l+='0'
		else:
			l+='1'
	#print(l)
	n=int(l,2)
	k=n%1000000007
	print(k)

Same bro!
t=int(input())
for _ in range(t):
s=str(input())
a=’’
for i in range(len(s)):
if s[i]==‘a’ or s[i]==‘e’ or s[i]==‘i’ or s[i]==‘o’ or s[i]==‘u’:
a+=‘0’
else:
a+=‘1’
print(int(int(a,2)%(1e9+7)))

why is this giving WA???

i saw submissions for this question and people used isalpha for checking and there answer accepted , but i am not understanding why it is required as in input it was mentioned that string will be lowercase english alphabets

No i have seen correct submission without ischar

try using input().strip(). I think there were some extra spaces in test files.

2 Likes

On clicking run code it will show NZEC you have to give custom input

What is even going on with this problem? I see so many accepted submissions that clearly overflow.

(replied to wrong person, but still)

mod = 10**9 + 7
def main():

    myset = {'a', 'e', 'i', 'o', 'u'}
    T = int(input())
    while T:
        string = input()
        ans = 0
        for i in string:
            ans *= 2
            if i in myset:    # its a vowel
                ans += 0
            else:
                ans += 1
            ans %= mod
        print(ans)
        T-=1

main()

I hope this will help you, just iterate over the string , multiply it every time and add 0 or 1 according to vowel and consonant :slight_smile:

One suggestion for you bro, never use

s2 = s2 + "0"

adding in such a way will cost you O(n) time complexity in strings (not only in python but also in c++, you can search about it, its quite famous topic in strings), instead of which you can use a list where append function will do the same in avg. O(1) time. after that, you can use the join function to convert it into the string.

This question brought down my rank. I thought that the MSB would be (0 or 1) \times 10^{50} instead of 2^{50} (N_2 \rarr N_{10}). That’s why I decided to use python (C++ is terrible with large numbers IMO).
My python solution:

for _ in range(int(input())):
    s = input()
    binary = []
    for i in s:
        if (i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u'):
            binary.append(0)
        else:
            binary.append(1)
    n = 0
    for i in range(1, len(binary)+1):
        n += (binary[-i]*pow(2, i-1, 1000000007))
    print(n % 1000000007)

I don’t see any error here. But the verdict was WA.
Then I realized that the MSB can be atmost 2^{50} in base-10 which is \approx 10^{15} which will easily fit in the long long data type. So I changed the language to C++.
My C++ code:

#include <bits/stdc++.h>

using namespace std;

int power(int x, int y, int p)  
{  
    int res = 1;
    x = x % p;
    if (x == 0) return 0;
    while (y > 0)  
    {    
        if (y & 1)  
            res = (res*x) % p;    
        y = y>>1; 
        x = (x*x) % p;  
    }  
    return res;  
}  

const int mod = 1e9+7;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        string s;
        cin >> s;
        vector <int> binary(0);
        for (int i = 0; i < s.size(); ++i)
        {
            if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u') binary.push_back(0);
            else binary.push_back(1);
        }
        long long ans = 0;
        for (int i = s.size()-1; i >= 0; --i) ans += (binary[i]*power(2, s.size()-1-i, mod));
        cout << (ans % mod) << '\n';
    }    
}

The only difference is that I used modular exponentiation here. But guess what, this time it was AC. I was happy, but I still don’t get what’s wrong here. Can someone please explain why this happened?

Isn’t the length of the string up to 10^5, not 50?

1 Like

Oh yes. I was in a hurry and misread the constraints :man_facepalming:. But I still don’t understand what’s wrong with my python submission.

Maybe what @aert said was true, and there are extra spaces that are screwing up your input. In general, this contest seems to be horribly prepared, so I wouldn’t doubt that it could be a possible issue.

1 Like

I see. Thanks for your help :slight_smile:.

true

I don’t think there were extra spaces in the input because my simple solution (I mentioned above)
worked without using strip function

my code pretty much same as yours…except you are calculating the value within for loop while i am calculation it after getting the string…but getting wrong answer

thanks, it worked for me