• 【字符串压缩】


    1.字符串压缩

    本题可使用本地IDE编码,不能使用本地已有代码。

    无跳出限制,编码后请点击 “保存并提交” 按钮进行代码提交。

    解答要求

    时间限制:C/C++ 1000ms | 其他语言 2000ms

    空间限制:C/C++ 256MB | 其他语言 512MB

    64bit IO Format:%lld

    ■ 题目描述

    给定—段英文句子和一个英文单词列表。英文句子包含英文单词和标点符号,其中:
    1)英文单词只包含[a-zA-Z]范围内的字符;

    2)标点符号包括逗号、句号、双引号(双引号两边至少有一个空格)。

    如果列表中有单词在句子中存在(大小写不敏感)且该单词未被双引号包含,则使用该单词在列表中的索引值(索引值从0开始)代替句子中的该单词,

    如果英文单词列表中存在重复的英文单词,则以该单词最后一次出现的索引值进行替换。

    解答要求

    时间限制: C/C++ 400ms,其他语言:800ms
    内存限制 C/C++ 200MB,其他语言:400MB

    输入

    第一行:一段英文句子

    第二行:英文单词列表

    提示:

    每个英文单词长度在[1,50]范围内。
    输入的荣文句子长度在[0,10000]范围内。
    输入的英文单词列表长度在[0,100000]范围内。
    英文句子中不会出现双引号不匹配的情况。

    输出

    替换后的英文句子

    样例1

    输入:

    Hello world.
    Good Hello LOOP

    输出:

    world.

    样例2

    输入:

    An introduction is " the first paragraph " of your paper.
    what say first Second IS introduction IS end

    输出:

    An 5 6 " the first paragraph ” of your paper.

    解释:

    字符串列表中的introduction、IS在句子中存在,first虽然在句子中也存在但是被双引号包含了,

    所以使用introduction单词、IS单词(最后一次出现)的索引值进行替换,

    得到结果为An 5 6 “ the first paragraph ” of your papger

    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    
    using namespace std;
    
    string lower(string s) {
    	for (int i = 0; i < s.size(); i++) {
    		s[i] = tolower(s[i]);
    	}
    	return s;
    }
    
    int main() {
    	string juzi;
    	getline(cin, juzi);
    	unordered_map<string, int> List;
    	string s;
    	int idx = 0;
    	while (cin >> s) {
    		s = lower(s);
    		List[s] = idx;
    		idx++;
    	}
    	//
    	vector<string> words;
    	int sz = juzi.size();
    	string tmp;
    	int flag = 0;
    	for (int i = 0; i < sz; i++) {
    		char ch = juzi[i];
    		if (ch == '"') {
    			tmp += ch;
    			if (flag == 0) flag = 1;
    			else if (flag == 1) {
    				flag = 0;
    				words.push_back(tmp);
    				tmp.clear();
    			}
    			continue;
    		}
    		if (flag == 1) {
    			tmp += ch;
    			continue;
    		}
    		if (isalpha(ch)) {
    			tmp += ch;
    		}
    		if (ch == ' ' || ch == '.' || ch == ',') {
    			if (tmp.size() == 0) continue;
    			words.push_back(tmp);
    			tmp.clear();
    			if (ch == '.' || ch == ',') {
    				tmp += ch;
    				words.push_back(tmp);
    				tmp.clear();
    			}
    		}
    	}
    	//
    	int n = words.size();
    	for (int i = 0; i < n; i++) {
    		string word = words[i];
    		word = lower(word);
    		if (List.count(word)) {
    			words[i] = to_string(List[word]);
    		}
    	}
    	//
    	for (int i = 0; i < n; i++) {
    		if (i == 0) cout << words[i];
    		else {
    			if (words[i] == "," || words[i] == ".") cout << words[i];
    			else cout << " " << words[i];
    		}
    	}
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
  • 相关阅读:
    论文开题报告的研究基础怎么写?
    VOLTE呼叫流程介绍
    Java项目:SSM游戏点评网站
    基于Java实现的设备模拟器
    mysql回表查询和索引覆盖
    鲜花商城管理系统
    Nwafu-OJ-1506 Problem 9 阶段2考试题目3 二分法解方程
    Swagger:在线接口文档
    linux解压文件命令
    Day 88
  • 原文地址:https://blog.csdn.net/LemonShy2019/article/details/126714561