Skip to content

Instantly share code, notes, and snippets.

@Abreto
Created August 28, 2012 14:06
Show Gist options
  • Save Abreto/3498292 to your computer and use it in GitHub Desktop.
Save Abreto/3498292 to your computer and use it in GitHub Desktop.
CXSJSX_OPENJUDGE_practice1
/* cxsjsx.openjudge.cn/practice1 Problem 026. */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct _set
{
int *data;
int count;
int size;
}set;
int init( set *s, int size );
int destory( set *s );
void __update__( set *s );
int get( set s, int i );
void put( set *s, int i, int x );
int add( set *s, int x );
int del( set *s, int x );
int ask( set s, int x );
int card( set s );
int main(void)
{
int i = 0;
int N = 0;
char com[3];
int x = 0;
set S;
init(&S, 100);
scanf("%d", &N);
for(i = 0;i < N;i++)
{
scanf("%s %d", com, &x);
if( strcmp(com, "add") == 0 )
{
printf("%d\n", add(&S, x));
}
else if( strcmp(com, "del") == 0 )
{
printf("%d\n", del(&S, x));
}
else
{
printf("%d %d\n", ask(S,x), card(S));
}
}
destory(&S);
return 0;
}
int init(set *s, int size)
{
s->data = (int *)malloc( size * sizeof(int) );
s->count = 0;
s->size = size;
}
int destory(set *s)
{
free( s->data );
}
void __update__(set *s)
{
int i = 0;
int *arr = (int *)malloc( (s->size + 10) * sizeof(int) );
for(i = 0;i < card(*s);i++)
*(arr+i) = *(s->data + i);
free(s->data);
s->data = arr;
s->size += 10;
}
int get(set s, int i)
{
return ( *(s.data + i) );
}
void put(set *s, int i, int x)
{
*(s->data + i) = x;
}
int add(set *s, int x)
{
if( s->count == s->size )
__update__(s);
*(s->data + s->count++) = x;
return card(*s);
}
int del(set *s, int x)
{
int i = 0, m = 0;
int c = card(*s);
for(i = 0;i < card(*s);i++)
if( get(*s,i) == x )
{
for(m = i;m < card(*s)-1;m++)
put(s, m, get(*s, m+1));
s->count --;
}
return c;
}
int ask(set s, int x)
{
int i = 0;
for(i = 0;i < card(s);i++)
if( get(s,i) == x )
goto did;
return 0;
did:
return 1;
}
int card(set s)
{
return (s.count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment