我是靠谱客的博主 甜甜皮卡丘,最近开发中收集的这篇文章主要介绍ZCMU-2183: Hockey,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

2183: Hockey

Time Limit: 2 Sec   Memory Limit: 256 MB
Submit: 7   Solved: 3
[ Submit][ Status][ Web Board]

Description

Petya loves hockey very much. One day, as he was watching a hockey match, he fell asleep. Petya dreamt of being appointed to change a hockey team's name. Thus, Petya was given the original team name w and the collection of forbidden substrings s1,s2,...,sn. All those strings consist of uppercase and lowercase Latin letters. String w has the length of |w|, its characters are numbered from 1 to |w|.

First Petya should find all the occurrences of forbidden substrings in the w string. During the search of substrings the case of letter shouldn't be taken into consideration. That is, strings "aBC" and "ABc" are considered equal.

After that Petya should perform the replacement of all letters covered by the occurrences. More formally: a letter in the position i should be replaced by any other one if for position i in string w there exist pair of indices l,r (1≤lir≤|w|) such that substring w[l...r] is contained in the collection s1,s2,...,sn, when using case insensitive comparison. During the replacement the letter's case should remain the same. Petya is not allowed to replace the letters that aren't covered by any forbidden substring.

Letter letter (uppercase or lowercase) is considered lucky for the hockey players. That's why Petya should perform the changes so that the letter occurred in the resulting string as many times as possible. Help Petya to find such resulting string. If there are several such strings, find the one that comes first lexicographically.

Note that the process of replacements is not repeated, it occurs only once. That is, if after Petya's replacements the string started to contain new occurrences of bad substrings, Petya pays no attention to them.

Input

The first line contains the only integer n (1≤n≤100) − the number of forbidden substrings in the collection. Next n lines contain these substrings. The next line contains string w. All those n+1 lines are non-empty strings consisting of uppercase and lowercase Latin letters whose length does not exceed 100. The last line contains a lowercase letter letter.

Output

Output the only line − Petya's resulting string with the maximum number of letters letter. If there are several answers then output the one that comes first lexicographically.

The lexicographical comparison is performed by the standard < operator in modern programming languages. The line a is lexicographically smaller than the line b, if a is a prefix of b, or there exists such an i (1≤i≤|a|), that ai<bi, and for any j (1≤j<iaj=bj|a| stands for the length of string a.

Examples
Input
3
bers
ucky
elu
PetrLoveLuckyNumbers
t
Output
PetrLovtTttttNumtttt
Input
4
hello
party
abefglghjdhfgj
IVan
petrsmatchwin
a
Output
petrsmatchwin
Input
2
aCa
cba
abAcaba
c
Output
abCacba

【题意】

给你n个禁用子串w1,w2……wn,在主串w中找出与禁用相同的的子串用字母letter替换。若要替换的子串中有字母与letter相同,

那么就按字典序(PS:如果letter是a,那是b啦,不然就用a好了)去替换。输出时大小写保持不变。

【解析】

看代码注释。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100 + 5;
string s[maxn], sa[maxn], w, wa;
bool m[maxn];
char letter;
string To(string s)//把串转化成小写
{
	int l = s.length();
	for (int i = 0; i < l; i++)
		if (s[i] >= 'A'&&s[i] <= 'Z') s[i] = tolower(s[i]);
	return s;
}
int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> s[i];
		sa[i] = To(s[i]);
	}
	cin >> w >> letter;
	wa = To(w);//主串变小写
	int l = wa.length();
	memset(m, 0, sizeof(m));
	for (int i = 0; i < l; i++)//遍历主串
		for (int k = 0; k < n; k++)//遍历各个子串
			if (wa.substr(i, sa[k].length()) == sa[k])
				//如果在wa中从i开始截取长度为sa[k].length的子串与当前子串相等
				for (int j = i; j < i + sa[k].length(); j++) m[j] = 1;
	if (letter >= 'A'&&letter <= 'Z') letter = tolower(letter);
	for (int i = 0; i < l; i++)
		if (m[i])
		{
			if (wa[i] == letter)//如果要替换的字母与letter 小写相等
			{					//按字典序,无非就两种情况
				char tmp;
				if (wa[i] == 'a') tmp = 'b';//如果这个字母正好是a那么用b来代替
				else tmp = 'a';//不是a则用a
				/*大小写转化放进主串中*/
				if (w[i] >= 'A'&&w[i] <= 'Z') w[i] = tmp - 32;
				else w[i] = tmp;
			}
			//否则就直接大小写转化放进主串中
			else if (w[i] >= 'A'&&w[i] <= 'Z') w[i] = letter - 32;
			else w[i] = letter;
		}
	cout << w << endl;
	return 0;
}

最后

以上就是甜甜皮卡丘为你收集整理的ZCMU-2183: Hockey的全部内容,希望文章能够帮你解决ZCMU-2183: Hockey所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(33)

评论列表共有 0 条评论

立即
投稿
返回
顶部