zoukankan      html  css  js  c++  java
  • 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;
    }



  • 相关阅读:
    JavaScript获取数组索引
    jQuery实现购物车效果
    JavaScript随机验证码
    javascript头像上传
    JavaScript中null和undefined的区别
    cookie、sessionStorage和localStorage区别
    Cookie封装
    封装好的Ajax
    通过反射实现javaweb项目中权限的刷新
    java中反射的理解
  • 原文地址:https://www.cnblogs.com/JMDWQ/p/2368799.html
Copyright © 2011-2022 走看看