int main()
{
    int flag,t;
    char s[10];
    freopen("poj1056.txt","r",stdin);
    freopen("poj1056ans.txt","w",stdout);
    for (t=1;scanf("%s",s)!=EOF;t++)
    {
	TrieInit();
	Insert(s);
	flag=1;
	while (scanf("%s",s) && s[0]!='9') if (!Insert(s)) flag=0;
        if (flag) printf("Set %d is immediately decodable\n",t);
        else  printf("Set %d is not immediately decodable\n",t);
    }
    return 0;
}
int main()
{
    int flag,t,n,i;
    char s[15];
    freopen("poj3630.txt","r",stdin);
    freopen("poj3630ans.txt","w",stdout);
    scanf("%d",&t);
    while (t--)
    {
	scanf("%d",&n);
	TrieInit();
	flag=1;
	for (i=0;i<n;i++)
	{
	    scanf("%s",s);
	    if (!Insert(s)) flag=0;
	}
        if (flag) printf("YES\n");
        else  printf("NO\n");
    }
    return 0;
}
Exemple #3
0
rc_t KMain ( int argc, char *argv [] )
{
  rc_t rc;
  MyKVPair *pair;
  char buf[1024];
  int counter = 0;
  String key;
  int len;

  if (argc > 1 && !strcmp(argv[1], "-read")) {
    TrieRead(false);
    return 0;
  }

  if (argc > 1 && !strcmp(argv[1], "-swapread")) {
    TrieRead(true);
    return 0;
  }

  /* the documentation claims to return Unix status codes
     EINVAL or ENOMEM. however, it's more likely we just
     ran out of energy to update the comments...

     this creates a Trie with an initial character set of
     0-9, but which expands to accept new characters as seen
     thanks to the "true" value for "cs_expand"

     a standard Trie would encode the entire string into
     the prefix-tree, while this version only encodes as much
     of the strings initial characters as necessary before
     depositing the nodes into a BSTree at the leaves. you have
     told the code that it can accept up to 512 nodes in the
     BSTree until it has to split them up. to get more standard
     Trie behavior, choose a lower number, e.g. 1 */
  rc = TrieInit(&t, "a-z", 1, true);
  if (rc != 0) 
    LOGERR ( klogInt, rc, "triecreate" );

  /* you might want to get out after such an error... */

  /* another comment for you - using "buf, sizeof buf, stdin"
     will be safer ( I know this is just for experimentation,
     but since I'm reviewing it, I want you to get your money's worth */
  while (NULL != fgets(buf, 1024, stdin)) {
    len = strlen(buf);
    buf[--len] = '\0';
    /* printf("%d\n", len); */
    if (len <= 0)
      break;

    /* in C++, the cast to MyKVPair* is required, whereas
       in C it's not - which is part of its beauty. I prefer
       to avoid the cast unless we're planning on porting to C++,
       because the cast prevents you from changing the type of "pair"
       up above. same goes for "sizeof" - I prefer to let the compiler
       choose the right size for the allocation as "sizeof *pair"
       which will always be correct, even if I were to retype "pair" */
    pair = (MyKVPair *)malloc(sizeof(MyKVPair) + len + 1);
    if (pair == NULL) {
      fprintf(stderr, "Error in malloc\n");
      return -1;
    }
    strcpy(pair->key, buf);
    StringInitCString( &pair->tnode.key, (const char *)&pair->key );
    pair->value = counter++;
    /* fprintf(stderr, "At trieinsert\n"); */

    /* here I'd prefer "&pair->tnode" rather than the typecast
       ( do you get the idea I worry about typecasts? ) */
    rc = TrieInsert( &t, (TNode *)pair );
    if (rc != 0) 
      LOGERR ( klogInt, rc, "triecreate" );    
  }
  while (NULL != fgets(buf, 1024, stdin)) {
    len = strlen(buf);
    buf[--len] = '\0';
    StringInitCString( &key, buf );

    /* the first cast is required, although would normally
       want to be a "const MyKVPair*" so that you are sure
       not to modify it. the second cast is unnecessary */
    pair = (MyKVPair *)TrieFind(&t, (const String *)&key);
    if (pair != NULL) {
      printf("%s %d\n", buf, pair->value);
    }
  }
  Persist("/tmp/foo");
  return 0;
}