字符串中的有效地址

题目网址: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\n",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\n",ans) ;
    return 0 ;
}

  

转载于:https://www.cnblogs.com/chen9510/p/5004392.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值