codeforces Coder-Strike 2014 Round 1 C题解题报告
来源:互联网 发布:数据分析工作计划 编辑:程序博客网 时间:2024/04/29 01:32
Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules.
In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba.
Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle?
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of patterns. Next n lines contain the patterns.
It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters.
In a single line print the answer to the problem — the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them.
2?ab??b
xab
2ab
?
1?a?b
cacb
Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on.
题目大意:
给出N个字符串,且N个字符串长度一样. 字符串由小写字母和'?'组成, '?'可以变换成任意一个小写字母. 两个字符串进行匹配时,如若某个字符不一样,则只需要用'?'替代即可.
求出一个字符串,跟所有的N个字符串进行匹配,且要求'?'个数最少.
解法:
N个字符串扫一遍,按照规则来匹配,如若不一样,则为'?'.
代码:
#include <cstdio>#include <cstring>using namespace std;int n, len;char st1[100010], ans[100010];int main() {scanf("%d\n", &n);for (int i = 1; i <= n; i++) {scanf("%s", st1);len = strlen(st1);for (int j = 0; j < len; j++) {if (i == 1) {if (st1[j] == '?')ans[j] = '!';elseans[j] = st1[j];}else if (st1[j] == '?')continue;else if (ans[j] == '!')ans[j] = st1[j];else if (ans[j] != st1[j])ans[j] = '?';}}for (int i = 0; i <= len; i++) {if (ans[i] == '!')printf("a");elseprintf("%c", ans[i]);}}
- codeforces Coder-Strike 2014 Round 1 C题解题报告
- codeforces Coder-Strike 2014 Round 1 B题解题报告
- codeforces Coder-Strike 2014 Round 1 A题解题报告
- [Codeforces]Coder-Strike 2014 - Round 1
- Coder-Strike 2014 - Round 1
- Coder-Strike 2014 - Round 1
- Coder-Strike 2014 - Round 1
- Coder-Strike 2014 - Qualification Round C. Kicker(推理题)
- 【索引】Coder-Strike 2014 - Round 1
- Coder-Strike 2014 - Round 1__Giving Awards
- 【Codeforces】【Coder-Strike 2014 - Finals】【E Cup Trick】【题解】
- Coder-Strike 2014 - Round 2
- Coder-Strike 2014 - Round 2
- coder-strike 2014 C Kicker
- Coder-Strike 2014 - Round 1 B. Network Configuration
- Coder-Strike 2014 - Round 1 D. Giving Awards
- Coder-Strike 2014 - Round 1__E-mail Addresses
- Codeforces Coder-Strike 2014 - Finals (online edition, Div. 1)
- C++大型项目开发约束
- MAC---安装Mac OS X版Eclipse
- 当时有多少心愿 就有多少的残缺
- vmware workstation虚拟机连接宿主机:桥接、NAT和Host-only是什么意思?
- 我的Java代码生成工具 AutoJCode
- codeforces Coder-Strike 2014 Round 1 C题解题报告
- Activity的四种启动模式和onNewIntent()
- fname=排序法!!!
- 增加新的驱动设备
- 设置全屏及获取屏幕的宽高
- SP2010开发和VS2010专家"食谱"--第一章节--列表和事件Receiver(4)--添加自定义错误信息到事件Receiver
- 设计模式(3)-对象创建型模式-Abstract Factory模式
- sort排序模板
- C#调用VB进行简繁转换