zoukankan      html  css  js  c++  java
  • 百度之星2016资格赛D,水题

    很简单的题,主要是要用字符串哈希,把字符串处理成整数。接下来可以继续用hash,也可以像我一样用个map就搞定了。

    /*
     * Author    : ben
     */
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <set>
    #include <map>
    #include <stack>
    #include <string>
    #include <vector>
    #include <deque>
    #include <list>
    #include <functional>
    #include <numeric>
    #include <cctype>
    using namespace std;
    typedef long long LL;
    /*
     * 输入非负整数
     * 支持short、int、long、long long等类型(修改typec即可)。
     * 用法typec a = get_int();返回-1表示输入结束
     */
    typedef int typec;
    typec get_int() {
        typec res = 0, ch;
        while (!((ch = getchar()) >= '0' && ch <= '9')) {
            if (ch == EOF)
                return -1;
        }
        res = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9')
            res = res * 10 + (ch - '0');
        return res;
    }
    //输入整数(包括负整数,故不能通过返回值判断是否输入到EOF,本函数当输入到EOF时,返回-1),用法int a = get_int2();
    int get_int2() {
        int res = 0, ch, flag = 0;
        while (!((ch = getchar()) >= '0' && ch <= '9')) {
            if (ch == '-')
                flag = 1;
            if (ch == EOF)
                return -1;
        }
        res = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9')
            res = res * 10 + (ch - '0');
        if (flag == 1)
            res = -res;
        return res;
    }
    /**
     * 输入一个字符串到str中,与scanf("%s", str)类似,
     * 会忽略掉缓冲区中的空白字符。返回值为输入字符串
     * 的长度,返回-1表示输入结束。
     */
    int get_str(char *str) {
        char c;
        while ((c = getchar()) <= ' ') {
            if(c == EOF) {
                return -1;
            }
        }
        int I = 0;
        while (c > ' ') {
            str[I++] = c; c = getchar();
        }
        str[I] = 0;
        return I;
    }
    
    int hash_code(const char str[]) {
        int h = 0;
        for (int i = 0; str[i] > 0; i++) {
            h = 31 * h + str[i];
        }
        return h;
    }
    
    int main() {
        int N = get_int();
        char str[50];
        map<int, int> mymap;
        for (int i = 0; i < N; i++) {
            get_str(str);
            int len = strlen(str);
            sort(str, str + len);
            int hash = hash_code(str);
            if (mymap.count(hash) > 0) {
                printf("%d
    ", mymap[hash]++);
            } else {
                printf("0
    "); 
                mymap[hash] = 1;
            }
        }
        return 0;
    }
  • 相关阅读:
    @@IDENTITY,SCOPE_IDENTITY和IDENT_CURRENT的辨析
    Blue Jeans[poj3080]题解
    绿色通道题解
    后缀数组
    Power Strings[poj2406]题解
    KMP算法
    Life Forms[poj3294]题解
    STM32固件库文件编程结构思想的理解
    GPIO设置
    HTML_v2
  • 原文地址:https://www.cnblogs.com/moonbay/p/5493609.html
Copyright © 2011-2022 走看看