- 题目
- 思路
- AC代码
是一个字符串模拟题,就按照给定的点和@来进行模拟。
AC代码#include#include #include using namespace std; bool have_alpha(string check){ for(int i=0;check[i];++i){ if(check[i]>='a'&&check[i]<='z') return true; } return false; } int main() { string s; cin >> s; //cout< point, at; for (int i = 0; s[i]; ++i) { if (s[i] == '@') at.emplace_back(i); if (s[i] == '.') point.emplace_back(i); } //合法的email中的.一定在@后面 int move_point = 0, move_at = 0, len1 = at.size(), len2 = point.size(), point_index, at_index; while (move_point < len2 && move_at < len1) { point_index = point[move_point]; at_index = at[move_at]; //对应的两个在s中的下标 if (at_index < point_index) { //.在@的后面 string pre; if (move_point == 0) { //说明第一次出现.在@的后面 pre = s.substr(0, at_index); //cout << "pre为" << pre << endl; } else { //说明在后面 for (int i = point[move_point - 1] + 1; i < at_index; ++i) { pre += s[i]; } //cout << "pre为" << pre << endl; } string last; for (int i = point_index + 1; s[i]; ++i) { if (s[i] >= 'a' and s[i] <= 'z') last += s[i]; else break;//如果不是字母就跳出来 } //cout << "last是" << last << endl; string middle = s.substr(at_index + 1, point_index - at_index - 1); //cout << "middle是" << middle << endl; if (have_alpha(pre) and middle.find('_') == -1 and last != "") { int cnt = 0; for (int k = 0; pre[k]; ++k) { if (pre[k] >= 'a' && pre[k] <= 'z') cnt++; } res += cnt * last.size(); move_at++; } else { move_at++; } } else { move_point++; } } cout << res; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)