首页 > 技术文章 > Hdu 1800 字符串hash

Stomach-ache 2014-05-13 21:35 原文

题目链接

题意: 给出n(n<=3000)个字符串(长度<30,数字组成,肯能存在前导0), 问该序列最少可以分成多少个单调序列.可以转化成求相同字符串的个数的最大值

附上代码:

 1 /*************************************************************************
 2     > File Name: 1800.cpp
 3     > Author: Stomach_ache
 4     > Mail: sudaweitong@gmail.com
 5     > Created Time: 2014年05月13日 星期二 20时12分31秒
 6     > Propose: 
 7  ************************************************************************/
 8 
 9 #include <cmath>
10 #include <string>
11 #include <cstdio>
12 #include <fstream>
13 #include <cstring>
14 #include <iostream>
15 #include <algorithm>
16 using namespace std;
17 
18 int hash[3005];
19 unsigned int
20 BKDHash(const char *str) {
21       unsigned int hash = 0, seed = 131;
22 
23     while (*str == '0') {
24           str++;
25     }
26     while (*str) {
27           hash = hash * seed + (*str++);
28     }
29 
30     return hash & 0x7FFFFFFF;
31 }
32 
33 int
34 cmp(const void *x, const void *y) {
35       return *(int*)x - *(int*)y;
36 }
37 
38 int
39 main(void) {
40       int n;
41     while (~scanf("%d", &n) && n) {
42           for (int i = 0; i < n; i++) {
43               char str[35];
44             scanf("%s", str);
45               hash[i] = BKDHash(str);
46         }
47         qsort(hash, n, sizeof(int), cmp);
48         int ans = 1, cur = hash[0], cnt = 1;
49         for (int i = 1; i < n; i++) {
50               if (hash[i] == cur) {
51                   cnt++;
52                 if (cnt > ans) {
53                       ans = cnt;
54                 }
55             } else {
56                   cnt = 1;
57                 cur = hash[i];
58             }
59         }
60         printf("%d\n", ans);
61     }
62 
63     return 0;
64 }

 

推荐阅读