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 ;
    }
    

      

  • 相关阅读:
    linux内核分析第一周学习笔记
    信息安全系统设计基础期末学习总结
    信息安全系统设计基础实验四实验报告
    《Linux内核分析》第七周 可执行程序的装载
    《在kali上完成gdb调试》
    《Linux内核分析》 第六周
    《Linux 内核分析》第五周
    《Linux内核分析》 第四周
    《Linux内核分析》第三周
    Linux内核分析第二周
  • 原文地址:https://www.cnblogs.com/chen9510/p/5004392.html
Copyright © 2011-2022 走看看