Codeforces883E-Field of Wonders

E. Field of Wonders
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarpus takes part in the "Field of Wonders" TV show. The participants of the show have to guess a hidden word as fast as possible. Initially all the letters of the word are hidden.

The game consists of several turns. At each turn the participant tells a letter and the TV show host responds if there is such letter in the word or not. If there is such letter then the host reveals all such letters. For example, if the hidden word is "abacaba" and the player tells the letter "a", the host will reveal letters at all positions, occupied by "a": 135 and 7 (positions are numbered from left to right starting from 1).

Polycarpus knows m words of exactly the same length as the hidden word. The hidden word is also known to him and appears as one of these m words.

At current moment a number of turns have already been made and some letters (possibly zero) of the hidden word are already revealed. Previously Polycarp has told exactly the letters which are currently revealed.

It is Polycarpus' turn. He wants to tell a letter in such a way, that the TV show host will assuredly reveal at least one more letter. Polycarpus cannot tell the letters, which are already revealed.

Your task is to help Polycarpus and find out the number of letters he can tell so that the show host will assuredly reveal at least one of the remaining letters.

Input

The first line contains one integer n (1 ≤ n ≤ 50) — the length of the hidden word.

The following line describes already revealed letters. It contains the string of length n, which consists of lowercase Latin letters and symbols "*". If there is a letter at some position, then this letter was already revealed. If the position contains symbol "*", then the letter at this position has not been revealed yet. It is guaranteed, that at least one letter is still closed.

The third line contains an integer m (1 ≤ m ≤ 1000) — the number of words of length n, which Polycarpus knows. The following m lines contain the words themselves — n-letter strings of lowercase Latin letters. All words are distinct.

It is guaranteed that the hidden word appears as one of the given m words. Before the current move Polycarp has told exactly the letters which are currently revealed.

Output

Output the single integer — the number of letters Polycarpus can tell so that the TV show host definitely reveals at least one more letter. It is possible that this number is zero.

Examples
input
4
a**d
2
abcd
acbd
output
2
input
5
lo*er
2
lover
loser
output
0
input
3
a*a
2
aaa
aba
output
1
Note

In the first example Polycarpus can tell letters "b" and "c", which assuredly will be revealed.

The second example contains no letters which can be told as it is not clear, which of the letters "v" or "s" is located at the third position of the hidden word.

In the third example Polycarpus exactly knows that the hidden word is "aba", because in case it was "aaa", then the second letter "a" would have already been revealed in one of previous turns.

题解:我们先将没有用的字符串去掉,然后从a到z遍历,若每一个字符串中都有这一个字符,则ans+1。

Code:

var
  n,m,i,j,ans,cnt:longint;
  ch:char;s:string;flag,flag1:boolean;
  f:array['a'..'z'] of longint;
  st:array[0..1005] of string;
  b:array[0..1005] of longint;
begin
  readln(n);
  readln(s);
  for i:=1 to n do
    if s[i]='*' then inc(cnt);
  readln(m);
  for i:=1 to m do readln(st[i]);
  for i:=1 to n do
    if s[i]<>'*' then f[s[i]]:=1;
  for i:=1 to m do
    for j:=1 to n do
    begin
      if (s[j]='*')and(f[st[i][j]]=1) then
      begin
        b[i]:=1;
        break;
      end;
      if (s[j]<>'*')and(st[i][j]<>s[j]) then
      begin
        b[i]:=1;
        break;
      end;
    end;
  ans:=0;
  for ch:='a' to 'z' do
  begin
    flag:=true;
    for i:=1 to m do
      if b[i]=0 then
      begin
        flag1:=true;
        for j:=1 to n do
          if (s[j]='*')and(st[i][j]=ch) then
          begin
            flag1:=false;
            break;
          end;
        if flag1 then
        begin
          flag:=false;
          break;
        end;
      end;
    if flag then inc(ans);
  end;
  writeln(ans);
end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JackflyDC

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值