栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

HNU 实验七 字符串 E-mail地址

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

HNU 实验七 字符串 E-mail地址

文章目录
    • 题目
    • 思路
    • 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;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/629601.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号