2021: String Normalization
| Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
|---|---|---|---|---|---|
| 10s | 8192K | 415 | 141 | Standard |
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'或'1'的情况进行全面考虑,最终输出所有可能生成的不同规范化字符串的数量。
1566

被折叠的 条评论
为什么被折叠?



