题目链接:Codeforces 412E E-mail Addresses
题目大意:问说给出字符串中有几个正确的邮箱,
1.‘@’前非空的字符串,由字符,数字和下划线组成,只能由字符开头
2.’@‘和’.‘中间的字符串非空,只能由字符,数字和下划线组成
3.‘.’后非空字符串,只能有字符组成
解题思路:计算每个@前面字符的个数,以及.后面字符的个数,相乘即为当前这对@和.所满足的个数,不过要注意@和.之间的字符串非空。
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; typedef long long ll; const int N = 1e6+5; char s[N]; bool isLetter(char c) { if (c >= ‘a‘ && c <= ‘z‘) return true; if (c >= ‘A‘ && c <= ‘Z‘) return true; return false; } int main () { scanf("%s", s); int n = strlen(s); ll ans = 0; for (int i = 0; i < n; i++) { if (s[i] == ‘@‘) { ll x = 0, y = 0; for (int j = i-1; j >= 0; j--) { if (s[j] == ‘@‘ || s[j] == ‘.‘) break; if (isLetter(s[j])) x++;; } for (int j = i+1; j < n; j++) { if (s[j] == ‘@‘ || s[j] == ‘_‘) { i = j-1; break; } if (s[j] == ‘.‘) { if (j - i == 1) break; int k; for (k = j+1; k < n; k++) { if (!isLetter(s[k])) break; y++; } i = k-1; break; } } ans += x * y; } } cout << ans << endl;; return 0; }
Codeforces 412E E-mail Addresses(数论),布布扣,bubuko.com
Codeforces 412E E-mail Addresses(数论)
原文:http://blog.csdn.net/keshuai19940722/article/details/24179639