Art_of_Programming_Contest_Part11

# Art_of_Programming_Contest_Part11 - APPENDIX B COMMON...

This preview shows pages 1–5. Sign up to view the full content.

APPENDIX B COMMON CODES/ROUTINES FOR PROGRAMMING 203 register int i,j; pas[0][0]=1; pas[1][0]=pas[1][1]=1; for(i=2;i<=n;i++) { pas[i][0]=1; for(j=1;j<i;j++) { pas[i][j]= pas[i-1][j-1]+pas[i-1][j]; } pas[i][j]=1; } } main(void) { pascals(10); int n,m; while(scanf("%d%d",&n,&m)!=EOF) { printf("%lu",pas[n][m]); } return 0; } Combination with repeated objects : If in a word of s length character, a character is repeated l times, then the number of arrangement is: If there is more then one repeated character the we can write, where, l1, is the repeation times of first repeated character. l2, is for second repeated character and, so on… but remember calculating both n! and l1!* l2! * l3!* …. . *ln may occurs overflow. So I use

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
APPENDIX B COMMON CODES/ROUTINES FOR PROGRAMMING 204 The code of this type of combination is as follows: #include<stdio.h> #include<math.h> #include<string.h> #define MAX 30 /* *************************************************************** */ /* A sample function that calculate how many ways that you can */ /* rearrange a word with its letter */ /* *************************************************************** */ double test(char *str) { int de[MAX]={0}; int ss[300] = {0}; int l = strlen(str); int i,j=0; double c=1,d=1; for(i=0;i<l;i++) { ss[str[i]]++; if(ss[str[i]] > 1) de[j++] = ss[str[i]]; } c = 1; for(i=2;i<=l;i++) { c*=i; if(j>0) d*= de[--j]; if((d!=1) && !(fmod(c,d))) { c /= d; d=1; } } return c; } /* A sample main function */
APPENDIX B COMMON CODES/ROUTINES FOR PROGRAMMING 205 main(void) { char word[MAX]; int n; int j=0; scanf("%d",&n); for(;n>0;n--) { scanf("%s",word); printf("Data set %d: %.0f",++j,test(word)); putchar('\n'); } return 0; } longest common subsequence (LCS) Given two sequence X and Y, we say that a sequence z is a common subsequence of C and Y if Z is a subsequence of both X and Y. longest common subsequence (LCS) is just the longest " common subsequence " of two sequences. LCS_LENGTH(X,Y) Input two sequence X<x1,x2,x3,. ...xm> and Y<y1,y2,y3. ....yn>. It stores the C[i,j] values in the table C[0. ..m,0. ..n] whose entries are computed in row major order. It also maintain the table b[1. ..m,1. ..n] to simplify construction of an optimal solution. 1. m <- length[X] 2. n <- length[Y] 3. for i <- 1 to m 4. do c[I,0] <- 0 5. for j <- 0 to n 6. do c[0,j] <- 0 7. for i <- 1 to m 8. for j <- 1 to n 9. do if x i = y j 10. then c[i,j] <- c[i -1,j-1]+1 11. b[i,j] <- 1 12. else if c[i – 1,j] >= c[i,j-1] 13. then c[i,j] <- c[i – 1,j] 14. b[i,j] <- 2 15. else c[i,j] <- c[i,j-1] 16. return c and b

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
APPENDIX B COMMON CODES/ROUTINES FOR PROGRAMMING 206 PRINT_ LCS(b,X,i,j) 1. if i = 0 or j = 0 2. then return 3. if b[i,j] = 1 4. then PRINT_LCS(b,X,i-1,j-1) 5. else if b[i,j] = 2 6. then PRINT_LCS(b,X,i-1,j) 7. else PRINT_LCS(b,X,i,j-1) Code for LCS #include<stdio.h> #include<string.h> #define MAX 100 // size of each sequence char str1[MAX],str2[MAX]; // the sequences /* *********************************************** */ /* This function print the LCS to the screen
This is the end of the preview. Sign up to access the rest of the document.

## This document was uploaded on 08/10/2011.

### Page1 / 20

Art_of_Programming_Contest_Part11 - APPENDIX B COMMON...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online