2021: String Normalization

2021: String Normalization


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
10s8192K415141Standard

Consider a simplified charset C={'0', '1', '*'}, where '*' stands for either '0' or '1'. A string is semi-formed if it contains '*'s, otherwise it is normal-formed. A set of strings is semi-formed if at least one string of the set is semi-formed. Likewise, a set of strings is normal-formed if every string of the set is normal-formed. We can normalize a semi-formed string by replacing every '*' with either '0' or '1'. Usually, we can obtain many normal-formed strings from one semi-formed string. We can also normalize a semi-formed set by normalize every semi-formed string of the set. And the resulted normal-formed set may be multiple also. In this problem, you are to write a program to normalize a semi-formed set.

Input

The input contains several test cases, each of which contains two integers N(<=15) and M(<=2500) in a single line, followed by M lines, each containing N characters(defined under C) representing a semi-formed string. The last test case is marked by N=M=0, which you should not process(terminate your program immediately).

Output

For each semi-formed set, your program should print the number of different normal-formed strings that can be generated during the normalization. For example, the semi-formed set {"10", "*1", "0*"} can generate {"10", "01", "11", "00"}. So the number 4 should be answered.

Sample Input

2 3
10
*1
0*
3 5
00*
*00
1**
0*0
111
0 0

Sample Output

4
7

 

Problem Source: 2nd JLU Programming Contest

#include<iostream>
#include<cstring>
#include<ctype.h>
using namespace std;
bool flag[32767];
char map[2510][20];
int n,m,countc;
void dfs(int step,int id)
{
 if(id==n)
 {
  int sum=0;
  for(int j=0;j<n;j++) sum=sum*2+map[step][j]-'0';
  if(flag[sum]==0) {flag[sum]=1;countc++;}
  return ;
 }  
 int i;
 if(isdigit(map[step][id])) dfs(step,id+1);
 else
 {
  map[step][id]='0';
  dfs(step,id+1);
  map[step][id]='1';
  dfs(step,id+1);
  map[step][id]='*';
 }

int main()
{
 int i,j;
 while(scanf("%d%d",&n,&m)==2&&n&&m)
 {
  for(i=0;i<m;i++)  scanf("%s",map[i]);
  countc=0;
  memset(flag,0,sizeof(flag));
  for(i=0;i<m;i++) dfs(i,0);
  printf("%d/n",countc);
 }
 return 0;
}
  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值