TLE csu 1236: 删数游戏

直接做,TLE:

View Code
/* csu 1236 */
# include <stdio.h>
# include <string.h>
# define MAXN 2001
void del(char *s, int len)
{
int i;

i = 1;
while (i < len && s[i-1]<=s[i])
++i;
while (i < len)
{
s[i-1] = s[i];
++i;
}
s[i-1] = '\0';
}
int main()
{
int len, i, S;
char ch[MAXN];

//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);

while (~scanf("%s%d", ch, &S))
{
len = strlen(ch);
for (i = 0; i < S; ++i)
del(ch, len);
printf("%s\n", ch);
}

return 0;
}


改为标记,省去了元素移位,还是TLE,果断要换思路了。。水了这么久,该学学方法了

/* csu 1236 */
# include <stdio.h>
# include <string.h>
# define MAXN 2001
# define INDEX(i) ((i)>>3)
# define OFFSET(i) ((i)%8)
# define get_bit(i) ((m[INDEX(i)]>>OFFSET(i)) & 0x1)
# define set_bit(i) ((m[INDEX(i)]) |= (1<<OFFSET(i)))

char m[INDEX(MAXN)+1];

void mark(char *s, int len)
{
int i;
i = 0;
while (i < len-1)
{
if (s[i]>s[i+1] && !get_bit(i)) break;
++i;
}
set_bit(i);
}

int main()
{
int len, i, S;
char ch[MAXN];

freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);

while (~scanf("%s%d", ch, &S))
{
len = strlen(ch);
for (i = 0; i < S; ++i)
mark(ch, len);
for (i = 0; i < len; ++i)
if (!get_bit(i)) putchar(ch[i]);
putchar('\n');
}

return 0;
}



posted on 2012-02-26 16:43  getgoing  阅读(277)  评论(0编辑  收藏  举报

导航