【字符串压缩】

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<iostream>
#include<stdio.h>
#include<math.h>
#include<cstdio>
#include<string>
#include<iostream>
#include<vector>
#include<set>
#include<unordered_map>
#include<algorithm>
#include<sstream>
#include<queue>
#include<limits.h>
#include<stack>
#include<cmath>
#include<map>
#include<iomanip>
#include<bitset>

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;
}

猜你喜欢

转载自blog.csdn.net/LemonShy2019/article/details/126714561