UVA644 - Immediate Decodability
# 问题描述
输入一系列二进制字符串,判断是否这些二进制中是否为另外一个二进制的前缀,或者存在相等的二进制。
# 思路
学会strstr()函数,可以让代码更加清楚明了。 strstr(str1,str2):判断字符串str2是否为str1的子串,如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL 。
# 代码
#include <iostream>
#include <cstring>
#include <cctype>
#include <cstdio>
using namespace std;
const int N =50;
char str[N][N];
int main()
{
char s[N];
int i,j,kase=1,flag,line=0;
while(gets(s))
{
flag=0;
if(s[0]=='9')
{
for(i=0;i<line;i++)
{
for(j=0;j<line;j++)
{
if(i!=j)
{
if(strstr(str[j],str[i])!=NULL&&strcmp(strstr(str[j],str[i]),str[j])==0) //判断两字符串是否为子串
{
flag=1;
break;
}
if(strcmp(str[i],str[j])==0) //比较字符串是否相等
{
flag=1;
break;
}
}
}
}
printf("Set %d is%s immediately decodable\n",kase++,flag?" not":"");
line=0;
}
else
{
strcpy(str[line++],s);
}
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
编辑 (opens new window)
上次更新: 2022/05/18, 19:03:08