很簡單的題,主要是要用字符串哈希,把字符串處理成整數。接下來能夠繼續用hash,也能夠像我同樣用個map就搞定了。ios
/* * 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\n", mymap[hash]++); } else { printf("0\n"); mymap[hash] = 1; } } return 0; }