codeforces 412C Pattern
来源:互联网 发布:php单例模式代码 编辑:程序博客网 时间:2024/04/30 09:15
Description
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?
Input
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.
Output
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.
Sample Input
Input
2
?ab
??b
Output
xab
Input
2
a
b
Output
?
Input
1
?a?b
Output
cacb
Hint
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.
题意:检查每一位是否可以不使用?,如果可以优先使用其他任何字母,如果不可以就必须使用?。
找到一个可以和所有给出的字符串都匹配的字符串,并且使用最小的?号。
#include <cstdio>#include <cstring>#include <iostream>using namespace std;char a[100010],s[100010];int flag[100010];int main (){ memset(flag,0,sizeof(flag)); int n; for(int i=0; i<10010; i++) s[i]='?'; cin>>n; int len; for(int i=0; i<n; i++) { cin>>a; len=strlen(a); for(int j=0; j<len; j++) { if(a[j]==s[j]&&a[j]!='?') {} else if(a[j]!='?') { if(flag[j]==0) { s[j]=a[j]; flag[j]++; } else if(flag[j]>=1) { s[j]='?'; flag[j]++; } } } } for(int i=0;i<len;i++) { if(flag[i]==0) s[i]='a'; cout<<s[i]; } cout<<endl; return 0;}
- codeforces 412C Pattern
- Codeforces 412C Pattern(暴力)
- Codeforces C. Pattern 412 题解
- Codeforces Round #412 C
- CodeForces 722B Verse Pattern
- codeforces 722B Verse Pattern
- codeforces 722 B. Verse Pattern
- codeforces round # 412 c(数论)
- Design Pattern - Command (C#)
- 12c -- Pattern Matching
- Objective-C Singleton Pattern
- Design Pattern - IDisposable Pattern(C#)
- Verse Pattern Codeforces Intel Round-B
- 【57.14%】【codeforces 722B】Verse Pattern
- CodeForces 722B. Verse Pattern(模拟)
- Observer Design Pattern in C#!
- Observer Design Pattern in C#!
- Design Pattern - Singleton(C#)
- CodeForces 546C Soldier and Cards (队列)
- 最长公共子序列和最长公共子串
- calculate pow(x,n)
- C语言开发总结(十一)
- nagios配置详解与集群监控
- codeforces 412C Pattern
- Java——多人聊天室(基于TCP的网络编程——Socket)
- Oracle例外处理
- 智能家居喂鱼系统(五)-树莓派C++库
- TIMESTAMP类型插入到VARCHAR2后转成DATE类型和指定格式字符串
- 菜鸟学Java----基本类型及其封装类
- 关于switch语句中省略break语句的问题
- ViewPager
- objective - C 下字符串的处理方法