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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值