首页 > 代码库 > Coder-Strike 2014 - Round 1__E-mail Addresses

Coder-Strike 2014 - Round 1__E-mail Addresses

题目链接

  • 题意:
    给定一个字符串,判断合法串的个数。对于形如str1@str2.str3的串,满足:str1包括数字、字母、下划线且由字母开头;str2由字母、数字组成;str3由字母组成;三个串均非空,且是连续的串
  • 分析:
    题目没什么难度,就是处理起来比较麻烦。可以记录一下所有@和.出现的位置,然后判断符合条件的三个串有几个,乘积即可

const int MAXN = 1100000;

char ipt[MAXN];
LL alp[MAXN], number[MAXN], under[MAXN], succ[MAXN];
//从头开始字母的个数;从头开始的数字个数;从头开始的下划线个数;当前符号之后有几个连续的字母

int main()
{
//    freopen("in.txt", "r", stdin);
    while (~RS(ipt + 1))
    {
        vector<int> vt;
        CLR(under, 0); CLR(alp, 0); CLR(number, 0); CLR(succ, 0);
        ipt[0] = ‘.‘;
        int len = strlen(ipt);
        ipt[len++] = ‘@‘; ipt[len] = 0;
        REP(i, len)
        {
            if (i)
            {
                alp[i] = alp[i - 1];
                number[i] = number[i - 1];
                under[i] = under[i - 1];
            }
            if (isalpha(ipt[i])) alp[i]++;
            else if (isdigit(ipt[i])) number[i]++;
            else if (ipt[i] == ‘_‘) under[i]++;
            else
            {
                vt.push_back(i);
            }
        }
        int tt = 0;
        LL ans = 0;
        FED(i, len - 1, 0)
        {
            succ[i] = tt;
            if (isalpha(ipt[i])) tt++;
            else tt = 0;
        }
        FF(i, 1, vt.size() - 1)
        {
            int cur = vt[i], nxt = vt[i + 1];
            if (ipt[cur] == ‘@‘ && ipt[nxt] == ‘.‘ && under[nxt] == under[cur] && nxt - cur > 1)
            {
                ans += succ[nxt] * (alp[cur] - alp[vt[i - 1]]);
            }
        }
        cout << ans << endl;
    }
    return 0;
}


Coder-Strike 2014 - Round 1__E-mail Addresses