您的位置:首页 > 其它

uva 644 Immediate Decodability(字符串比对)

2014-06-20 20:19 239 查看
题意是找出编码中是否存在一个编码是另一个编码的前缀。

利用排序的性质,排在前面的肯定是短的,即为前缀。

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char str[1000][100];

int min(int a, int b)
{
return a > b ? b : a;
}

int cmp(void const *va, void const *vb)
{
char *a = (char*)va;
char *b = (char*)vb;
return strcmp(a, b);
}

bool is_decode(int n)
{
int i, j;
for (i = 1; i < n; i++)
{
int m = min(strlen(str[i - 1]), strlen(str[i]));
for (j = 0; j < m; j++)
{
if (str[i - 1][j] != str[i][j])
break;
}
if (j == m)
return false;
}
return true;
}

int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
int ncase = 1;
while (scanf("%s", str[0]) != EOF)
{
int i = 1;
while (scanf("%s", str[i]) && str[i][0] != '9')
i++;

qsort(str, i, sizeof(str[0]), cmp);

if (is_decode(i))
printf("Set %d is immediately decodable\n", ncase++);
else
printf("Set %d is not immediately decodable\n", ncase++);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: