• L1011. AB


    本题要求你计算A-B。不过麻烦的是,A和B都是字符串 —— 即从字符串A中把字符串B所包含的字符全删掉,剩下的字符组成的就是字符串A-B。

    输入格式:

    输入在2行中先后给出字符串A和B。两字符串的长度都不超过104,并且保证每个字符串都是由可见的ASCII码和空白字符组成,最后以换行符结束。

    输出格式:

    在一行中打印出A-B的结果字符串。

    输入样例:
    I love GPLT!  It's a fun game!
    aeiou
    输出样例:
    I lv GPLT!  It's  fn gm!
     
    时间限制
    100 ms
    内存限制
    65536 kB
    代码长度限制
    8000 B
    判题程序
    Standard
    作者
    陈越

    解题思路:本题是要借助ASCII的编码来建立一个大小为128的数组,对B字符串来进行存储,第i个字符若为a,则将c的数组下标为a的ASCII值的数置位1,这个算法大大降低了时间复杂度。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int main()
    {
        char a[10001];
        char b[10001];
        int c[128];
        int i;

        gets(a);
        gets(b);

        for(i = 0; i < 128; i++)
        {
            c[i] = 0;
        }

        for(i = 0; i < strlen(b); i++)
        {
            c[(int)b[i]] = 1;
        }

        for(i = 0; i < strlen(a); i++)
        {
            if((c[(int)a[i]]) == 0)
                printf("%c", a[i]);
        }
        printf("\n");
        return 0;
    }

    算法复杂度O(n).


  • 相关阅读:
    系统权限控制模型
    [Golang] 剑走偏锋 -- IoComplete ports
    Golang 正则匹配 -- regexp
    golang -- 字符串就地取反
    Hyperledger Fabric chaincode 开发(疑难解答)
    could not launch process: decoding dwarf section info at offset 0x0: too short
    win10 Ubuntu16 双系统
    7-8 哈利·波特的考试
    7-7 六度空间
    7-6 列出连通集
  • 原文地址:https://www.cnblogs.com/beimengmuxi/p/6561408.html
Copyright © 2020-2023  润新知