0

There is a problem in SGU (Problem 274).

Task is writing a simple email-validator.

Valid email address can be described as follows:

[letter] ::= a|b|...|z|A|B|...|Z

[symbol] ::= [letter] |0|1|...|9|_|-

[word] ::= [symbol] | [symbol][word]

[prefix] ::= [word] | [prefix].[word]

[domain] ::= [letter][letter] | [letter][letter][letter]

[suffix] ::= [prefix].[domain]

[address] ::= [prefix]@[suffix]

I'm sure that my validator works correctly but i got wrong answer.

Here is my validator:

Description: First it checks the email address length and position of @. Then it breaks it into maximal parts, Prefix and Suffix and check whether they're valid or not.

bool check(string command, int k, int f, int l)
{
    /*

        command: it is "prefix" or "suffix"
        k: index of the email (There are several email addresses)
        f: first of the string
        l: end of the string 

    */ 

    if(f > l) return false;

    if(command == "prefix")
    {        
        for(int i = f ; i <= l ; i++)
        {
           if(s[k][i]-'A' < 26 && s[k][i]-'A' >= 0) continue;
           if(s[k][i]-'a' < 26 && s[k][i]-'a' >= 0) continue;
           if(s[k][i]-'0' < 10 && s[k][i]-'0' >= 0) continue;   
           if(s[k][i] == '-' || s[k][i] == '_') continue;
           if(s[k][i] == '.' && i < l && i > f) continue;

           return false; 
        }

        return true; 
    } 

    if(command == "suffix")
    {
        if(s[k][l-2] != '.' && s[k][l-3] != '.') return false;

        int dot = (s[k][l-2] == '.')? l-2 : l-3;

        for(int i = dot+1 ; i <= l ; i++)
        {
           if(s[k][i]-'A' < 26 && s[k][i]-'A' >= 0) continue;
           if(s[k][i]-'a' < 26 && s[k][i]-'a' >= 0) continue;
           return false;
        }

        if(check("prefix", k, f, dot-1)) return true;
        return false;
    }
}

void solve()
{
    int at;

    for(int i = 1 ; i <= n ; i++)
    {     
        if(s[i].length() < 6)
            continue;

        at = 0;

        for(int j = 1 ; j < s[i].length()-4 ; j++)
            if(s[i][j] == '@') 
            {
                at = j;
                break;       
            }

        if(!at) continue;    

        if(check("prefix", i, 0, at-1) && check("suffix", i, at+1, s[i].length()-1))
            ans[i] = true;
    }   
}

Sample test:



Input

3

abc@abc

abc@abc.abc

_@-.ru


Output

NO

YES

YES


msam
  • 1
  • 3
  • Could you be a bit more precise on how your answer is wrong? – kviiri Jul 25 '14 at 10:56
  • First index of the string is **0** and last index is **n-1**, where **n** is the length of the string. – Mahesh Jul 25 '14 at 10:58
  • 1
    Your valid-address grammar is highly simplified. You might want to check e.g. [this old SO question and its answers](http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address). – Some programmer dude Jul 25 '14 at 11:00
  • Unfortunately there is no information about the test cases. Yes, the first index is 0 and the last is n-1 – msam Jul 25 '14 at 11:07

1 Answers1

0

You should explain how wrong your output but i run your code it is working for the rules and maybe you forgot to set ans[i] all false at start right? Because their garbage value may lead these problems.

As i said it is run fine for your CFG at first.If you mean it verify valid email something like a "something@ss.ss.ss.ss.ss.ss.ss" with "wrong answer" it is your CFGs problem.Last thing there is regex class at c++, you can try it too.

I hope it can help you.

oknsnl
  • 351
  • 1
  • 11