• 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;
    }



  • 相关阅读:
    codeforces 1343D(差分+思维)
    codeforces 1305C(鸽巢原理+同余)
    aa
    codeforces 1325D 位运算+思维
    Codeforces 1312D 组合数学
    Codeforces 1284C 组合数学
    Codeforces 1296C(map)
    CodeForces 1271B(思维)
    CodeForces 1281B(思维+暴力)
    HDU 1506(单调栈)
  • 原文地址:https://www.cnblogs.com/JMDWQ/p/2368799.html
Copyright © 2020-2023  润新知