国产L精品国产亚洲区久久,国产精品久久久久久福利漫画,亚洲日韩欧美一区久久久久我http://www.shnenglu.com/Joe/category/14148.html堅信:勤能補拙zh-cnTue, 09 Nov 2010 19:58:17 GMTTue, 09 Nov 2010 19:58:17 GMT60PKU 2408 Anagram Groups (排序)http://www.shnenglu.com/Joe/archive/2010/11/05/132564.htmlsimplyzhaosimplyzhaoFri, 05 Nov 2010 07:38:00 GMThttp://www.shnenglu.com/Joe/archive/2010/11/05/132564.htmlhttp://www.shnenglu.com/Joe/comments/132564.htmlhttp://www.shnenglu.com/Joe/archive/2010/11/05/132564.html#Feedback0http://www.shnenglu.com/Joe/comments/commentRss/132564.htmlhttp://www.shnenglu.com/Joe/services/trackbacks/132564.html
Anagram Groups
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 2318Accepted: 649

Description

World-renowned Prof. A. N. Agram's current research deals with large anagram groups. He has just found a new application for his theory on the distribution of characters in English language texts. Given such a text, you are to find the largest anagram groups. 

A text is a sequence of words. A word w is an anagram of a word v if and only if there is some permutation p of character positions that takes w to v. Then, w and v are in the same anagram group. The size of an anagram group is the number of words in that group. Find the 5 largest anagram groups.

Input

The input contains words composed of lowercase alphabetic characters, separated by whitespace(or new line). It is terminated by EOF. You can assume there will be no more than 30000 words.

Output

Output the 5 largest anagram groups. If there are less than 5 groups, output them all. Sort the groups by decreasing size. Break ties lexicographically by the lexicographical smallest element. For each group output, print its size and its member words. Sort the member words lexicographically and print equal words only once.

Sample Input

undisplayed
trace
tea
singleton
eta
eat
displayed
crate
cater
carte
caret
beta
beat
bate
ate
abet

Sample Output

Group of size 5: caret carte cater crate trace .
Group of size 4: abet bate beat beta .
Group of size 4: ate eat eta tea .
Group of size 1: displayed .
Group of size 1: singleton .

Source

思路:
這題將排序發(fā)揮到了極致啊呵呵,排序來排序去就AC了

代碼:
 1 /* 47MS */
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<string.h>
 5 #define MAX_NUM 30001
 6 #define MAX_LEN 36
 7 #define MAX_OUT 5
 8 struct Word {
 9     char word[MAX_LEN];
10     char word_cmp[MAX_LEN];
11 } words[MAX_NUM];
12 
13 struct Summary {
14     struct Word *first;
15     int count;
16 } smmry[MAX_NUM];
17 
18 int total, total_category;
19 
20 int
21 cmp_char(const void *arg1, const void *arg2)
22 {
23     return (*(char *)arg1) - (*(char *)arg2);
24 }
25 
26 int
27 cmp_words(const void *arg1, const void *arg2)
28 {
29     int ret = strcmp(((struct Word *)arg1)->word_cmp, ((struct Word *)arg2)->word_cmp);
30     if(ret == 0)
31         ret = strcmp(((struct Word *)arg1)->word, ((struct Word *)arg2)->word);
32     return ret;
33 }
34 
35 int
36 cmp_category(const void *arg1, const void *arg2)
37 {
38     int ret = ((struct Summary *)arg2)->count - ((struct Summary *)arg1)->count;
39     if(ret == 0)
40         ret = strcmp(((struct Summary *)arg1)->first->word, ((struct Summary *)arg2)->first->word);
41     return ret;
42 }
43 
44 int
45 main(int argc, char **argv)
46 {
47     int i, j, num, len;
48     total = total_category = 0;
49     while(scanf("%s", words[total].word) != EOF) {
50         len = strlen(words[total].word);
51         strcpy(words[total].word_cmp, words[total].word);
52         qsort(words[total].word_cmp, len, sizeof(char), cmp_char); 
53         ++total;
54     }
55     qsort(words, total, sizeof(struct Word), cmp_words);
56 
57     num = 1;
58     for(i=1; i<total; i++) {
59         if(strcmp(words[i].word_cmp, words[i-1].word_cmp) == 0)
60             ++num;
61         else {
62             smmry[total_category].first = words+i-num;
63             smmry[total_category].count = num;
64             ++total_category;
65             num = 1;
66         }
67     }
68     smmry[total_category].first = words+i-num;
69     smmry[total_category++].count = num;
70     qsort(smmry, total_category, sizeof(struct Summary), cmp_category);
71 
72     total_category = total_category < MAX_OUT ? total_category : MAX_OUT;
73     for(i=0; i<total_category; i++) {
74         printf("Group of size %d: %s ", smmry[i].count, smmry[i].first->word);
75         for(j=1; j<smmry[i].count; j++)
76             if(strcmp((smmry[i].first+j)->word, (smmry[i].first+j-1)->word) != 0)
77                 printf("%s ", (smmry[i].first+j)->word);
78         printf(".\n");
79     }
80 }


simplyzhao 2010-11-05 15:38 發(fā)表評論
]]>
PKU 2418 Hardwood Specieshttp://www.shnenglu.com/Joe/archive/2010/10/30/131801.htmlsimplyzhaosimplyzhaoFri, 29 Oct 2010 16:59:00 GMThttp://www.shnenglu.com/Joe/archive/2010/10/30/131801.htmlhttp://www.shnenglu.com/Joe/comments/131801.htmlhttp://www.shnenglu.com/Joe/archive/2010/10/30/131801.html#Feedback0http://www.shnenglu.com/Joe/comments/commentRss/131801.htmlhttp://www.shnenglu.com/Joe/services/trackbacks/131801.htmlhttp://poj.org/problem?id=2418

思路:
題意清晰明了,不難,用三種方法分別實現:
快速排序
動態(tài)生成節(jié)點的二叉查找樹
靜態(tài)分配節(jié)點的二叉查找樹

結果發(fā)現,原來對于快速排序與靜態(tài)分配節(jié)點都不是很熟悉,二維數組的快速排序分析見http://www.shnenglu.com/Joe/archive/2010/10/29/131746.html,而動態(tài)生成節(jié)點則需要注意如果函數需要修改指針,那么必須傳遞指向指針的指針,因為C是值傳遞的

另外,我以為靜態(tài)分配節(jié)點應該比動態(tài)分配節(jié)點節(jié)約很多時間的,結果居然差不多...而快速排序在這題明顯是最耗時的

代碼(快排)
 1 /* 35640K 1844MS */
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<string.h>
 5 #define MAX_LEN 36
 6 #define MAX_NUM 1000001
 7 char species[MAX_NUM][MAX_LEN];
 8 
 9 int
10 cmp(const void *arg1, const void *arg2)
11 {
12     return strcmp((char *)arg1, (char *)arg2);
13 }
14 
15 int
16 main(int argc, char **argv)
17 {
18     int i, count, total = 0;
19     while(gets(species[total]) != NULL)
20         ++total;
21     qsort(species, total, sizeof(species[0]), cmp);
22     count = 1;
23     for(i=1; i<total; i++) {
24         if(strcmp(species[i], species[i-1]) == 0
25             ++count;
26         else {
27             printf("%s %.4f\n", species[i-1], (count*100.0)/total);
28             count = 1;
29         }
30     }
31     printf("%s %.4f\n", species[total-1], (count*100.0)/total);
32 }

代碼(動態(tài)分配節(jié)點的BST,原本想實現下destroy函數的,結果怕麻煩就留給系統(tǒng)回收吧(*^__^*) 嘻嘻……)
 1 /* binary search tree(dynamic allocation) */
 2 /* 544K 1188MS */
 3 #include<stdio.h>
 4 #include<stdlib.h>
 5 #include<string.h>
 6 #include<assert.h>
 7 #define MAX_LEN 36
 8 struct Node {
 9     char spec[MAX_LEN];
10     int count;
11     struct Node *left, *right;
12 };
13 int total;
14 
15 struct Node *
16 create_node(char *str)
17 {    
18     struct Node *node = (struct Node *)malloc(sizeof(struct Node));
19     assert(node != NULL);
20     strcpy(node->spec, str);
21     node->left = node->right = NULL;
22     node->count = 1;
23     return node;
24 }
25 
26 void
27 insert(struct Node **root, char *str)
28 {
29     int ret;
30     struct Node *node;
31     if(*root==NULL) {
32         *root = create_node(str);
33         return;
34     }
35     ret = strcmp((*root)->spec, str);
36     if(ret == 0)
37         ++((*root)->count);
38     else if(ret < 0)
39         insert(&((*root)->right), str);
40     else
41         insert(&((*root)->left), str);
42 }
43 
44 void
45 inorder(struct Node *root)
46 {
47     if(root == NULL)
48         return;
49     inorder(root->left);
50     printf("%s %.4f\n", root->spec, (root->count)*100.0/total);
51     inorder(root->right);
52 }
53 
54 void
55 destroy(struct Node **root)
56 {
57 }
58 
59 int
60 main(int argc, char **argv)
61 {
62     char str[MAX_LEN];
63     struct Node *bst = NULL;
64     total = 0;
65     while(gets(str) != NULL) {
66         ++total;
67         insert(&bst, str);
68     }
69     inorder(bst);
70 }

代碼(靜態(tài)分配節(jié)點的BST)
 1 /* binary search tree(static allocation) */
 2 /* 492K 1188MS */
 3 #include<stdio.h>
 4 #include<stdlib.h>
 5 #include<string.h>
 6 #include<assert.h>
 7 #define MAX_LEN 36
 8 #define MAX_NUM 10007
 9 #define ROOT 1
10 struct Node {
11     char spec[MAX_LEN];
12     int count;
13     int left, right;
14 }bst[MAX_NUM];
15 int cur_index, total;
16 
17 int
18 find(int root, char *str)
19 {
20     int ret, parent, cur = root;
21     while(cur != 0) {
22         parent = cur;
23         ret = strcmp(bst[cur].spec, str);
24         if(ret == 0) {
25             ++bst[cur].count;
26             return 0;
27         } else if(ret < 0) {
28             cur = bst[cur].right; 
29         } else {
30             cur = bst[cur].left;
31         }
32     }
33     return parent;
34 }
35 
36 #define ADD(index, str) { \
37     strcpy(bst[index].spec, str); \
38     bst[index].left = bst[index].right = 0; \
39     bst[index].count = 1; \
40     ++index; }
41 
42 void
43 insert(int parent, char *str)
44 {
45     int ret = strcmp(bst[parent].spec, str);
46     assert(ret != 0);
47     if(ret < 0)
48         bst[parent].right = cur_index;
49     else 
50         bst[parent].left = cur_index;
51     ADD(cur_index, str);
52 }
53 
54 void
55 inorder(int index) 
56 {
57     if(index == 0)
58         return;
59     inorder(bst[index].left);
60     printf("%s %.4f\n", bst[index].spec, (bst[index].count*100.0)/total);
61     inorder(bst[index].right);
62 }
63 
64 int
65 main(int argc, char **argv)
66 {
67     int parent;
68     char str[MAX_LEN];
69     total = 1;
70     cur_index = ROOT;
71     gets(str);
72     ADD(cur_index, str); /* create the root node first */
73     while(gets(str) != NULL) {
74         ++total;
75         if((parent=find(ROOT, str)) > 0)
76             insert(parent, str);
77     }
78     inorder(ROOT);
79 }



simplyzhao 2010-10-30 00:59 發(fā)表評論
]]>
[Tips][Original] qsort應用于指針數組與二維數組(字符)的差異http://www.shnenglu.com/Joe/archive/2010/10/29/131746.htmlsimplyzhaosimplyzhaoFri, 29 Oct 2010 07:09:00 GMThttp://www.shnenglu.com/Joe/archive/2010/10/29/131746.htmlhttp://www.shnenglu.com/Joe/comments/131746.htmlhttp://www.shnenglu.com/Joe/archive/2010/10/29/131746.html#Feedback0http://www.shnenglu.com/Joe/comments/commentRss/131746.htmlhttp://www.shnenglu.com/Joe/services/trackbacks/131746.html在將qsort函數應用于對指針數組與二維數組排序時,傳遞給compare函數的參數類型是不同的

首先,我們舉個簡單的例子,先將qsort對整數數組排序:
 1 int
 2 cmp(const void *arg1, const void *arg2)
 3 {
 4     return (*(int *)arg1)-(*(int *)arg2);
 5 }
 6 
 7 int
 8 main(int argc, char **argv)
 9 {
10     int i;
11     int arr[] = {31524};
12     qsort(arr, sizeof(arr)/sizeof(arr[0]), sizeof(int), cmp);
13 }
排序針對的是數組里的元素而言的,這里整數數組的元素就是整數,因此qsort的第三個參數就是sizeof(int),而傳遞給比較函數cmp的參數就是相對應的指向整數的指針

接著,我們來看看指針數組的情形:
 1 int
 2 cmp(const void *arg1, const void *arg2)
 3 {
 4     return strcmp((*(char **)arg1), (*(char **)arg2));
 5 }
 6 
 7 int
 8 main(int argc, char **argv)
 9 {
10     int i;
11     /* pointer array */
12     char *str[] = {"java""c""python""perl"}; 
13     qsort(str, sizeof(str)/sizeof(str[0]), sizeof(char *), cmp);
14 }
這里的理解其實跟整數數組差不多,關鍵是抓住數組里元素的類型,既然稱之為指針數組,那數組元素的類型自然就是指針,因此qsort的第三個參數就是sizeof(char *),而傳遞給比較函數cmp的參數就是相對應的指向指針的指針,這里即char **類型

二維數組的理解最為復雜,代碼如下:
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 
 5 int
 6 cmp1(const void *arg1, const void *arg2)
 7 {
 8     return strcmp((*((char (*)[])arg1)), (*((char (*)[])arg2)));
 9 }
10 
11 int
12 cmp2(const void *arg1, const void *arg2)
13 {
14     return strcmp((char *)arg1, (char *)arg2);
15 }
16 
17 int
18 main(int argc, char **argv)
19 {
20     int i;
21     char str1[4][8= {"java""c""python""peal"};
22     printf("COMPARE-FUNCTION-1\n");
23     qsort(str1, 4sizeof(str1[0]), cmp1);
26 
27     char str2[4][8= {"java""c""python""peal"};
28     printf("COMPARE-FUNCTION-2\n");
29     qsort(str2, 4sizeof(str2[0]), cmp2);
34 }
這里cmp1與cmp2都能正常的工作(*^__^*) 嘻嘻……
還是按照上述方法來分析,抓住數組元素的類型來入手,二維數組實際上就是數組的數組,因此二維數組的元素類型就是一維數組,因此qsort的第三個參數就是sizeof(str1[0])或sizeof(str2[0]),那傳遞給比較函數的參數應該就是指向數組的指針,這點可以通過gdb設置斷點來得到證實:
 1 (gdb) p &str1[0]
 2 $1 = (char (*)[8]) 0xbffff2cc
 3 (gdb) p &str1[1]
 4 $2 = (char (*)[8]) 0xbffff2d4
 5 
 6 Breakpoint 2, cmp1 (arg1=0xbffff2cc, arg2=0xbffff2d4) at char_test2.c:8
 7 8        return strcmp((*((char (*)[])arg1)), (*((char (*)[])arg2)));
 8 (gdb) p arg1
 9 $3 = (const void *0xbffff2cc
10 (gdb) p arg2
11 $4 = (const void *0xbffff2d4
12 (gdb) p *(char (*)[])arg1
13 $5 = "j"
14 (gdb) p *(char (*)[8])arg1
15 $6 = "java\000\000\000"
通過第2行與第9行的比較可以發(fā)現,比較函數的參數arg1其實就是&str1[0],類型為char (*)[],這也是為什么cmp1能正常工作的原因
那么cmp2呢,它為什么正確呢?
在cmp1中:strcmp((*((char (*)[])arg1)), (*((char (*)[])arg2))); 這里傳遞給strcmp的參數之所以不會出錯,是因為我們將arg1解地址操作獲得一個數組,而數組名其實是指向數組首元素的指針,arg1既然是指向str1[0]這個一維數組的指針,而str1[0]本身其實就是指向這個一維數組的指針,也就是說arg1其實就是str1[0],因此cmp2能夠正常工作
1 (gdb) p &str1[0]
2 $3 = (char (*)[8]) 0xbffff2cc
3 (gdb) p &str1[0][0]
4 $4 = 0xbffff2cc "java"
5 (gdb) p arg1
6 $5 = (const void *0xbffff2cc
7 (gdb) p (char *)arg1
8 $6 = 0xbffff2cc "java"

額...貌似越說越復雜的樣子,不過這是我理解的過程,見諒...

simplyzhao 2010-10-29 15:09 發(fā)表評論
]]>
PKU 1089 Intervalshttp://www.shnenglu.com/Joe/archive/2010/10/17/130206.htmlsimplyzhaosimplyzhaoSun, 17 Oct 2010 11:35:00 GMThttp://www.shnenglu.com/Joe/archive/2010/10/17/130206.htmlhttp://www.shnenglu.com/Joe/comments/130206.htmlhttp://www.shnenglu.com/Joe/archive/2010/10/17/130206.html#Feedback0http://www.shnenglu.com/Joe/comments/commentRss/130206.htmlhttp://www.shnenglu.com/Joe/services/trackbacks/130206.htmlhttp://poj.org/problem?id=1089

思路:
根據interval的begin升序排列,然后對于interval A, B只存在三種情況:
A包含B、A與B相交、A與B分離

代碼:
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #define MAX_LEN 50001
 5 struct Interval {
 6     int begin, end;
 7 }itvs[MAX_LEN], target[MAX_LEN];
 8 int N, total;
 9 
10 int
11 compare(const void *arg1, const void *arg2)
12 {
13     struct Interval *= (struct Interval *)arg1;
14     struct Interval *= (struct Interval *)arg2;
15     if(a->begin == b->begin)
16         return a->end - b->end;
17     return a->begin - b->begin;
18 }
19 
20 void
21 init()
22 {
23     int i;
24     for(i=0; i<N; i++)
25         scanf("%d %d"&itvs[i].begin, &itvs[i].end);
26     qsort(itvs, N, sizeof(struct Interval), compare);
27     total = 0;
28 }
29 
30 void
31 solve()
32 {
33     int i;
34     struct Interval cur = itvs[0];
35     for(i=1; i<N; i++) {
36         if(itvs[i].begin > cur.end) {
37             target[total++= cur;
38             cur = itvs[i];
39         } else {
40             if(itvs[i].end > cur.end)
41                 cur.end = itvs[i].end;
42         }
43     }
44     target[total++= cur;
45 }
46 
47 void
48 output()
49 {
50     int i;
51     for(i=0; i<total; i++)
52         printf("%d %d\n", target[i].begin, target[i].end);
53 }
54 
55 int
56 main(int argc, char **argv)
57 {
58     while(scanf("%d"&N) != EOF) {
59         init();
60         solve();
61         output();
62     }
63 }


simplyzhao 2010-10-17 19:35 發(fā)表評論
]]>
PKU 1828 Monkeys' Pridehttp://www.shnenglu.com/Joe/archive/2010/09/08/126198.htmlsimplyzhaosimplyzhaoWed, 08 Sep 2010 15:41:00 GMThttp://www.shnenglu.com/Joe/archive/2010/09/08/126198.htmlhttp://www.shnenglu.com/Joe/comments/126198.htmlhttp://www.shnenglu.com/Joe/archive/2010/09/08/126198.html#Feedback0http://www.shnenglu.com/Joe/comments/commentRss/126198.htmlhttp://www.shnenglu.com/Joe/services/trackbacks/126198.htmlhttp://acm.pku.edu.cn/JudgeOnline/problem?id=1828

思路:
按照坐標從上到下、從左到右排序
首先想到的是O(n^2)的算法,時間需要1600+MS
然后,發(fā)現其實在排序之后只要從后向前掃描一遍即可得出結果

代碼:
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #define MAX_LEN 50001
 5 int n;
 6 struct Point {
 7     int x, y;
 8 }points[MAX_LEN];
 9 
10 int
11 compare(const void *arg1, const void *arg2)
12 {
13     struct Point *= (struct Point *)arg1;
14     struct Point *= (struct Point *)arg2;
15     if(a->== b->x)
16         return a->- b->y;
17     return a->- b->x;
18 }
19 
20 int
21 main(int argc, char **argv)
22 {
23     int i, j, cnt, ymax;
24     while(scanf("%d"&n)!=EOF && n) {
25         for(i=0; i<n; i++)
26             scanf("%d %d"&points[i].x, &points[i].y);
27         qsort(points, n, sizeof(struct Point), compare);
28         /* O(n^2) AC 1600+MS 
29         cnt = 0;
30         for(i=0; i<n; i++) {
31             for(j=i+1; j<n; j++) {
32                 if(points[j].y >= points[i].y)
33                     break;
34             }
35             if(j == n)
36                 ++cnt;
37         }
38         */
39         /* O(nlgn) AC 235MS */
40         cnt = 1;
41         ymax = points[n-1].y;
42         for(i=n-2; i>=0; i--) {
43             if(ymax < points[i].y) {
44                 ++cnt;
45                 ymax = points[i].y;
46             }
47         }
48         printf("%d\n", cnt);
49     }
50 }



simplyzhao 2010-09-08 23:41 發(fā)表評論
]]>
PKU 2388 Who's in the Middlehttp://www.shnenglu.com/Joe/archive/2010/07/04/119284.htmlsimplyzhaosimplyzhaoSun, 04 Jul 2010 02:19:00 GMThttp://www.shnenglu.com/Joe/archive/2010/07/04/119284.htmlhttp://www.shnenglu.com/Joe/comments/119284.htmlhttp://www.shnenglu.com/Joe/archive/2010/07/04/119284.html#Feedback0http://www.shnenglu.com/Joe/comments/commentRss/119284.htmlhttp://www.shnenglu.com/Joe/services/trackbacks/119284.htmlhttp://acm.pku.edu.cn/JudgeOnline/problem?id=2388

思路:
1. 排序
這題直接用快排就能AC,很水...

2. 求解第i個順序統(tǒng)計量(詳見CLRS)
   利用快速排序過程中的思路,期望時間復雜度是O(n)

   ps: partition函數的執(zhí)行過程是比較有意思的呵呵
 1 int 
 2 partition(long *arr, int begin, int end)
 3 {
 4     int i, j;
 5     i = begin;
 6     long pivot = arr[begin];
 7     for(j=begin+1; j<=end; j++)
 8         if(arr[j] <= pivot)
 9             swap(arr, ++i, j);
10     swap(arr, i, begin);
11     return i;
12 }
13 
14 long
15 find_ith_os(long *arr, int begin, int end, int ith)
16 {
17     if(begin == end)
18         return arr[begin];
19     int p = partition(arr, begin, end);
20     int k = p-begin+1;
21     if(k == ith)
22         return arr[p];
23     else if(ith < k)
24         return find_ith_os(arr, begin, p-1, ith);
25     else
26         return find_ith_os(arr, p+1, end, ith-k);
27 }






simplyzhao 2010-07-04 10:19 發(fā)表評論
]]>
PKU 1007 DNA Sorting/2299 Ultra-QuickSorthttp://www.shnenglu.com/Joe/archive/2010/07/04/119282.htmlsimplyzhaosimplyzhaoSun, 04 Jul 2010 02:07:00 GMThttp://www.shnenglu.com/Joe/archive/2010/07/04/119282.htmlhttp://www.shnenglu.com/Joe/comments/119282.htmlhttp://www.shnenglu.com/Joe/archive/2010/07/04/119282.html#Feedback0http://www.shnenglu.com/Joe/comments/commentRss/119282.htmlhttp://www.shnenglu.com/Joe/services/trackbacks/119282.html問題:
http://acm.pku.edu.cn/JudgeOnline/problem?id=1007
http://acm.pku.edu.cn/JudgeOnline/problem?id=2299

思路:
求逆序對的個數
這兩題的基本問題是一致的,給定一個數組(包括字符串),求出逆序對的個數

1. 最簡單的方法
兩層循環(huán),復雜度O(n^2)
 1 int
 2 inversion_cal(char *str)
 3 {
 4     int i, j, count = 0;
 5     int len = strlen(str);
 6     for(i=0; i<len; i++)
 7         for(j=i+1; j<len; j++)
 8             if(str[i] > str[j])
 9                 ++count;
10     return count;
11 }

2. 歸并排序&分治思想
其實,只要將歸并排序稍加修改,就是一個求解逆序對個數問題的O(nlgn)方法
要理解的是這其中涉及的分治思想(三步驟):
a. 分解為子問題
b. 求解子問題
c. 合并子問題的解來得到原問題的解
具體對應到求逆序對個數的問題:
a. 將原數組分解為前后兩個子數組
b. 求解子數組的逆序對個數
c. 合并前后子數組,同時計算逆序對個數(這個是指逆序對的第一個數在前子數組中,而第二個數在后子數組中)

 1 long long
 2 merge_count(long *arr, long *temp, long begin, long end)
 3 {
 4     if(begin >= end)
 5         return 0;
 6     long i, j, k, mid = (begin+end)/2;
 7     long long rt = 0;
 8     rt += merge_count(arr, temp, begin, mid);
 9     rt += merge_count(arr, temp, mid+1, end);
10     i = k = begin;
11     j = mid+1;
12     while(i<=mid && j<=end) {
13         if(arr[i] <= arr[j])
14             temp[k++= arr[i++];
15         else {
16             temp[k++= arr[j++];
17             rt += (mid-i+1);
18         }
19     }
20     for( ; i<=mid; i++)
21         temp[k++= arr[i];
22     for( ; j<=end; j++) {
23         temp[k++= arr[j];
24         rt += (mid-i+1);
25     }
26     /* copy */
27     for(k=begin; k<=end; k++)
28         arr[k] = temp[k];
29     return rt;
30 }

3. 特例方法
針對PKU 1007該題的特殊性: 字符串中只包含A, G, T, C四個字母,還有一種更加簡單的O(n)方法

 1 int 
 2 inversion_cal2(char *str)
 3 {
 4     int i, temp[4], count = 0;
 5     int len = strlen(str);
 6     memset(temp, 0sizeof(temp));
 7     for(i=len-1; i>=0; i--) {
 8         switch(str[i]) {
 9             case 'A':
10                 ++temp[0];
11                 break;
12             case 'C':
13                 ++temp[1];
14                 count += temp[0];
15                 break;
16             case 'G':
17                 ++temp[2];
18                 count += (temp[0]+temp[1]);
19                 break;
20             case 'T':
21                 ++temp[3];
22                 count += (temp[0]+temp[1]+temp[2]);
23                 break;
24         }
25     }
26     return count;
27 }



simplyzhao 2010-07-04 10:07 發(fā)表評論
]]>
午夜精品久久久久久99热| 午夜精品久久久久久影视777 | 色综合色天天久久婷婷基地 | 色综合久久无码五十路人妻| 亚洲欧洲久久久精品| 亚洲AⅤ优女AV综合久久久| 国产精品美女久久久久AV福利| 91精品国产91久久久久福利| 久久青青草原精品国产| 亚洲AV无码久久精品狠狠爱浪潮| 7777精品伊人久久久大香线蕉| 久久久久久极精品久久久| 久久久久久亚洲精品无码| 欧美久久一级内射wwwwww.| 国内精品久久久久久久coent| 久久精品国产亚洲av瑜伽| 久久久亚洲精品蜜桃臀| 色综合久久夜色精品国产| 久久精品国产男包| 国产A三级久久精品| 人妻久久久一区二区三区| 99久久精品费精品国产一区二区| 久久国产乱子精品免费女| 精品国产91久久久久久久a| 亚洲人AV永久一区二区三区久久| 久久精品成人欧美大片| 国产产无码乱码精品久久鸭| 久久99精品久久久久久水蜜桃| 久久久99精品成人片中文字幕| 久久影院午夜理论片无码| 欧美亚洲国产精品久久高清| 国产午夜免费高清久久影院| 999久久久免费国产精品播放| 色婷婷久久综合中文久久一本| 亚洲午夜久久久影院伊人| 亚洲精品高清久久| 国产精品久久久香蕉| 成人免费网站久久久| 亚洲欧美国产日韩综合久久| 欧美一区二区三区久久综合 | 久久久久人妻一区精品色|