zoukankan      html  css  js  c++  java
  • 字符串中的有效地址

    题目网址:http://codeforces.com/problemset/problem/412/E
    E. E-mail Addresses
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    One of the most important products of the R1 company is a popular @r1.com mail service. The R1 mailboxes receive and send millions of emails every day.

    Today, the online news thundered with terrible information. The R1 database crashed and almost no data could be saved except for one big string. The developers assume that the string contains the letters of some users of the R1 mail. Recovering letters is a tedious mostly manual work. So before you start this process, it was decided to estimate the difficulty of recovering. Namely, we need to calculate the number of different substrings of the saved string that form correct e-mail addresses.

    We assume that valid addresses are only the e-mail addresses which meet the following criteria:

    • the address should begin with a non-empty sequence of letters, numbers, characters '_', starting with a letter;
    • then must go character '@';
    • then must go a non-empty sequence of letters or numbers;
    • then must go character '.';
    • the address must end with a non-empty sequence of letters.

    You got lucky again and the job was entrusted to you! Please note that the substring is several consecutive characters in a string. Two substrings, one consisting of the characters of the string with numbers l1, l1 + 1, l1 + 2, ..., r1 and the other one consisting of the characters of the string with numbers l2, l2 + 1, l2 + 2, ..., r2, are considered distinct if l1 ≠ l2 or r1 ≠ r2.

    Input

    The first and the only line contains the sequence of characters s1s2... sn (1 ≤ n ≤ 106) — the saved string. It is guaranteed that the given string contains only small English letters, digits and characters '.', '_', '@'.

    Output

    Print in a single line the number of substrings that are valid e-mail addresses.

    Sample test(s)
    input
    gerald.agapov1991@gmail.com
    output
    18
    input
    x@x.x@x.x_e_@r1.com
    output
    8
    input
    a___@1.r
    output
    1
    input
    .asd123__..@
    output
    0
    Note

    In the first test case all the substrings that are correct e-mail addresses begin from one of the letters of the word agapov and end in one of the letters of the word com.

    In the second test case note that the e-mail x@x.x is considered twice in the answer. Note that in this example the e-mail entries overlap inside the string.

    方法一:

    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    char a[1005005];
    
    int main()
    {
        int flag1,flag2,flag3;
        long long sum,len,x,y,k;
        scanf("%s",a);
        sum=0;
        len=strlen(a);
        x=0;y=0;flag1=0;flag2=0;flag3=0;k=0;
        for(int i=0;i<len;i++)
        {
            if(flag1==0&&a[i]>='a'&&a[i]<='z')
            {
                x++;
                goto endw;
            }
            if(flag1==0&&a[i]=='.')
            {
                x=0;
                goto endw;
            }
            if(a[i]=='@'&&flag1==0)
            {
                flag1=1;
                goto endw;
            }
            if(flag1==1&&flag2==0&&((a[i]>='a'&&a[i]<='z')||(a[i]>='0'&&a[i]<='9')))
            {
                flag3=1;
                if(a[i]>='a'&&a[i]<='z')
                 k++;
                goto endw;
            }
            if(flag1==1&&flag2==0&&a[i]=='@')
            {
                flag1=1;flag3=0;
                x=k;k=0;
                goto endw;
            }
            if(flag1==1&&flag2==0&&a[i]=='_')
            {
                flag1=0;flag3=0;
                x=k;k=0;
                goto endw;
            }
            if(flag1==1&&flag3==0&&a[i]=='.')
            {
                x=0;flag1=0;
                goto endw;
            }
            if(flag3==1&&flag2==0&&a[i]=='.')
            {
                flag2=1;
                goto endw;
            }
            if(flag2==1&&a[i]>='a'&&a[i]<='z')
            {
                y++;
                goto endw;
            }
            if(flag2==1&&(a[i]<'a'||a[i]>'z'))
            {
                sum+=x*y;
                x=y;y=0;k=0;
                flag1=0;flag2=0;flag3=0;
                if(a[i]=='@')
                flag1=1;
                if(a[i]=='.')
                x=0;
            }
            endw :;
        }
        sum+=x*y;
        printf("%I64d
    ",sum);
        return 0;
    }

    方法二:

    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    
    using namespace std ;
    
    char ch[1052001] ;
    
    int main()
    {
        scanf("%s",ch) ;
        int len = strlen(ch) ;
        long long cnt = 0 ,a = 0,ans = 0 ;
        for(int i = 0 ; i < len ; i++)
        {
            if((ch[i] >= 'a' && ch[i] <= 'z')) cnt ++ ;
            else if(ch[i] == '.') cnt = 0 ;
            else if(ch[i] == '@')
            {
                int j = ++ i ;
                while(i < len && ((ch[i] >= 'a' && ch[i] <= 'z') || (ch[i] >= '0' && ch[i] <= '9')))
                    ++ i ;
                if(i != j && ch[i] == '.')
                {
                    j = ++ i ;
                    a = 0 ;
                    while(i < len && (ch[i] >= 'a' && ch[i] <= 'z'))
                    {
                        i++ ;
                        a++ ;
                    }
                    ans += a*cnt ;
               }
                i = j-1 ;
                cnt = 0 ;
            }
        }
        printf("%I64d
    ",ans) ;
        return 0 ;
    }
    

      

  • 相关阅读:
    WCF系列之双工通信 牧羊人
    WCF系列之承载(IISHTTP) 牧羊人
    在Ubuntu 10.10下安装JDK配置Eclipse及Tomcat【转载 + 订正】
    gconfeditor简介【转载】
    Ubuntu10.10(linux) 安装jdk1.6及环境变量的设置【转载】
    ubuntu下txt文件中文显示乱码的方法【转载】
    程序员的编辑器——VIM【转载】
    xx is not in the sudoers file 问题解决【转载】
    win7下配置PHP+apache+mysql【转载 + 补充】
    ubuntu忘记密码,忘记root密码的解决方法!【转载】
  • 原文地址:https://www.cnblogs.com/chen9510/p/5004392.html
Copyright © 2011-2022 走看看