PTA-中国大学MOOC-陈越、何钦铭-数据结构
01-复杂度1 最大子列和问题
#include <stdio.h> int maxss(int* N, int len); int main() { int k; scanf("%d\n", &k); int i; int N[k]; for (i=0;i<k;i++){ scanf("%d", &N[i]); } printf("%d\n", maxss(N, k)); return 0; } int maxss(int* N, int len){ int ret = 0; int temp = 0; int i; for (i=0;i<len;i++){ temp += N[i]; if(temp<0)temp=0; if(temp>ret)ret=temp; } return ret; }
01-复杂度2 Maximum Subsequence Sum
#include <stdio.h> void maxss(int* ret, int len1, int* N, int len2); int main() { int k; scanf("%d\n", &k); int i; int N[k]; for (i=0;i<k;i++){ scanf("%d", &N[i]); } int ret[3]; maxss(ret, 3, N, k); for (i=0;i<3;i++){ printf("%d", ret[i]); if ( i!=2 ){ printf(" "); } else printf("\n"); } return 0; } void maxss(int* ret, int len1, int* N, int len2){ int sum = -1; int label = 0; int first = 0; int last = len2 - 1; int firTemp = 0; int temp = 0; int i; for (i=0;i<len2;i++){ if (label==0 && temp==0 && N[i]>=0){ firTemp = i; if(N[i]==0) label=1; } temp += N[i]; if (temp == 0)label=1; if(temp<0){ temp=0; label=0; } if(N[i]>=0 && temp>sum){ sum = temp; first = firTemp; last = i; } } if(sum<0)sum=0; ret[0] = sum; ret[1] = N[first]; ret[2] = N[last]; return; }
01-复杂度3 二分查找
Position BinarySearch( List L, ElementType X )
{
int start=1, end=L->Last, middle;
while(end>=start){
middle = (start+end)/2;
if (L->Data[middle]==X){
return middle;
}else if (L->Data[middle]>X){
end = middle-1;
}else{
start = middle + 1;
}
}
return NotFound;
}
02-线性结构1 两个有序链表序列的合并
List Merge( List L1, List L2) { List p, L = (List)malloc(sizeof(struct Node)); p = L; while (L1->Next!=NULL && L2->Next!=NULL){ if( L1->Next->Data < L2->Next->Data){ p->Next = L1->Next; p = p->Next; L1->Next = L1->Next->Next; }else if ( L1->Next->Data > L2->Next->Data){ p->Next = L2->Next; p = p->Next; L2->Next = L2->Next->Next; }else { p->Next = L1->Next; p = p->Next; L1->Next = L1->Next->Next; p->Next = L2->Next; p = p->Next; L2->Next = L2->Next->Next; } } while (L1->Next != NULL){ p->Next = L1->Next; p = p->Next; L1->Next = L1->Next->Next; } while (L2->Next != NULL){ p->Next = L2->Next; p = p->Next; L2->Next = L2->Next->Next; } return L; }
02-线性结构2 一元多项式的乘法与加法运算
#include <stdio.h> typedef struct PolyNode* Polynomial; struct PolyNode{ int coef; int expon; Polynomial link; }; Polynomial ReadPoly(); void Attach(int c, int e, Polynomial *pRear); Polynomial Add(Polynomial P1, Polynomial P2); Polynomial Muti( Polynomial P1, Polynomial P2); void PrintPoly(Polynomial P); int main() { Polynomial P1 = ReadPoly(), P2 = ReadPoly(); Polynomial sum = Add(P1, P2); Polynomial product = Muti(P1, P2); PrintPoly(product); PrintPoly(sum); return 0; } Polynomial ReadPoly() { Polynomial P, Rear,t; int c, e, N; scanf("%d", &N); P = (Polynomial)malloc(sizeof(struct PolyNode)); P->link = NULL; Rear = P; while ( N-- ){ scanf("%d %d", &c, &e); Attach(c, e, &Rear); } t = P; P = P->link; free(t); return P; } void Attach(int c, int e, Polynomial *pRear) { Polynomial P; P = (Polynomial)malloc(sizeof(struct PolyNode)); P->coef = c; P->expon = e; P->link = NULL; (*pRear)->link = P; *pRear = P; } Polynomial Add(Polynomial P1, Polynomial P2) { Polynomial t1=P1,t2=P2; Polynomial t, P = (Polynomial)malloc(sizeof(struct PolyNode)); P->link = NULL; Polynomial Rear = P; while ( t1&&t2 ){ if (t1->expon == t2->expon){ if (t1->coef+t2->coef){ Attach(t1->coef+t2->coef, t1->expon, &Rear); } t1 = t1->link; t2 = t2->link; } else if (t1->expon > t2->expon){ Attach(t1->coef, t1->expon, &Rear); t1 = t1->link; } else { Attach(t2->coef, t2->expon, &Rear); t2 = t2->link; } } while (t1){ Attach(t1->coef, t1->expon, &Rear); t1 = t1->link; } while (t2){ Attach(t2->coef, t2->expon, &Rear); t2 = t2->link; } t = P; P = P->link; free(t); return P; } Polynomial Muti( Polynomial P1, Polynomial P2) { Polynomial P, Rear,t1,t2,t; int c, e; if (!P1||!P2) return NULL; t1=P1;t2=P2; P = (Polynomial)malloc(sizeof(struct PolyNode)); P->link =NULL; Rear = P; while(t2){ Attach(t1->coef*t2->coef, t1->expon+t2->expon, &Rear); t2 = t2->link; } t1 = t1->link; while (t1){ t2 = P2;Rear = P; while (t2){ e = t1->expon+t2->expon; c = t1->coef*t2->coef; while (Rear->link&&Rear->link->expon>e){ Rear = Rear->link; } if (Rear->link&&Rear->link->expon==e){ if (Rear->link->coef+c){ Rear->link->coef+=c; } else{ t = Rear->link; Rear->link = t->link; free(t); } } else { t = (Polynomial)malloc(sizeof(struct PolyNode)); t->coef = c; t->expon = e; t->link = Rear->link; Rear->link = t; Rear = Rear->link; } t2 = t2->link; } t1 = t1->link; } t2 = P;P= P->link;free(t2); return P; } void PrintPoly(Polynomial P) { int flag = 1; if(!P){ printf("0 0\n"); return; } while (P){ if (flag) flag=0; else printf(" "); printf("%d %d",P->coef, P->expon); P = P->link; } printf("\n"); }
02-线性结构3 Reversing Linked List
#include <stdio.h> #include <stdlib.h> #define MAX 100000 int reverse(int fir, int len); int existK(int first, int K); typedef struct Node * List; struct Node { int data; int next; }; List link[MAX]; int main() { int N, K, first; scanf("%d %d %d", &first, &N, &K); int i; int address, data, next; for ( i=0;i<N;i++ ){ scanf("%d %d %d", &address, &data, &next); List p = (List)malloc(sizeof(struct Node)); p->data = data; p->next = next; link[address] = p; } int tfir = first, tlast; for ( i=0;i<N/K;i++ ){ if (existK(tfir, K)){ if(i==0){ first = reverse(tfir, K); } else{ link[tlast]->next = reverse(tfir, K); } tlast = tfir; tfir = link[tfir]->next; } } while (1){ if (link[first]->next!=-1){ printf("%05d %d %05d\n", first, link[first]->data, link[first]->next); } else{ printf("%05d %d %d\n", first, link[first]->data, link[first]->next); break; } first = link[first]->next; } return 0; } int reverse(int fir, int len) { if ( len > 1 ){ fir = reverse(fir, len-1); } if ( len == 1 ){ return fir; } int last = fir; int i; for ( i=1;i<len-1;i++ ){ last = link[last]->next; } int added = link[last]->next; int t = link[added]->next; link[added]->next = fir; link[last]->next = t; fir = added; return fir; } int existK(int first, int K) { int i; for ( i=1;i<K;i++){ if ( link[first]->next == -1 ) {return 0;} first = link[first]->next; } return 1; }
02-线性结构4 Pop Sequence
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> bool isPopSeq(int MaxSize, int N); typedef int Position; struct SNode{ int *Data; Position Top; int MaxSize; }; typedef struct SNode * Stack; Stack CreateStack(int MaxSize) { Stack S = (Stack)malloc(sizeof(struct SNode)); S->Data = (int*)malloc(MaxSize * sizeof(int)); S->Top = -1; S->MaxSize = MaxSize; return S; } bool isFull(Stack S) { return (S->Top == S->MaxSize-1); } bool Push(Stack S, int X) { if( isFull(S)){ printf("堆栈满"); return false; }else{ S->Data[++(S->Top)] = X; return true; } } bool isEmpty(Stack S) { return (S->Top == -1); } int Pop(Stack S) { if (isEmpty(S)){ printf("堆栈空"); return 0; }else{ return (S->Data[(S->Top)--]); } } bool isPopSeq(int MaxSize, int N) { int i, j, num, pvalue; j=1; bool flag = true; Stack S = CreateStack(MaxSize); for (i=1;i<=N;i++){ scanf("%d", &num); if (flag){ if (isEmpty(S)){ if (j>N) { flag = false; continue; } Push(S, j++); } while(1){ pvalue = Pop(S); if (pvalue != num){ Push(S, pvalue); if (isFull(S)){ flag = false; break; }else{ if (j>N){ flag = false; break; } else Push(S, j++); } } else break; } } } return flag; } int main() { int M, N, K; scanf("%d %d %d", &M, &N, &K); int i; int ret[K]; for (i=0;i<K;i++){ if(isPopSeq(M, N)){ ret[i] = 1; } else { ret[i] = 0; } } for (i=0;i<K;i++){ if ( ret[i]==1 ){ printf("YES\n"); } else { printf("NO\n"); } } return 0; }
03-树1 树的同构
#include <stdio.h> typedef struct Node * BinTree; struct Node{ char data; char left; char right; }; void addNode(BinTree*, int, char, char, char); int isSameStruct(BinTree*, int, BinTree*, int); char son(BinTree* bt,int index,int dir); int main() { int i, N1, N2; char data, left, right; scanf("%d\n", &N1); BinTree BinTree1[N1]; for (i=0;i<N1;i++){ scanf("%c %c %c\n", &data, &left, &right); addNode(BinTree1, i, data, left, right); } scanf("%d\n", &N2); BinTree BinTree2[N2]; for (i=0;i<N2;i++){ scanf("%c %c %c\n", &data, &left, &right); addNode(BinTree2, i, data, left, right); } if (isSameStruct(BinTree1, N1, BinTree2, N2)){ printf("Yes"); } else { printf("No"); } return 0; } void addNode(BinTree* bt,int index,char data,char left,char right) { BinTree t = (BinTree)malloc(sizeof(struct Node)); t->data = data; t->left = left; t->right = right; bt[index] = t; } int isSameStruct(BinTree* BinTree1,int N1,BinTree* BinTree2,int N2) { if (N1 != N2) return 0; int i, j, find=0; for ( i=0;i<N1;i++){ for ( j=0;j<N2;j++ ){ if (BinTree1[i]->data == BinTree2[j]->data){ find = 1; if (son(BinTree1, i, 0)!=son(BinTree2, j, 0) && son(BinTree1, i, 0)!=son(BinTree2, j, 1)){ return 0; }else if (son(BinTree1, i, 1)!=son(BinTree2, j, 0) && son(BinTree1, i, 1)!=son(BinTree2, j, 1)){ return 0; } } } if (!find) return 0; find = 0; } return 1; } char son(BinTree* bt,int index,int dir)//左儿子dir为0,右儿子为1 { if (!dir){ if (bt[index]->left == '-'){ return 0; }else{ return bt[bt[index]->left - '0']->data; } }else{ if (bt[index]->right == '-'){ return 0; }else{ return bt[bt[index]->right - '0']->data; } } }
03-树2 List Leaves
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define ElementType int #define ERROR -1 typedef int index; struct TNode{ index left; index right; }; index BinaryTree(struct TNode *Tarray, int N); void traverseBT(struct TNode *Tarray, int N, index first); typedef int Position; struct QNode{ ElementType *Data; Position Front, Rear; int MaxSize; }; typedef struct QNode * Queue; Queue CreateQueue( int MaxSize ) { Queue q = (Queue)malloc(sizeof(struct QNode)); q->Data = (ElementType*)malloc(MaxSize * sizeof(ElementType)); q->Front = 0; q->Rear = 0; q->MaxSize = MaxSize; return q; } bool IsFull( Queue Q ) { return (Q->Rear+1)%Q->MaxSize == Q->Front; } bool AddQ( Queue Q, ElementType X ) { if ( !IsFull(Q) ){ Q->Rear = (Q->Rear+1)%Q->MaxSize; Q->Data[Q->Rear] = X; return true; } else { printf("队列满"); return false; } } bool IsEmpty( Queue Q ) { return Q->Rear == Q->Front; } ElementType DeleteQ( Queue Q ) { if ( !IsEmpty(Q) ){ Q->Front = (Q->Front+1)%Q->MaxSize; return Q->Data[Q->Front]; } else{ printf("队列空"); return ERROR; } } int main() { int N; scanf("%d\n", &N); struct TNode Tarray[N]; int i; char lchild, rchild; for ( i=0;i<N;i++ ){ if ( i< N-1 ){ scanf("%c %c\n", &lchild, &rchild); } else{ scanf("%c %c", &lchild, &rchild); } if (lchild == '-'){ Tarray[i].left = -1; } else{ Tarray[i].left = lchild - '0'; } if (rchild == '-'){ Tarray[i].right = -1; } else{ Tarray[i].right = rchild - '0'; } } index first = BinaryTree(Tarray, N); traverseBT(Tarray, N, first); return 0; } index BinaryTree(struct TNode *Tarray, int N) { int sign[N]; int i; for ( i=0;i<N;i++ ){ sign[i] = 1; } for ( i=0;i<N;i++ ){ if (Tarray[i].left != -1){ sign[Tarray[i].left] = 0; } if (Tarray[i].right != -1){ sign[Tarray[i].right] = 0; } } for ( i=0;i<N;i++ ){ if ( sign[i] ){ return i; } } } void traverseBT(struct TNode *Tarray, int N, index first) { Queue Q = CreateQueue(N+1); AddQ(Q, first); int i, tag=1; while ( !IsEmpty(Q) ){ i = DeleteQ(Q); if ( Tarray[i].left!=-1 ){ AddQ(Q, Tarray[i].left); } if ( Tarray[i].right!=-1 ){ AddQ(Q, Tarray[i].right); } if ( Tarray[i].left==-1 && Tarray[i].right==-1 ){ if ( tag ){ tag = 0; printf("%d", i); } else{ printf(" %d", i); } } } }
03-树3 Tree Traversals Again
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h> #define ElementType BinaryTree #define ERROR NULL typedef struct SNode *Stack; typedef struct TNode *BinaryTree; struct SNode{ ElementType data; Stack next; }; struct TNode{ int data; BinaryTree left, right; }; Stack createStack() { Stack S = (Stack)malloc(sizeof(struct SNode)); S->next = NULL; return S; } bool isEmpty(Stack S) { if ( S->next ){ return false; } else{ return true; } } void push(Stack S, ElementType x) { Stack p = (Stack)malloc(sizeof(struct SNode)); p->data = x; p->next = S->next; S->next = p; } ElementType pop(Stack S) { if (isEmpty(S)){ printf("堆栈空"); return ERROR; } else{ ElementType x = S->next->data; Stack n = S->next; S->next = S->next->next; free(n); return x; } } void postorder(BinaryTree bt) { bool isStart = true; BinaryTree T = bt; Stack S1 = createStack(); Stack S2 = createStack(); while ( T||!isEmpty(S1) ){ while (T){ push(S1, T); push(S2, T); T = T->right; } if ( !isEmpty(S1) ){ T = pop(S1); T = T->left; } } while ( !isEmpty(S2) ){ if (isStart){ isStart = false; T = pop(S2); printf("%d", T->data); } else{ T = pop(S2); printf(" %d", T->data); } } } int main() { Stack S = (Stack)malloc(sizeof(struct SNode)); int N; scanf("%d\n", &N); int i, num; char fstr[5], nstr[5]; BinaryTree root; scanf("%s %d\n", nstr, &num); BinaryTree now = (BinaryTree)malloc(sizeof(struct TNode)); now->data = num; now->left = NULL; now->right = NULL; push(S, now); root = now; strcpy(fstr, nstr); for( i=1;i<2*N;i++ ){ scanf("%s", nstr); if ( !strcmp(nstr, "Push") ){ scanf("%d\n", &num); BinaryTree bt = (BinaryTree)malloc(sizeof(struct TNode)); bt->data = num; bt->left = NULL; bt->right = NULL; push(S, bt); if ( !strcmp(fstr, "Push") ){ now->left = bt; } else{ now->right = bt; } now = bt; } else if ( !strcmp(nstr, "Pop") ){ now = pop(S); } strcpy(fstr, nstr); } postorder(root); return 0; }
04-树4 是否同一棵二叉搜索树
#include <stdio.h> #include <stdlib.h> #include<stdbool.h> typedef struct TNode * BST; struct TNode{ int data; BST left, right; }; void readData(int* data, int N) { int i; for ( i=0;i<N;i++ ){ scanf("%d", &data[i]); } return; } BST addNode(BST T, int i) { if ( !T ){ BST t = (BST)malloc(sizeof(struct TNode)); t->data = i; t->left = t->right = NULL; T = t; } else if ( i<T->data ){ T->left = addNode(T->left, i); } else if ( i>T->data){ T->right = addNode(T->right, i); } return T; } BST buildTree(int* data, int N) { BST T=NULL; int i; for ( i=0;i<N;i++ ){ T = addNode(T, data[i]); } return T; } bool isSame(BST T, BST T1) { bool ret; if ( T==T1){ ret = true; } else if ( T==NULL || T1==NULL ){ ret = false; } else if ( T->data==T1->data ){ ret = (isSame(T->left, T1->left) && isSame(T->right, T1->right)); } else{ ret = false; } return ret; } int main() { int N=1, L, i; int *data; BST T=NULL, T1=NULL; while (true){ scanf("%d", &N); if (N==0){ break; } scanf("%d", &L); data = (int*)malloc(N * sizeof(int)); readData(data, N); T = buildTree(data, N); for ( i=0;i<L;i++ ){ readData(data, N); T1 = buildTree(data, N); if (isSame(T, T1)){ printf("Yes\n"); } else{ printf("No\n"); } } } return 0; }
04-树5 Root of AVL Tree
#include <stdio.h> #include <stdlib.h> typedef struct AVLNode * Position; typedef Position AVLTree; struct AVLNode{ int data; AVLTree left; AVLTree right; int height; }; int max(int a, int b) { return a>b ? a : b; } int getH(AVLTree A) { if (A){ return A->height; } else{ return -1; } } AVLTree LL(AVLTree A) { AVLTree B = A->left; A->left = B->right; B->right = A; A->height = max(getH(A->left), getH(A->right)) + 1; B->height = max(getH(B->left), getH(B->right)) + 1; return B; } AVLTree RR(AVLTree A) { AVLTree B = A->right; A->right = B->left; B->left = A; A->height = max(getH(A->left), getH(A->right)) + 1; B->height = max(getH(B->left), getH(B->right)) + 1; return B; } AVLTree LR(AVLTree A) { AVLTree B = A->left; A->left = RR(B); return LL(A); } AVLTree RL(AVLTree A) { AVLTree B = A->right; A->right = LL(B); return RR(A); } AVLTree insert(AVLTree T, int X) { if (!T){ T = (AVLTree)malloc(sizeof(struct AVLNode)); T->data = X; T->left = NULL; T->right = NULL; } else if ( X<T->data ){ T->left = insert(T->left, X); if (getH(T->left) - getH(T->right)==2){ if ( X<T->left->data ){ T = LL(T); } else{ T = LR(T); } } } else if ( X>T->data ){ T->right = insert(T->right, X); if(getH(T->left) - getH(T->right)==-2){ if ( X>T->right->data ){ T = RR(T); } else{ T = RL(T); } } } T->height = max(getH(T->left), getH(T->right)) + 1; return T; } int main() { int N; scanf("%d", &N); int i, v; AVLTree T = NULL; for ( i=0;i<N;i++ ){ scanf("%d", &v); T = insert(T, v); } printf("%d", T->data); return 0; }
04-树6 Complete Binary Search Tree
#include <stdio.h> int* findMin(int* a, int N, int i) { if ( i>= N ){ return NULL; } else if ( 2*i+1>=N ){ return &a[i]; } else if ( 2*i+2>=N ){ return &a[2*i+1]; } else{ return findMin(a, N, 2*i+1); } } int* findMax(int* a, int N, int i) { if ( i>= N ){ return NULL; } else if ( 2*i+2>=N ){ return &a[i]; } else{ return findMax(a, N, 2*i+2); } } void swap(int* a, int* b) { int t; t = *b; *b = *a; *a = t; return; } void change(int* a, int N) { int i=0; int *v=NULL; while ( i<N ){ if (2*i+1<N){ v = findMax(a, N, 2*i+1); if(a[i]<*v){ swap(&a[i], v); i = 0; continue; } } if (2*i+2<N){ v = findMin(a, N, 2*i+2); if(a[i]>*v){ swap(&a[i], v); i = 0; continue; } } i++; } return; } int main() { int N; scanf("%d",&N); int key[N]; int i; for ( i=0;i<N;i++ ){ scanf("%d", &key[i]); } change(key, N); for ( i=0;i<N;i++ ){ if (i){ printf(" "); } printf("%d", key[i]); } return 0; }
04-树7 二叉搜索树的操作集
BinTree Insert(BinTree BST, ElementType X) { if (!BST){ Position p = (Position)malloc(sizeof(struct TNode)); p->Data = X; p->Left = p->Right = NULL; BST = p; return BST; } else if ( X<BST->Data ){ BST->Left = Insert(BST->Left, X); } else if ( X>BST->Data ){ BST->Right = Insert(BST->Right, X); } return BST; } BinTree Delete(BinTree BST, ElementType X) { if ( !Find(BST, X) ){ printf("Not Found\n"); return BST; } else if ( X<BST->Data ){ BST->Left = Delete(BST->Left, X); return BST; } else if ( X>BST->Data ){ BST->Right = Delete(BST->Right, X); return BST; } else{ if (BST->Left==NULL && BST->Right==NULL){ return NULL; } else if (BST->Left==NULL){ return BST->Right; } else if (BST->Right==NULL){ return BST->Left; } else{ Position p = FindMax(BST->Left); BST->Data = p->Data; BST->Left = Delete(BST->Left, p->Data); return BST; } } } Position Find(BinTree BST, ElementType X) { while( BST ){ if (X<BST->Data){ BST = BST->Left; } else if (X>BST->Data){ BST = BST->Right; } else{ return BST; } } return NULL; } Position FindMin(BinTree BST) { if (!BST){ return NULL; } while ( BST->Left ){ BST = BST->Left; } return BST; } Position FindMax(BinTree BST) { if (!BST){ return NULL; } while ( BST->Right ){ BST = BST->Right; } return BST; }
05-树7 堆中的路径
#include <stdio.h> #include <stdlib.h> typedef struct HNode* MinHeap; struct HNode{ int* data; int capacity; int num; }; void insert(MinHeap H, int v); void print(MinHeap H, int i); int main() { int N, M; MinHeap H = (MinHeap)malloc(sizeof(struct HNode)); scanf("%d %d", &N, &M); H->capacity = N; H->num = 0; H->data = (int*)malloc((N+1)*sizeof(int)); H->data[0] = -10001; int i, v; for ( i=0;i<N;i++ ){ scanf("%d", &v); insert(H, v); } int index; for ( i=0;i<M;i++ ){ scanf("%d", &index); print(H, index); } return 0; } void insert(MinHeap H, int v) { if ( H->num < H->capacity ) { H->num++; } int parent, now = H->num; for ( ;;now/=2 ){ parent = now / 2; if ( v < H->data[parent] ){ H->data[now] = H->data[parent]; } else{ break; } } H->data[now] = v; return; } void print(MinHeap H, int i) { int sign = 1; while ( i>0 ){ if (sign){ printf("%d", H->data[i]); sign = 0; } else{ printf(" %d", H->data[i]); } i /= 2; } printf("\n"); return; }
05-树8 File Transfer
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> typedef int * set; void initial(set S, int N); int root(set S, int e); bool search(set S, int a, int b); void Union(set S, int a, int b); int numOfSet(set S, int N); int main() { int N; scanf("%d", &N); set S = (set)malloc((N+1)*sizeof(int)); initial(S, N); char c; int a, b; while (true){ scanf("%c", &c); if ( c=='S' ){ break; } else { scanf(" %d %d\n", &a, &b); if ( c=='C' ){ if (search(S, a, b)){ printf("yes\n"); } else { printf("no\n"); } } else if ( c=='I' ){ Union(S, a, b); } } } int k = numOfSet(S, N); if ( k== 1){ printf("The network is connected.\n"); } else { printf("There are %d components.", k); } return 0; } void initial(set S, int N) { int i; for ( i=1;i<=N;i++ ){ S[i] = -1; } return; } int root(set S, int e) { while (S[e]>0){ e = S[e]; } return e; } bool search(set S, int a, int b) { return root(S, a)==root(S, b); } void Union(set S, int a, int b) { int r1, r2; r1 = root(S, a); r2 = root(S, b); if ( r1!=r2 ){ if ( S[r1]>S[r2] ){ S[r1] = r2; S[r2]--; } else { S[r2] = r1; S[r1]--; } } return; } int numOfSet(set S, int N) { int i; int num = 0; for ( i=1;i<=N;i++ ){ if (S[i]<0) num++; } return num; }
05-树9 Huffman Codes
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h> typedef struct HuffmanNode* HuffTree; struct HuffmanNode{ char chr; int f; HuffTree left, right; }; typedef struct da * dat; struct da{ char* ch; int* f; int capacity; int number; }; typedef struct HNode* MinHeap; struct HNode{ HuffTree* data; int capacity; int num; }; void insert(MinHeap H, HuffTree v); MinHeap createMinHeap(int N); HuffTree pop(MinHeap H); HuffTree buildHuffTree(MinHeap H); int WPL(HuffTree HT, int k); HuffTree createHuffTree(); int judge(int N, dat d); int main() { int N; scanf("%d", &N); dat d = (dat)malloc(sizeof(struct da)); d->capacity = N; d->number = 0; d->ch = (char*)malloc(N * sizeof(char)); d->f = (int*)malloc(N * sizeof(int)); char c; int f, i; for ( i=0;i<N;i++ ){ if ( i==0 ){ scanf("\n%c %d", &c, &f); } else { scanf(" %c %d", &c, &f); } d->ch[d->number] = c; d->f[d->number++] = f; } MinHeap H = createMinHeap(N); for ( i=0;i<N;i++ ){ HuffTree huffnode = (HuffTree)malloc(sizeof(struct HuffmanNode)); huffnode->chr = d->ch[i]; huffnode->f = d->f[i]; huffnode->left = huffnode->right = NULL; insert(H, huffnode); } HuffTree HT = buildHuffTree(H); int wpl = WPL(HT, 0); int M; scanf("%d", &M); for ( i=0;i<M;i++ ){ if (judge(N, d)==wpl){ printf("Yes\n"); } else { printf("No\n"); } } return 0; } void insert(MinHeap H, HuffTree v) { if ( H->num < H->capacity ) { H->num++; } int parent, now = H->num; for ( ;;now/=2 ){ parent = now / 2; if ( v->f < H->data[parent]->f ){ H->data[now] = H->data[parent]; } else{ break; } } H->data[now] = v; return; } MinHeap createMinHeap(int N) { MinHeap H = (MinHeap)malloc(sizeof(struct HNode)); H->capacity = N; H->data = (HuffTree*)malloc((N+1)*sizeof(HuffTree)); HuffTree h = (HuffTree)malloc(sizeof(struct HuffmanNode)); h->f = -1; H->data[0] = h; H->num = 0; return H; } HuffTree pop(MinHeap H) { HuffTree ret; if ( H->num == 0 ){ ret = NULL; } else{ ret = H->data[1]; int now=1, child; HuffTree v = H->data[H->num--]; for ( ;now*2<=H->num;now = child ){ child = now*2; if ( child<H->num && H->data[child]->f>H->data[child+1]->f ){ child++; } if ( H->data[child]->f < v->f ){ H->data[now] = H->data[child]; } else { break; } } H->data[now] = v; } return ret; } HuffTree buildHuffTree(MinHeap H) { HuffTree min1, min2; HuffTree parent = NULL; while ( H->num>1 ){ min1 = pop(H); min2 = pop(H); parent = (HuffTree)malloc(sizeof(struct HuffmanNode)); parent->f = min1->f + min2->f; parent->left = min1; parent->right = min2; insert(H, parent); } return parent; } int WPL(HuffTree HT, int k) { if (!HT) { return 0; } if (!HT->left){ return k*HT->f; } else { return WPL(HT->left, k+1)+WPL(HT->right, k+1); } } HuffTree createHuffTree() { HuffTree HT = (HuffTree)malloc(sizeof(struct HuffmanNode)); HT->left = HT->right = NULL; HT->f = 0; return HT; } int judge(int N, dat d) { int ret = -1; char c; char* str = malloc(64*sizeof(char)); HuffTree root = createHuffTree(); HuffTree p; int i, j; for ( i=0;i<N;i++ ){ p = root; scanf("\n%c %s", &c, str); for ( j=0;j<strlen(str);j++ ){ if (p->f) { ret = 0; } if (str[j]=='0' ){ if(!p->left) p->left = createHuffTree(); p = p->left; } if (str[j]=='1'){ if ( !p->right ) p->right = createHuffTree(); p = p->right; } } p->f = d->f[i]; } if (ret){ ret = WPL(root, 0); } return ret; }
06-图1 列出连通集
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define ElementType int #define ERROR -1 typedef int Position; struct QNode{ ElementType *Data; Position Front, Rear; int MaxSize; }; typedef struct QNode * Queue; Queue CreateQueue( int MaxSize ) { Queue q = (Queue)malloc(sizeof(struct QNode)); q->Data = (ElementType*)malloc(MaxSize * sizeof(ElementType)); q->Front = 0; q->Rear = 0; q->MaxSize = MaxSize; return q; } bool IsFull( Queue Q ) { return (Q->Rear+1)%Q->MaxSize == Q->Front; } bool AddQ( Queue Q, ElementType X ) { if ( !IsFull(Q) ){ Q->Rear = (Q->Rear+1)%Q->MaxSize; Q->Data[Q->Rear] = X; return true; } else { printf("队列满"); return false; } } bool IsEmpty( Queue Q ) { return Q->Rear == Q->Front; } ElementType DeleteQ( Queue Q ) { if ( !IsEmpty(Q) ){ Q->Front = (Q->Front+1)%Q->MaxSize; return Q->Data[Q->Front]; } else{ printf("队列空"); return ERROR; } } void DFS(int* graph, int N, int ix, int* visited); void BFS(int* graph, int N, int ix, int* visited); void initial(int* visited, int N); int main() { int N, E; scanf("%d %d", &N, &E); int graph[N][N]; int i, j; for ( i=0;i<N;i++ ){ for ( j=0;j<N;j++ ){ graph[i][j] = 0; } } int v1, v2; for ( i=0;i<E;i++ ){ scanf("%d %d", &v1, &v2); graph[v1][v2] = 1; graph[v2][v1] = 1; } int visited[N]; initial(visited, N); for ( i=0;i<N;i++ ){ if ( !visited[i] ){ printf("{"); DFS(graph[0], N, i, visited); printf(" }\n"); } } initial(visited, N); for ( i=0;i<N;i++ ){ if ( !visited[i] ){ printf("{"); BFS(graph[0], N, i, visited); printf(" }\n"); } } return 0; } void DFS(int* graph, int N, int ix, int* visited) { int i; if ( !visited[ix] ){ printf(" %d", ix); visited[ix] = 1; for ( i=0;i<N;i++ ){ if ( !visited[i] && graph[ix*N+i] ){ DFS(graph, N, i, visited); } } } return; } void BFS(int* graph, int N, int ix, int* visited) { int i; Queue q = CreateQueue(N); if ( !visited[ix] ){ printf(" %d", ix); visited[ix] = 1; } for ( i=0;i<N;i++ ){ if ( !visited[i] && graph[ix*N+i] ){ printf(" %d", i); AddQ(q, i); visited[i] = 1; } } while ( !IsEmpty(q) ){ i = DeleteQ(q); BFS(graph, N, i, visited); } } void initial(int* visited, int N) { int i; for ( i=0;i<N;i++ ){ visited[i] = 0; } }
06-图2 Saving James Bond - Easy Version
#include <stdio.h> int isSafe(int* v, int D); int jump(int* v, int* w, int D); int DFS(int* node, int D, int* visited, int v, int N); int main() { int N, D; scanf("%d %d", &N, &D); int node[N][2]; int i; for ( i=0;i<N;i++ ){ scanf("%d %d", &node[i][0], &node[i][1]); } int visited[N]; for ( i=0;i<N;i++ ){ visited[i] = 0; } int center[2] = {0, 0}; int sign = 0; if ( isSafe(center, D+7.5) ){ sign = 1; } else { for ( i=0;i<N;i++ ){ if ( !visited[i] && jump(center, node[i], D+7.5)){ if (DFS(node[0], D, visited, i, N)){ sign = 1; break; } } } } if ( sign ){ printf("Yes"); } else { printf("No"); } return 0; } int isSafe(int* vnode, int D) { return (vnode[0]<=-50+D || vnode[0]>=50-D || vnode[1]<=-50+D || vnode[1]>=50-D); } int jump(int* v, int* w, int D) { return ((v[0]-w[0])*(v[0]-w[0])+(v[1]-w[1])*(v[1]-w[1]))<=D*D; } int DFS(int* node, int D, int* visited, int v, int N) { if ( isSafe(node+2*v, D) ){ return 1; } int ret = 0; int i; if (!visited[v] ){ visited[v] = 1; for ( i=0;i<N;i++ ){ if (!visited[i] && jump(node+2*v, node+2*i, D)){ ret = DFS(node, D, visited, i, N); if (ret) return ret; } } } return ret; }
06-图3 六度空间
#include <stdio.h> #include <stdlib.h> typedef struct GNode * PtrToGNode; typedef struct Ap * PtrToAp; typedef struct Graphcontent * Graph; typedef struct QNode * PtrToQNode; typedef struct queNode * Queue; Graph createGraph(int N); int BFS(Graph G, int index); struct GNode{ int order; PtrToAp next; }; struct Ap{ PtrToGNode adjpt; PtrToAp next; }; struct Graphcontent{ PtrToGNode* Node; int N; }; struct QNode{ PtrToGNode Qdata; PtrToQNode next; }; struct queNode{ PtrToQNode front, rear; }; Queue createQueue() { Queue q = (Queue)malloc(sizeof(struct queNode)); q->front = q->rear = NULL; return q; } int isEmpty(Queue Q) { return (Q->front == NULL); } void addQ(Queue Q, PtrToGNode G) { PtrToQNode q = (PtrToQNode)malloc(sizeof(struct QNode)); q->Qdata = G; q->next = NULL; if (isEmpty(Q)){ Q->front = Q->rear = q; } else { Q->rear->next = q; Q->rear = q; } } PtrToGNode deleteQ(Queue Q) { if (isEmpty(Q)){ return NULL; } else { PtrToQNode p = Q->front; Q->front = p->next; PtrToGNode ret = p->Qdata; free(p); return ret; } } int main() { int N, M; scanf("%d %d", &N, &M); Graph G = createGraph(N); int i; int anode, bnode; PtrToAp ap; for ( i=0;i<M;i++ ){ scanf("%d %d", &anode, &bnode); ap = (PtrToAp)malloc(sizeof(struct Ap)); ap->adjpt = G->Node[bnode-1]; ap->next = G->Node[anode-1]->next; G->Node[anode-1]->next = ap; ap = (PtrToAp)malloc(sizeof(struct Ap)); ap->adjpt = G->Node[anode-1]; ap->next = G->Node[bnode-1]->next; G->Node[bnode-1]->next = ap; } for ( i=0;i<N;i++ ){ printf("%d: %.2f%%\n", i+1, 100.0*BFS(G, i)/N); } return 0; } Graph createGraph(int N) { Graph G = (Graph)malloc(sizeof(struct Graphcontent)); G->N = N; G->Node = (PtrToGNode*)malloc(N*sizeof(PtrToGNode)); int i; for ( i=0;i<N;i++ ){ G->Node[i] = malloc(sizeof(struct GNode)); G->Node[i]->order = i+1; G->Node[i]->next = NULL; } return G; } int BFS(Graph G, int index) { int* visited = (int*)malloc(G->N*sizeof(int)); int i; for ( i=0;i<G->N;i++ ){ visited[i] = 0; } int count=0, level = 0; PtrToGNode p, end1, end2; end1 = end2 = G->Node[index]; PtrToAp q; Queue Q = createQueue(); addQ(Q, G->Node[index]); visited[G->Node[index]->order-1] = 1; count++; while (!isEmpty(Q) && level<6){ p = deleteQ(Q); q = p->next; while ( q ){ if ( !visited[q->adjpt->order-1] ){ end2 = q->adjpt; addQ(Q, q->adjpt); visited[q->adjpt->order-1] = 1; count++; } q = q->next; } if ( p==end1 ){ level++; end1 = end2; } } while ( !isEmpty(Q) ){ deleteQ(Q); } free(visited); free(Q); return count; }
1076 Forwards on Weibo
#include <stdio.h> #include <stdlib.h> typedef struct wbuser * User; typedef struct wbfollower * fans; typedef struct wbgraph * Graph; typedef struct QNode * PtrToQNode; typedef struct queNode * Queue; struct wbuser{ int ID; fans next; }; struct wbfollower{ User u; fans next; }; struct wbgraph{ User* V; int N; }; struct QNode{ User Qdata; PtrToQNode next; }; struct queNode{ PtrToQNode front, rear; }; Queue createQueue() { Queue q = (Queue)malloc(sizeof(struct queNode)); q->front = q->rear = NULL; return q; } int isEmpty(Queue Q) { return (Q->front == NULL); } void addQ(Queue Q, User G) { PtrToQNode q = (PtrToQNode)malloc(sizeof(struct QNode)); q->Qdata = G; q->next = NULL; if (isEmpty(Q)){ Q->front = Q->rear = q; } else { Q->rear->next = q; Q->rear = q; } } User deleteQ(Queue Q) { if (isEmpty(Q)){ return NULL; } else { PtrToQNode p = Q->front; Q->front = p->next; User ret = p->Qdata; free(p); return ret; } } Graph createGraph(int N) { Graph G = (Graph)malloc(sizeof(struct wbgraph)); G->N = N; G->V = (User*)malloc(N*sizeof(User)); int i; for ( i=0;i<N;i++ ){ G->V[i] = (User)malloc(sizeof(struct wbuser)); G->V[i]->ID = i+1; G->V[i]->next = NULL; } return G; } int BFS(Graph G, int ID, int L) { int visited[G->N]; int i; for ( i=0;i<G->N;i++ ){ visited[i] = 0; } int count=0, level=0; User end1, end2, p; fans q; end1 = end2 = G->V[ID-1]; Queue Q = createQueue(); addQ(Q, G->V[ID-1]); count++; visited[ID-1] = 1; while ( !isEmpty(Q) && level<L ){ p = deleteQ(Q); q = p->next; while ( q ){ if ( !visited[q->u->ID-1] ){ addQ(Q, q->u); end2 = q->u; visited[q->u->ID-1] = 1; count++; } q = q->next; } if ( p==end1 ){ level++; end1 = end2; } } while ( !isEmpty(Q) ){ deleteQ(Q); } free(Q); return count-1; } int main() { int N, L; scanf("%d %d", &N, &L); Graph G = createGraph(N); int i, j; int Mi, userid; fans f; for ( i=0;i<N;i++ ){ scanf("%d", &Mi); for ( j=0;j<Mi;j++ ){ scanf("%d", &userid); f = (fans)malloc(sizeof(struct wbfollower)); f->u = G->V[i]; f->next = G->V[userid-1]->next; G->V[userid-1]->next = f; } } int K; scanf("%d", &K); for ( i=0;i<K;i++ ){ scanf("%d", &userid); printf("%d\n", BFS(G, userid, L)); } return 0; }
07-图4 哈利·波特的考试
#include <stdio.h> #include <stdlib.h> #define inf 200 typedef int Vertex; typedef struct ENode * Edge; struct ENode{ Vertex v, w; int weight; }; typedef struct graph * MGraph; struct graph{ int* matrix; int N; }; MGraph createGraph(int N) { MGraph G = (MGraph)malloc(sizeof(struct graph)); G->N = N; G->matrix = (int*)malloc(N*N*sizeof(int)); int i, j; for ( i=0;i<N;i++ ){ for ( j=0;j<N;j++ ){ G->matrix[i*N+j] = inf; } } return G; } void addEdge(MGraph G, Edge E) { G->matrix[E->v*G->N+E->w] = E->weight; G->matrix[E->w*G->N+E->v] = E->weight; return; } void Floyd(MGraph G, int* ret) { int i, j, k; for ( i=0;i<G->N;i++ ){ for ( j=0;j<G->N;j++ ){ ret[i*G->N+j] = G->matrix[i*G->N+j]; } } for ( k=0;k<G->N;k++ ){ for ( i=0;i<G->N;i++ ){ for ( j=0;j<G->N;j++ ){ if ( ret[i*G->N+k] + ret[k*G->N+j] < ret[i*G->N+j]){ ret[i*G->N+j] = ret[i*G->N+k] + ret[k*G->N+j]; } } } } } int main() { int N, M; scanf("%d %d", &N, &M); MGraph G = createGraph(N); Edge E = (Edge)malloc(sizeof(struct ENode)); int i, j; for ( i=0;i<M;i++ ){ scanf("%d %d %d", &E->v, &E->w, &E->weight); E->v--; E->w--; addEdge(G, E); } int ret[N*N]; Floyd(G, ret); int index = -1, max1, max2; max1 = inf; for ( i=0;i<N;i++ ){ max2 = 0; for ( j=0;j<N;j++ ){ if ( i!=j && ret[i*N+j]>max2 ){ max2 = ret[i*N+j]; } } if ( max2<max1 ){ index = i; max1 = max2; } } index++; if ( index ){ printf("%d %d", index, max1); } else { printf("%d", index); } return 0; }
07-图5 Saving James Bond - Hard Version
#include <stdio.h> #include <stdlib.h> #define inf 10000 typedef int vertex; struct ENode{ vertex v, w; }; typedef struct GNode * Graph; struct GNode{ int* matrix; int* data; int N; }; Graph createGraph(int N) { Graph G = (Graph)malloc(sizeof(struct GNode)); G->N = N; G->matrix = (int*)malloc(N*N*sizeof(int)); G->data = (int*)malloc(2*N*sizeof(int)); int i, j; for ( i=0;i<N;i++ ){ for ( j=0;j<N;j++ ){ G->matrix[i*N+j] = inf; } } return G; } void readVertex(Graph G) { int i; G->data[0] = G->data[1] = 0; for ( i=1;i<G->N;i++ ){ scanf("%d %d", &G->data[i*2], &G->data[i*2+1]); } } int distance(Graph G, vertex v, vertex w) { int cmp = (G->data[v*2]-G->data[w*2])*(G->data[v*2]-G->data[w*2])+ (G->data[v*2+1]-G->data[w*2+1])*(G->data[v*2+1]-G->data[w*2+1]); return cmp; } int Jump(Graph G, vertex v, vertex w, int D) { int cmp = (G->data[v*2]-G->data[w*2])*(G->data[v*2]-G->data[w*2])+ (G->data[v*2+1]-G->data[w*2+1])*(G->data[v*2+1]-G->data[w*2+1]); if ( v&&w ){ return cmp<=D*D; } else { return cmp<=(D+7.5)*(D+7.5); } } void addEdge(Graph G, int D) { int i, j; for ( i=0;i<G->N;i++ ){ for ( j=0;j<G->N;j++ ){ if ( i!=j && Jump(G, i, j, D) ){ G->matrix[i*G->N+j] =1; } } } } void Floyd(Graph G, int* ret, int* path) { int i, j, k; for ( i=0;i<G->N;i++ ){ for ( j=0;j<G->N;j++ ){ ret[i*G->N+j] = G->matrix[i*G->N+j]; path[i*G->N+j] = -1; } } for ( k=0;k<G->N;k++ ){ for ( i=0;i<G->N;i++ ){ for ( j=0;j<G->N;j++ ){ if ( i!=j && ret[i*G->N+k]+ret[k*G->N+j]<ret[i*G->N+j] ){ ret[i*G->N+j] = ret[i*G->N+k]+ret[k*G->N+j]; path[i*G->N+j] = k; } } } } return; } int isSafe(Graph G, vertex v, int D) { return G->data[v*2]<=-50+D || G->data[v*2]>=50-D || G->data[v*2+1]<=-50+D || G->data[v*2+1]>=50-D; } int firstDist(Graph G, vertex v, int* ret, int* path) { if (ret[v]==inf){ return inf; } else { while ( path[v]!=-1){ v = path[v]; } return distance(G, 0, v); } } void printPath(Graph G, int* path, vertex v, vertex w) { if ( path[v*G->N+w]==-1 ){ printf("%d %d\n", G->data[w*2], G->data[w*2+1]); } else { printPath(G, path, v, path[v*G->N+w]); printPath(G, path, path[v*G->N+w], w); } } int main() { int N, D; scanf("%d %d", &N, &D); Graph G = createGraph(N+1); readVertex(G); addEdge(G, D); int ret[G->N*G->N], path[G->N*G->N]; Floyd(G, ret, path); int i; if ( D>=50 ) printf("1"); else { int destination = 0, weight = inf, fdist = inf; for ( i=1;i<G->N;i++ ){ if ( isSafe(G, i, D) && ret[i]<inf){ if (ret[i]<weight){ destination = i; weight = ret[i]; fdist = firstDist(G, i, ret, path); } else if (ret[i]==weight) { if ( firstDist(G, i, ret, path)<fdist){ destination = i; weight = ret[i]; fdist = firstDist(G, i, ret, path); } } } } if ( !destination ){ printf("0"); } else { printf("%d\n", weight+1); printPath(G, path, 0, destination); } } return 0; }
07-图6 旅游规划
#include <stdio.h> #include <stdlib.h> #define MAX 500 #define inf 1000000 #define ERROR -1 typedef int Vertex; typedef struct ENode * Edge; struct ENode{ Vertex v, w; int weight, cost; }; typedef struct Mgraph * Graph; struct Mgraph{ int g[MAX][MAX]; int c[MAX][MAX]; int Nv; }; Graph createGraph(int N) { Graph G = (Graph)malloc(sizeof(struct Mgraph)); G->Nv = N; Vertex i, j; for ( i=0;i<N;i++ ){ for ( j=0;j<N;j++ ){ G->g[i][j] = G->c[i][j] = inf; } } return G; } void addEdge(Graph G, Edge E) { G->g[E->v][E->w] = E->weight; G->c[E->v][E->w] = E->cost; G->g[E->w][E->v] = E->weight; G->c[E->w][E->v] = E->cost; } Vertex findMinDist(Graph G, int dist[], int collected[]) { Vertex MinV, v; int MinDist = inf; for ( v=0;v<G->Nv;v++ ){ if ( !collected[v] && dist[v]<MinDist ){ MinV = v; MinDist = dist[v]; } } if ( MinDist<inf ){ return MinV; } else { return ERROR; } } void Dijkstra(Graph G, int dist[], int cost[], int path[], Vertex S) { int collected[MAX]; Vertex v, w; int tempDist, tempCost; for ( v=0;v<G->Nv;v++ ){ collected[v] = 0; dist[v] = G->g[S][v]; cost[v] = G->c[S][v]; if ( dist[v]<inf ){ path[v] = S; } else { path[v] = -1; } } dist[S] = 0; cost[S] = 0; collected[v] = 1; while (1){ v = findMinDist(G, dist, collected); if ( v==ERROR ) { break; } collected[v] = 1; for ( w=0;w<G->Nv;w++ ){ tempDist = dist[v] + G->g[v][w]; tempCost = cost[v] + G->c[v][w]; if ( dist[w]==tempDist && tempCost<cost[w] ){ cost[w] = tempCost; } if ( tempDist<dist[w] ){ dist[w] = tempDist; cost[w] = tempCost; path[w] = v; } } } return; } int main() { int N, M, S, D; scanf("%d %d %d %d", &N, &M, &S, &D); Graph G = createGraph(N); Edge E = (Edge)malloc(sizeof(struct ENode)); int i; for ( i=0;i<M;i++ ){ scanf("%d %d %d %d", &E->v, &E->w, &E->weight, &E->cost); addEdge(G, E); } int dist[N], cost[N], path[N]; Dijkstra(G, dist, cost, path, S); printf("%d %d", dist[D], cost[D]); return 0; }
1003 Emergency
#include <stdio.h> #include <stdlib.h> #define MAX 500 #define inf 10000 #define ERROR -1 typedef int Vertex; typedef struct ENode * Edge; struct ENode{ Vertex v, w; int weight; }; typedef struct Mgraph * Graph; struct Mgraph{ int g[MAX][MAX]; int n[MAX]; int Nv; }; Graph createGraph(int N) { Graph G = (Graph)malloc(sizeof(struct Mgraph)); G->Nv = N; Vertex v, w; for ( v=0;v<N;v++ ){ for ( w=0;w<N;w++ ){ G->g[v][w] = inf; } } return G; } void addEdge(Graph G, Edge E) { G->g[E->v][E->w] = E->weight; G->g[E->w][E->v] = E->weight; } Vertex findMinDist(Graph G, int dist[], int collected[]) { Vertex MinV, v; int MinDist = inf; for ( v=0;v<G->Nv;v++ ){ if ( !collected[v] && dist[v]<MinDist ){ MinV = v; MinDist = dist[v]; } } if ( MinDist==inf ){ return ERROR; } else { return MinV; } } int getNumOfQueue(Graph G, int path[], Vertex C2) { Vertex v; int num=0; v = C2; while (1) { num += G->n[v]; if ( path[v] == -1 ){ break; } v = path[v]; } return num; } void Dijkstra(Graph G, Vertex C1, Vertex C2) { int collected[MAX], path[MAX], dist[MAX], count[MAX]; int numOfQueue1, t; Vertex v, w; for ( v=0;v<G->Nv;v++ ){ collected[v] = 0; dist[v] = G->g[C1][v]; if ( dist[v]==inf ){ path[v] = -1; count[v] = 0; } else { path[v] = C1; count[v] = 1; } } count[C1] = 1; dist[C1] = 0; collected[C1] = 1; while (1) { v = findMinDist(G, dist, collected); if ( v==ERROR ) { break; } collected[v] = 1; for ( w=0;w<G->Nv;w++ ){ if ( G->g[v][w]<inf ){ if ( dist[v]+G->g[v][w] == dist[w] ){ numOfQueue1 = getNumOfQueue(G, path, w); t = path[w]; path[w] = v; if ( numOfQueue1 > getNumOfQueue(G, path, w) ){ path[w] = t; } count[w] += count[v]; } if ( dist[v]+G->g[v][w] < dist[w] ){ count[w] = count[v]; dist[w] = dist[v] + G->g[v][w]; path[w] = v; } } } } printf("%d %d", count[C2], getNumOfQueue(G, path, C2)); } int main() { int N, M; Vertex C1, C2; scanf("%d %d %d %d", &N, &M, &C1, &C2); Graph G = createGraph(N); Vertex v; for ( v=0;v<N;v++ ){ scanf("%d", &G->n[v]); } Edge E = (Edge)malloc(sizeof(struct ENode)); for ( v=0;v<M;v++ ){ scanf("%d %d %d", &E->v, &E->w, &E->weight); addEdge(G, E); } Dijkstra(G, C1, C2); return 0; } //注意起始节点和终止节点相同时路径数为1;
1072 Gas Station
#include <stdio.h> #include <stdlib.h> #include <math.h> #define inf 10000 #define ERROR -1 #define NMAX 1000 #define MMAX 10 #define MAX NMAX+MMAX double round(double); typedef int Vertex; typedef struct ENode * Edge; struct ENode{ Vertex v, w; int weight; }; typedef struct Mgraph * Graph; struct Mgraph{ int g[MAX][MAX]; int Nh, Ns; }; Graph createGraph(int N, int M) { Graph G = (Graph)malloc(sizeof(struct Mgraph)); G->Nh = N; G->Ns = M; Vertex v, w; for ( v=0;v<MAX;v++ ){ for ( w=0;w<MAX;w++ ){ G->g[v][w] = inf; } } return G; } void addEdge(Graph G, Edge E) { G->g[E->v][E->w] = E->weight; G->g[E->w][E->v] = E->weight; } Vertex strToVertex(char str[]) { Vertex v=0; int i; if ( str[0]=='G' ){ for ( i=1;str[i];i++ ){ v = v*10+(str[i]-'0'); } v += NMAX; } else { for ( i=0;str[i];i++ ){ v = v*10+(str[i]-'0'); } } v--; return v; } Vertex findMinDist(Graph G, Vertex S, int dist[][MAX], int collected[]) { Vertex MinV, v; int MinDist = inf; for ( v=0;v<MAX;v++ ){ if ( v<G->Nh || (NMAX<=v && v<NMAX+G->Ns) ){ if ( !collected[v] && dist[S-NMAX][v]<MinDist ){ MinDist = dist[S-NMAX][v]; MinV = v; } } } if ( MinDist==inf ){ return ERROR; } else { return MinV; } } void Dijkstra(Graph G, Vertex S, int dist[][MAX]) { int collected[MAX]; Vertex v, w; for ( v=0;v<MAX;v++ ){ if ( v<G->Nh || (NMAX<=v && v<NMAX+G->Ns) ){ dist[S-NMAX][v] = G->g[S][v]; collected[v] = 0; } } dist[S-NMAX][S] = 0; collected[S] = 0; while ( 1 ){ v = findMinDist(G, S, dist, collected); if ( v==ERROR ){ break; } collected[v] = 1; for ( w=0;w<MAX;w++ ){ if ( v<G->Nh || (NMAX<=v && v<NMAX+G->Ns) ){ if ( !collected[w] && G->g[v][w]<inf ){ if ( dist[S-NMAX][v]+G->g[v][w] < dist[S-NMAX][w] ){ dist[S-NMAX][w] = dist[S-NMAX][v]+G->g[v][w]; } } } } } return; } int maxDist(int dist[][MAX], int N, Vertex S) { int ret = 0; Vertex v; for ( v=0;v<N;v++ ){ if ( dist[S][v]>ret ){ ret = dist[S][v]; } } return ret; } int MinDist(int dist[][MAX], int N, Vertex S) { int ret = inf; Vertex v; for ( v=0;v<N;v++ ){ if ( dist[S][v]<ret ){ ret = dist[S][v]; } } return ret; } double average(int dist[][MAX], int N, Vertex S) { double ret = 0; Vertex v; for ( v=0;v<N;v++ ){ ret += dist[S][v]; } return ret/N; } int main() { int N, M, K, Ds; scanf("%d %d %d %d\n", &N, &M, &K, &Ds); Graph G = createGraph(N, M); int i; Edge E = (Edge)malloc(sizeof(struct ENode)); char str1[5], str2[5]; for ( i=0;i<K;i++ ){ scanf("%s %s %d\n", str1, str2, &E->weight); E->v = strToVertex(str1); E->w = strToVertex(str2); addEdge(G, E); } int dist[M][MAX]; Vertex v; for ( v=NMAX;v<NMAX+M;v++ ){ Dijkstra(G, v, dist); } Vertex index; int min = 0; double ave = inf; for ( v=0;v<M;v++ ){ if ( maxDist(dist, N, v)<=Ds ){ if ( MinDist(dist, N, v)>min ){ index = v; min = MinDist(dist, N, v); ave = average(dist, N, v); } else if ( MinDist(dist, N, v)==min ){ if (average(dist, N, v)<ave ){ index = v; ave = average(dist, N, v); } } } } if (ave==inf){ printf("No Solution"); } else{ printf("G%d\n", index+1); printf("%.1lf %.1lf", (double)min, round(10*ave)/10); } return 0; }
1131 Subway Map
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 10000 #define inf 10000 typedef int Vertex; typedef int Line; typedef struct ENode * Edge; struct ENode{ Vertex v, w; Line l; }; typedef struct Avjnode * PtrToAvj; struct Avjnode{ Vertex v; Line l; PtrToAvj next; }; typedef struct VNode{ Vertex v; PtrToAvj next; }AdjList[MAX]; typedef struct Mgraph * Graph; struct Mgraph{ AdjList G; int exist[MAX]; }; typedef struct QNode * PtrToQNode; struct QNode{ Vertex v; PtrToQNode next; }; typedef struct que * Queue; struct que{ PtrToQNode head, tail; }; Queue createQueue() { Queue Q = (Queue)malloc(sizeof(struct que)); Q->head = Q->tail = NULL; return Q; } int isEmpty(Queue Q) { return Q->head == NULL; } void addQ(Queue Q, Vertex v) { PtrToQNode p = (PtrToQNode)malloc(sizeof(struct QNode)); p->v = v; p->next = NULL; if ( isEmpty(Q) ){ Q->head = Q->tail = p; } else { Q->tail->next = p; Q->tail = p; } } Vertex deleteQ(Queue Q) { if (isEmpty(Q)){ return -1; } else { PtrToQNode p = Q->head; Q->head = p->next; Vertex v = p->v; free(p); return v; } } Graph createGraph(int N) { Graph G = (Graph)malloc(sizeof(struct Mgraph)); int i; for ( i=0;i<MAX;i++ ){ G->exist[i] = 0; } return G; } void addEdge(Graph G, Edge E) { PtrToAvj p; if ( G->exist[E->v]==0 ){ G->G[E->v].v =E->v; G->G[E->v].next = (PtrToAvj)malloc(sizeof(struct Avjnode)); G->G[E->v].next->v = E->w; G->G[E->v].next->l = E->l; G->G[E->v].next->next = NULL; G->exist[E->v] = 1; } else { p = (PtrToAvj)malloc(sizeof(struct Avjnode)); p->v = E->w; p->l = E->l; p->next = G->G[E->v].next; G->G[E->v].next = p; } if ( G->exist[E->w]==0 ){ G->G[E->w].v =E->w; G->G[E->w].next = (PtrToAvj)malloc(sizeof(struct Avjnode)); G->G[E->w].next->v = E->v; G->G[E->w].next->l = E->l; G->G[E->w].next->next = NULL; G->exist[E->w] = 1; } else { p = (PtrToAvj)malloc(sizeof(struct Avjnode)); p->v = E->v; p->l = E->l; p->next = G->G[E->w].next; G->G[E->w].next = p; } return; } Vertex strToVertex(char str[]) { Vertex v = 0; int i; for ( i=0;str[i];i++ ){ v = v*10 + (str[i]-'0'); } return v; } int transnum(Graph G, int path[], Vertex D) { int ret = 0; Vertex v, w; PtrToAvj p; Line l1, l2; v = path[D]; if ( path[v]==-1 ){ return ret; } p = G->G[v].next; while (p->v!=D){ p = p->next; } l1 = p->l; while ( 1 ){ w = path[v]; p = G->G[w].next; while (p->v!=v){ p = p->next; } l2 = p->l; if ( l2!=l1 ){ ret++; } if ( path[w]==-1 ){ break; } l1 = l2; v = w; } return ret; } void BFS(Graph G, int path[], int dist[], Vertex S) { int i, t, num; PtrToAvj p; for ( i=0;i<MAX;i++ ){ dist[i] = -1; } dist[S] = 0; path[S] = -1; Queue Q = createQueue(); addQ(Q, S); while ( !isEmpty(Q) ){ i = deleteQ(Q); if ( i==-1 ){ break; } for ( p=G->G[i].next;p;p=p->next ){ if ( dist[p->v]==-1 ){ addQ(Q, p->v); dist[p->v] = dist[i] + 1; path[p->v] = i; } else if ( dist[p->v] == dist[i] + 1 ){ num = transnum(G, path, p->v); t = path[p->v]; path[p->v] = i; if ( transnum(G, path, p->v) >= num ){ path[p->v] = t; } } } } return; } void printline(Graph G, int path[], Vertex S, Vertex D) { Line l1, l2; Vertex from, to; PtrToAvj p; to = D; from = path[to]; p = G->G[from].next; while ( p->v!=D ){ p = p->next; } l1 = p->l; if ( path[D]==S ){ printf("Take Line#%d from %04d to %04d.\n", l1, S, D); return; } while ( 1 ){ to = from; from = path[to]; p = G->G[from].next; while ( p->v!=to){ p = p->next; } l2 = p->l; if ( l1!=l2 ){ printline(G, path, S, to); printf("Take Line#%d from %04d to %04d.\n", l1, to, D); return; } else if (path[from]==-1){ printf("Take Line#%d from %04d to %04d.\n", l1, S, D); return; } } } int main() { int N, M; scanf("%d", &N); Graph G = createGraph(N); int i, j; char str1[5], str2[5]; Edge E = (Edge)malloc(sizeof(struct ENode)); for ( i=1;i<=N;i++ ){ scanf("%d %s", &M, str1); for ( j=1;j<M;j++ ){ scanf(" %s", str2); E->v = strToVertex(str1); E->w = strToVertex(str2); E->l = i; addEdge(G, E); strcpy(str1, str2); } } int K; Vertex S, D; scanf("%d", &K); int path[MAX], dist[MAX]; for ( i=0;i<K;i++ ){ scanf("\n%s %s", str1, str2); S = strToVertex(str1); D = strToVertex(str2); BFS(G, path, dist, S); printf("%d\n", dist[D]); printline(G, path, S, D); } return 0; }//本题测试点3存在问题,bfs函数中transnum与num的判断不加=就出 //错,加了就对,不清楚为什么。
改进:前述代码加上=虽能通过,但我分析后认为仍有bug:对如下测试存在错误(我认为题目所指的唯一解仅针对测试案例,而非针对所有站点,我一开始理解错了)
4 2 0 1 3 1 2 3 2 0 4 2 2 4 1 0 3
新代码:
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define MAX 10000 #define inf 10000 typedef int Vertex; typedef int Line; typedef struct ENode * Edge; struct ENode{ Line l; Vertex v, w; }; typedef struct AdjNode * PtrToAdj; struct AdjNode{ Vertex w; Line l; PtrToAdj next; }; typedef struct VNode{ bool existed; PtrToAdj next; }AdjList[MAX]; typedef struct LGraph* Graph; struct LGraph{ AdjList G; }; Graph CreateGraph() { Graph G = (Graph)malloc(sizeof(struct LGraph)); int i; for ( i=0;i<MAX;i++ ){ G->G[i].existed = false; G->G[i].next = NULL; } return G; } void AddEdge(Graph G, Edge E) { G->G[E->v].existed = G->G[E->w].existed = true; PtrToAdj p; p = (PtrToAdj)malloc(sizeof(struct AdjNode)); p->l = E->l; p->w = E->w; p->next = G->G[E->v].next; G->G[E->v].next = p; p = (PtrToAdj)malloc(sizeof(struct AdjNode)); p->l = E->l; p->w = E->v; p->next = G->G[E->w].next; G->G[E->w].next = p; } int transnum(Graph G, int path[], Vertex D) { int ret = 0; Vertex v, w; PtrToAdj p; Line l1, l2; v = path[D]; if ( path[v]==-1 ){ return ret; } p = G->G[v].next; while (p->w!=D){ p = p->next; } l1 = p->l; while ( 1 ){ w = path[v]; p = G->G[w].next; while (p->w!=v){ p = p->next; } l2 = p->l; if ( l2!=l1 ){ ret++; } if ( path[w]==-1 ){ break; } l1 = l2; v = w; } return ret; } void Initial(int dist[], int path[], Vertex S) { int i; for ( i=0;i<MAX;i++ ){ dist[i] = inf; path[i] = -1; } dist[S] = 0; } void FreshPath(int path[], int tmppath[]) { int i; for ( i=0;i<MAX;i++ ){ path[i] = tmppath[i]; } } void DFS(Graph G, int dist[], int path[], Vertex S, Vertex D) { PtrToAdj p; p = G->G[S].next; while (p){ if ( dist[S]+1 < dist[p->w] ){ dist[p->w] = dist[S] + 1; path[p->w] = S; if ( p->w!=D ){ DFS(G, dist, path, p->w, D); } } p = p->next; } } Vertex root(int path[], Vertex D) { while ( path[D]!=-1 ){ D = path[D]; } return D; } void strans(Graph G, int dist[], int path[], int tmpdist[], int tmppath[], Vertex S, Vertex D) { int sdist = dist[D], num = transnum(G, path, D); int i; PtrToAdj p = G->G[S].next; while (p){ if ( tmpdist[S]+1 <= tmpdist[p->w] ){ tmpdist[p->w] = tmpdist[S] + 1; tmppath[p->w] = S; if ( p->w!=D ){ strans(G, dist, path, tmpdist, tmppath, p->w, D); } else if ( tmpdist[D]==sdist ) { if ( transnum(G, tmppath, D)<num ){ num = transnum(G, tmppath, D); FreshPath(path, tmppath); } } } p = p->next; } } Graph BuildGraph() { Graph G = CreateGraph(); int N; scanf("%d", &N); int i, j; int M; Edge E = (Edge)malloc(sizeof(struct ENode)); for ( i=1;i<=N;i++ ){ E->l = i; scanf("%d", &M); scanf("%d", &E->v); for ( j=1;j<M;j++ ){ scanf("%d", &E->w); AddEdge(G, E); E->v = E->w; } } return G; } void printline(Graph G, int path[], Vertex S, Vertex D) { Line l1, l2; Vertex from, to; PtrToAdj p; to = D; from = path[to]; p = G->G[from].next; while ( p->w!=D ){ p = p->next; } l1 = p->l; if ( path[D]==S ){ printf("Take Line#%d from %04d to %04d.\n", l1, S, D); return; } while ( 1 ){ to = from; from = path[to]; p = G->G[from].next; while ( p->w!=to){ p = p->next; } l2 = p->l; if ( l1!=l2 ){ printline(G, path, S, to); printf("Take Line#%d from %04d to %04d.\n", l1, to, D); return; } else if (path[from]==-1){ printf("Take Line#%d from %04d to %04d.\n", l1, S, D); return; } } } int main() { Graph G = BuildGraph(); int dist[MAX], path[MAX]; int tmpdist[MAX], tmppath[MAX]; int i; int K, S, D; scanf("%d", &K); for ( i=0;i<K;i++ ){ scanf("%d %d", &S, &D); Initial(dist, path, S); DFS(G, dist, path, S, D); Initial(tmpdist, tmppath, S); strans(G, dist, path, tmpdist, tmppath, S, D); printf("%d\n", dist[D]); printline(G, path, S, D); } return 0; }
08-图7 公路村村通
#include <stdio.h> #include <stdlib.h> #define ERROR -1 #define inf 10000 #define MAX 1000 typedef int Vertex; typedef struct ENode * Edge; struct ENode{ Vertex v, w; int weight; }; typedef struct Mgraph * Graph; struct Mgraph{ int G[MAX][MAX]; int Nv; int Ne; }; Graph createGraph(int N, int M) { Graph G = (Graph)malloc(sizeof(struct Mgraph)); G->Nv = N; G->Ne = M; Vertex v, w; for ( v=0;v<N;v++ ){ for ( w=0;w<N;w++ ){ G->G[v][w] = inf; } } return G; } void addEdge(Graph G, Edge E) { G->G[E->v][E->w] = E->weight; G->G[E->w][E->v] = E->weight; return; } Vertex findMinDist(int dist[], int N) { int MinV, v; int MinDist = inf; for ( v=0;v<N;v++ ){ if ( dist[v]>0 && dist[v]<MinDist ){ MinV = v; MinDist = dist[v]; } } if ( MinDist==inf ){ return ERROR; } else { return MinV; } } int Prim(Graph G) { int dist[MAX], path[MAX], cnt=0, cost=0; Vertex v, w; for ( v=0;v<G->Nv;v++ ){ dist[v] = G->G[0][v]; if ( dist[v]<inf ){ path[v] = 0; } else{ path[v] = -1; } } dist[0] = 0; cnt++; while ( 1 ){ v = findMinDist(dist, G->Nv); if ( v==ERROR ){ break; } dist[v] = 0; cnt++; cost += G->G[path[v]][v]; for ( w=0;w<G->Nv;w++ ){ if ( G->G[v][w]<inf && G->G[v][w] < dist[w] ){ dist[w] = G->G[v][w]; path[w] = v; } } } if (cnt<G->Nv) { return ERROR; } else { return cost; } } int main() { int N, M; scanf("%d %d", &N, &M); Graph G = createGraph(N, M); Vertex v; Edge E = (Edge)malloc(sizeof(struct ENode)); for ( v=0;v<M;v++ ){ scanf("%d %d %d", &E->v, &E->w, &E->weight); E->v--; E->w--; addEdge(G, E); } printf("%d", Prim(G)); return 0; }
08-图8 How Long Does It Take
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define inf 10000 #define MAX 100 #define ElementType int #define ERROR -1 typedef int Position; struct QNode{ ElementType *Data; Position Front, Rear; int MaxSize; }; typedef struct QNode * Queue; Queue CreateQueue( int MaxSize ) { Queue q = (Queue)malloc(sizeof(struct QNode)); q->Data = (ElementType*)malloc(MaxSize * sizeof(ElementType)); q->Front = 0; q->Rear = 0; q->MaxSize = MaxSize; return q; } bool IsFull( Queue Q ) { return (Q->Rear+1)%Q->MaxSize == Q->Front; } bool AddQ( Queue Q, ElementType X ) { if ( !IsFull(Q) ){ Q->Rear = (Q->Rear+1)%Q->MaxSize; Q->Data[Q->Rear] = X; return true; } else { printf("队列满"); return false; } } bool IsEmpty( Queue Q ) { return Q->Rear == Q->Front; } ElementType DeleteQ( Queue Q ) { if ( !IsEmpty(Q) ){ Q->Front = (Q->Front+1)%Q->MaxSize; return Q->Data[Q->Front]; } else{ printf("队列空"); return ERROR; } } /****/ typedef struct ENode * Edge; struct ENode{ int v, w; int weight; }; typedef struct Mgraph * Graph; struct Mgraph{ int G[MAX][MAX]; int ECT[MAX]; int Indegree[MAX]; int Nv; int Ne; }; Graph createGraph(int N, int M) { Graph G = (Graph)malloc(sizeof(struct Mgraph)); G->Nv = N; G->Ne = M; int i, j; for ( i=0;i<N;i++ ){ G->ECT[i] = 0; G->Indegree[i] = 0; for ( j=0;j<N;j++ ){ G->G[i][j] = inf; } } return G; } void addEdge(Graph G, Edge E) { G->G[E->v][E->w] = E->weight; G->Indegree[E->w]++; return; } int BFS(Graph G) { Queue q = CreateQueue(G->Nv); int Indegree[MAX]; int i, j, cnt = 0; for ( i=0;i<G->Nv;i++ ){ Indegree[i] = G->Indegree[i]; } for ( i=0;i<G->Nv;i++ ){ if ( Indegree[i]==0 ){ AddQ(q, i); } } while ( !IsEmpty(q) ){ i = DeleteQ(q); cnt++; for ( j=0;j<G->Nv;j++ ){ if ( G->G[i][j]<inf ) { if ( --Indegree[j]==0 ){ AddQ(q, j); } if ( G->ECT[i]+G->G[i][j] > G->ECT[j] ){ G->ECT[j] = G->ECT[i]+G->G[i][j]; } } } } free(q); if (cnt!=G->Nv){ return ERROR; } return 1; } int main() { int N, M; scanf("%d %d", &N, &M); Graph G = createGraph(N, M); int i; Edge E = (Edge)malloc(sizeof(struct ENode)); for ( i=0;i<M;i++ ){ scanf("%d %d %d", &E->v, &E->w, &E->weight); addEdge(G, E); } if (BFS(G)==-1){ printf("Impossible\n"); } else { int MaxEct = 0; for ( i=0;i<G->Nv;i++ ){ if ( G->ECT[i]>MaxEct ){ MaxEct = G->ECT[i]; } } printf("%d\n", MaxEct); } return 0; }
08-图9 关键活动
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define inf 10000 #define MAX 100 #define ElementType int #define ERROR -1 typedef int Position; struct QNode{ ElementType *Data; Position Front, Rear; int MaxSize; }; typedef struct QNode * Queue; Queue CreateQueue( int MaxSize ) { Queue q = (Queue)malloc(sizeof(struct QNode)); q->Data = (ElementType*)malloc(MaxSize * sizeof(ElementType)); q->Front = 0; q->Rear = 0; q->MaxSize = MaxSize; return q; } bool IsFull( Queue Q ) { return (Q->Rear+1)%Q->MaxSize == Q->Front; } bool AddQ( Queue Q, ElementType X ) { if ( !IsFull(Q) ){ Q->Rear = (Q->Rear+1)%Q->MaxSize; Q->Data[Q->Rear] = X; return true; } else { printf("队列满"); return false; } } bool IsEmpty( Queue Q ) { return Q->Rear == Q->Front; } ElementType DeleteQ( Queue Q ) { if ( !IsEmpty(Q) ){ Q->Front = (Q->Front+1)%Q->MaxSize; return Q->Data[Q->Front]; } else{ printf("队列空"); return ERROR; } } /****/ typedef struct ENode * Edge; struct ENode{ int v, w; int weight; }; typedef struct Mgraph * Graph; struct Mgraph{ int G[MAX][MAX]; int ECT[MAX], LCT[MAX]; int Indegree[MAX], Outdegree[MAX]; int Nv; int Ne; }; Graph createGraph(int N, int M) { Graph G = (Graph)malloc(sizeof(struct Mgraph)); G->Nv = N; G->Ne = M; int i, j; for ( i=0;i<N;i++ ){ G->ECT[i] = 0; G->LCT[i] = inf; G->Indegree[i] = 0; G->Outdegree[i] = 0; for ( j=0;j<N;j++ ){ G->G[i][j] = inf; } } return G; } void addEdge(Graph G, Edge E) { G->G[E->v][E->w] = E->weight; G->Indegree[E->w]++; G->Outdegree[E->v]++; return; } int BFS(Graph G) { Queue q = CreateQueue(G->Nv); int Indegree[MAX]; int i, j, cnt = 0; for ( i=0;i<G->Nv;i++ ){ Indegree[i] = G->Indegree[i]; } for ( i=0;i<G->Nv;i++ ){ if ( Indegree[i]==0 ){ AddQ(q, i); } } while ( !IsEmpty(q) ){ i = DeleteQ(q); cnt++; for ( j=0;j<G->Nv;j++ ){ if ( G->G[i][j]<inf ) { if ( --Indegree[j]==0 ){ AddQ(q, j); } if ( G->ECT[i]+G->G[i][j] > G->ECT[j] ){ G->ECT[j] = G->ECT[i]+G->G[i][j]; } } } } free(q); if (cnt!=G->Nv){ return ERROR; } return 1; } void getLCT(Graph G) { int Outdegree[MAX]; Queue q = CreateQueue(G->Nv); int i, j; for ( i=0;i<G->Nv;i++ ){ Outdegree[i] = G->Outdegree[i]; } int MaxEct = 0; for ( i=0;i<G->Nv;i++ ){ if ( G->ECT[i]>MaxEct ){ MaxEct = G->ECT[i]; } } for ( i=0;i<G->Nv;i++ ){ if ( Outdegree[i]==0 ){ AddQ(q, i); G->LCT[i] = MaxEct; } } while ( !IsEmpty(q) ){ i = DeleteQ(q); for ( j=0;j<G->Nv;j++ ){ if ( G->G[j][i]<inf ){ if ( --Outdegree[j]==0 ){ AddQ(q, j); } if ( G->LCT[i]-G->G[j][i]<G->LCT[j] ){ G->LCT[j] = G->LCT[i]-G->G[j][i]; } } } } free(q); return; } void printKeyAct(Graph G, Edge E[]) { int i, j; for ( i=0;i<G->Nv;i++ ){ for ( j=G->Ne-1;j>-1;j-- ){ if ( E[j]->v==i && G->LCT[E[j]->w]-G->ECT[i] == E[j]->weight ){ printf("%d->%d\n", i+1, E[j]->w+1); } } } } int main() { int N, M; scanf("%d %d", &N, &M); Graph G = createGraph(N, M); int i; Edge E[M]; for ( i=0;i<M;i++ ){ E[i] = (Edge)malloc(sizeof(struct ENode)); scanf("%d %d %d", &E[i]->v, &E[i]->w, &E[i]->weight); E[i]->v--; E[i]->w--; addEdge(G, E[i]); } if (BFS(G)==-1){ printf("0\n"); } else { int MaxEct = 0; for ( i=0;i<G->Nv;i++ ){ if ( G->ECT[i]>MaxEct ){ MaxEct = G->ECT[i]; } } printf("%d\n", MaxEct); getLCT(G); printKeyAct(G, E); } return 0; }
09-排序1 排序
#include <stdio.h> #include <stdlib.h> #define ElementType long void swap(ElementType* a, ElementType* b) { ElementType t; t = *a; *a = *b; *b = t; } void BubbleSort(ElementType A[], int N) { int i, j, flag; for ( i=N-1;i>=0;i-- ){ flag = 0; for ( j=0;j<i;j++ ){ if ( A[j]>A[j+1] ){ swap(&A[j], &A[j+1]); flag = 1; } } if ( flag==0 ){ break; } } } void InsertionSort(ElementType A[], int N) { int i, j; ElementType tmp; for ( i=1;i<N;i++ ){ tmp = A[i]; for ( j=i;j>0 && A[j-1]>tmp;j-- ){ A[j] = A[j-1]; } A[j] = tmp; } } void ShellSort_original(ElementType A[], int N) { int D, i, j; ElementType tmp; for ( D=N/2;D>0;D/=2 ){ for ( i=D;i<N;i++ ){ tmp = A[i]; for ( j=i;j>=D && A[j-D]>tmp;j-=D ){ A[j] = A[j-D]; } A[j] = tmp; } } } void ShellSort(ElementType A[], int N) { int D, i, j, p; ElementType tmp; int Sedgewick[] = {929, 505, 209, 109, 41, 19, 5, 1, 0}; for ( i=0;Sedgewick[i]>N;i++ ) ; for ( D=Sedgewick[i];D>0;D=Sedgewick[++i] ){ for ( p=D;p<N;p++ ){ tmp = A[p]; for ( j=p;j>=D && A[j-D]>tmp;j-=D ){ A[j] = A[j-D]; } A[j] = tmp; } } } void PercDown(ElementType A[], int p, int N) { int Parent, Child; ElementType X; X = A[p]; for ( Parent=p;(Parent*2+1)<N;Parent=Child ){ Child = Parent*2+1; if ( Child!=N-1 && A[Child+1]>A[Child] ){ Child++; } if ( X>=A[Child] ){ break; } else { A[Parent] = A[Child]; } } A[Parent] = X; } void HeapSort(ElementType A[], int N) { int i; for ( i=N/2-1;i>=0;i-- ){ PercDown(A, i, N); } for ( i=N-1;i>0;i-- ){ swap(&A[0], &A[i]); PercDown(A, 0, i); } } void Merge(ElementType A[], ElementType TmpA[], int L, int R, int RightEnd) { /*R为右边起始位置,RightEnd为右边终点位置*/ int Lindex = L, Rindex = R, Tmpindex = L; while ( Lindex<R && Rindex<=RightEnd ){ if ( A[Lindex]<=A[Rindex] ){ TmpA[Tmpindex++] = A[Lindex++]; } else { TmpA[Tmpindex++] = A[Rindex++]; } } while ( Lindex<R ){ TmpA[Tmpindex++] = A[Lindex++]; } while ( Rindex<=RightEnd ){ TmpA[Tmpindex++] = A[Rindex++]; } int i, NumElements = RightEnd-L+1; for ( i=0;i<NumElements;i++, RightEnd-- ){ A[RightEnd] = TmpA[RightEnd]; } } void MSort(ElementType A[], ElementType TmpA[], int L, int RightEnd) { int center; if ( L<RightEnd ){ center = (L+RightEnd)/2; MSort(A, TmpA, L, center); MSort(A, TmpA, center+1, RightEnd); Merge(A, TmpA, L, center+1, RightEnd); } } void MergeSort(ElementType A[], int N) { ElementType* TmpA = (ElementType*)malloc(N*sizeof(ElementType)); if ( TmpA!=NULL ){ MSort(A, TmpA, 0, N-1); free(TmpA); } else { printf("空间不足"); } } void Merge_pass(ElementType A[], ElementType TmpA[], int N, int length) { int i; for ( i=0;i<=N-length*2;i+=length*2 ){ Merge(A, TmpA, i, i+length, i+length*2-1); } if ( i+length<N ){ Merge(A, TmpA, i, i+length, N-1); } else { while ( i<N ){ TmpA[i] = A[i]; i++; } } } void Merge_Sort(ElementType A[], int N) { ElementType* TmpA = (ElementType*)malloc(N*sizeof(ElementType)); int length=1; if ( TmpA!=NULL ){ while ( length<N ){ Merge_pass(A, TmpA, N, length); length *= 2; Merge_pass(TmpA, A, N, length); length *= 2; } free(TmpA); } else { printf("空间不足"); } } int main() { int N; scanf("%d", &N); int i; long A[N]; for ( i=0;i<N;i++ ){ scanf("%ld", &A[i]); } ShellSort_original(A, N); for ( i=0;i<N;i++ ){ if ( i!=0 ) { printf(" "); } printf("%ld", A[i]); } return 0; }
09-排序2 Insert or Merge
#include <stdio.h> #include <stdlib.h> #define ElementType int int isequal(int A[], int B[], int N) { int i, flag=1; for ( i=0;i<N;i++ ){ if ( A[i]!=B[i] ){ flag = 0; break; } } return flag; } void InsertionSort(ElementType A[], int B[], int N) { int i, j, k; int flag = 0; ElementType tmp; for ( i=1;i<N;i++ ){ tmp = A[i]; for ( j=i;j>0 && A[j-1]>tmp;j-- ){ A[j] = A[j-1]; } A[j] = tmp; if( flag==1 ){ printf("Insertion Sort\n"); for ( k=0;k<N;k++ ){ if ( k!=0 ) printf(" "); printf("%d", A[k]); } break; } if ( isequal(A, B, N)==1 ){ flag = 1; } } } void Merge(ElementType A[], ElementType TmpA[], int L, int R, int RightEnd) { /*R为右边起始位置,RightEnd为右边终点位置*/ int Lindex = L, Rindex = R, Tmpindex = L; while ( Lindex<R && Rindex<=RightEnd ){ if ( A[Lindex]<=A[Rindex] ){ TmpA[Tmpindex++] = A[Lindex++]; } else { TmpA[Tmpindex++] = A[Rindex++]; } } while ( Lindex<R ){ TmpA[Tmpindex++] = A[Lindex++]; } while ( Rindex<=RightEnd ){ TmpA[Tmpindex++] = A[Rindex++]; } int i, NumElements = RightEnd-L+1; for ( i=0;i<NumElements;i++, RightEnd-- ){ A[RightEnd] = TmpA[RightEnd]; } } void Merge_pass(ElementType A[], ElementType TmpA[], int N, int length) { int i; for ( i=0;i<=N-length*2;i+=length*2 ){ Merge(A, TmpA, i, i+length, i+length*2-1); } if ( i+length<N ){ Merge(A, TmpA, i, i+length, N-1); } else { while ( i<N ){ TmpA[i] = A[i]; i++; } } } int Merge_Sort(ElementType A[], int B[], int N) { ElementType* TmpA = (ElementType*)malloc(N*sizeof(ElementType)); int length=1; int i, flag=0; if ( TmpA!=NULL ){ while ( length<N ){ Merge_pass(A, TmpA, N, length); if( flag==1 ){ printf("Merge Sort\n"); for ( i=0;i<N;i++ ){ if ( i!=0 ) printf(" "); printf("%d", TmpA[i]); } return flag; } if ( isequal(TmpA, B, N)==1 ){ flag = 1; } length *= 2; Merge_pass(TmpA, A, N, length); if( flag==1 ){ printf("Merge Sort\n"); for ( i=0;i<N;i++ ){ if ( i!=0 ) printf(" "); printf("%d", TmpA[i]); } return flag; } if ( isequal(TmpA, B, N) ){ flag = 1; } length *= 2; } free(TmpA); } else { printf("空间不足"); } return flag; } int main() { int N; scanf("%d", &N); int i; int sr[N], pss[N], sr1[N]; for ( i=0;i<N;i++ ){ scanf("%d", &sr[i]); sr1[i] = sr[i]; } for ( i=0;i<N;i++ ){ scanf("%d", &pss[i]); } if( Merge_Sort(sr1, pss, N)==0 ){ InsertionSort(sr, pss, N); }; return 0; }
09-排序3 Insertion or Heap Sort
#include <stdio.h> #include <stdlib.h> #define ElementType int void swap(ElementType* a, ElementType* b) { ElementType t; t = *a; *a = *b; *b = t; } int isequal(int A[], int B[], int N) { int i, flag=1; for ( i=0;i<N;i++ ){ if ( A[i]!=B[i] ){ flag = 0; break; } } return flag; } void InsertionSort(ElementType A[], int B[], int N) { int i, j, k; int flag = 0; ElementType tmp; for ( i=1;i<N;i++ ){ tmp = A[i]; for ( j=i;j>0 && A[j-1]>tmp;j-- ){ A[j] = A[j-1]; } A[j] = tmp; if( flag==1 ){ printf("Insertion Sort\n"); for ( k=0;k<N;k++ ){ if ( k!=0 ) printf(" "); printf("%d", A[k]); } break; } if ( isequal(A, B, N)==1 ){ flag = 1; } } } void PercDown(ElementType A[], int p, int N) { int Parent, Child; ElementType X; X = A[p]; for ( Parent=p;(Parent*2+1)<N;Parent=Child ){ Child = Parent*2+1; if ( Child!=N-1 && A[Child+1]>A[Child] ){ Child++; } if ( X>=A[Child] ){ break; } else { A[Parent] = A[Child]; } } A[Parent] = X; } int HeapSort(ElementType A[], int B[], int N) { int i, j; int flag = 0; for ( i=N/2-1;i>=0;i-- ){ PercDown(A, i, N); } for ( i=N-1;i>0;i-- ){ swap(&A[0], &A[i]); PercDown(A, 0, i); if ( flag==1 ){ printf("Heap Sort\n"); for ( j=0;j<N;j++ ){ if( j!=0 ) printf(" "); printf("%d", A[j]); } return flag; } if ( isequal(A, B, N) ){ flag = 1; } } return flag; } int main() { int N; scanf("%d", &N); int i; int sr[N], pss[N], sr1[N]; for ( i=0;i<N;i++ ){ scanf("%d", &sr[i]); sr1[i] = sr[i]; } for ( i=0;i<N;i++ ){ scanf("%d", &pss[i]); } if( HeapSort(sr1, pss, N)==0 ){ InsertionSort(sr, pss, N); }; return 0; }
10-排序4 统计工龄
#include <stdio.h> #include <stdlib.h> #define ElementType int void swap(ElementType* a, ElementType* b) { ElementType t; t = *a; *a = *b; *b = t; } void InsertionSort(ElementType A[], int N) { int i, j; ElementType tmp; for ( i=1;i<N;i++ ){ tmp = A[i]; for ( j=i;j>0 && A[j-1]>tmp;j-- ){ A[j] = A[j-1]; } A[j] = tmp; } } ElementType Median3(ElementType A[], int Left, int Right) { /*返回主元*/ int Center = (Left+Right)/2; if ( A[Left]>A[Center] ){ swap(&A[Left], &A[Center]); } if ( A[Left]>A[Right] ){ swap(&A[Left], &A[Right]); } if ( A[Center]>A[Right] ){ swap(&A[Center], &A[Right]); } swap(&A[Center], &A[Right-1]); return A[Right-1]; } void Quick_sort(ElementType A[], int Left, int Right) { int Cutoff = 100; if ( Cutoff<=Right-Left+1 ){ ElementType Pivot = Median3(A, Left, Right); int i = Left, j = Right-1; for (;;){ while (A[++i]<Pivot){ } while (A[--j]>Pivot){ } if ( i<j ){ swap(&A[i], &A[j]); } else { break; } } swap(&A[i], &A[Right-1]); Quick_sort(A, Left, i-1); Quick_sort(A, i+1, Right); } else { InsertionSort(A+Left, Right-Left+1); } } void QuickSort(ElementType A[], int N) { Quick_sort(A, 0, N-1); } int main() { int N; scanf("%d", &N); int A[N]; int i; for ( i=0;i<N;i++ ){ scanf("%d", &A[i]); } QuickSort(A, N); int age = A[0], num = 0; for ( i=0;i<N;i++ ){ if ( A[i]!=age ){ printf("%d:%d\n", age, num); age = A[i]; num = 0; } if ( A[i]==age ){ num++; } } printf("%d:%d\n", age, num); return 0; }
10-排序5 PAT Judge
#include <stdio.h> #include <stdlib.h> #define NMAX 10000 #define KMAX 5 int main() { int N, K, M; scanf("%d %d %d", &N, &K, &M); int fullMark[K]; int i, j; for ( i=0;i<K;i++ ){ scanf("%d", &fullMark[i]); } int score[N][K], sign[N][K]; for ( i=0;i<N;i++ ){ for ( j=0;j<K;j++ ){ sign[i][j] = 0; } } int userID, problemID, tmp; for ( i=0;i<M;i++ ){ scanf("%d %d", &userID, &problemID); userID--; problemID--; scanf("%d", &tmp); if ( !(sign[userID][problemID]==1 && tmp<score[userID][problemID]) ){ score[userID][problemID] = tmp; } if (score[userID][problemID]!=-1){ sign[userID][problemID] = 1; } else { sign[userID][problemID] = -1; } } int table[N], flag, rankNum=0; for ( i=0;i<N;i++ ){ flag = 0; for ( j=0;j<K;j++ ){ if (sign[i][j]==1){ flag = 1; break; } } if (flag){ table[rankNum++] = i; } } int rank[N][3]; for ( i=0;i<rankNum;i++ ){ rank[table[i]][0] = rank[table[i]][1] = 0; for ( j=0;j<K;j++ ){ if (sign[table[i]][j]==1){ rank[table[i]][0] += score[table[i]][j]; if ( score[table[i]][j]==fullMark[j] ){ rank[table[i]][1]++; } } } } for ( i=1;i<rankNum;i++ ){ tmp = table[i]; for ( j=i;j>0;j-- ){ if ( rank[tmp][0]>rank[table[j-1]][0]){ table[j] = table[j-1]; } else if ( rank[tmp][0]==rank[table[j-1]][0] && rank[tmp][1]>rank[table[j-1]][1] ){ table[j] = table[j-1]; } else{ break; } } table[j] = tmp; } rank[table[0]][2] = 1; for ( i=1;i<rankNum;i++ ){ if ( rank[table[i]][0]==rank[table[i-1]][0] ){ rank[table[i]][2] = rank[table[i-1]][2]; } else { rank[table[i]][2] = i+1; } } for ( i=0;i<rankNum;i++ ){ printf("%d %05d %d", rank[table[i]][2], table[i]+1, rank[table[i]][0]); for ( j=0;j<K;j++ ){ if ( sign[table[i]][j]==1 ){ printf(" %d", score[table[i]][j]); } else if ( sign[table[i]][j]==-1 ){ printf(" 0"); } else{ printf(" -"); } } printf("\n"); } return 0; }
10-排序6 Sort with Swap(0, i)
#include <stdio.h> int tableSort(int seq[], int N) { int i, j, k, cnt=0; if ( seq[0]!=0 ){ cnt-=2; } for ( i=0;i<N;i++ ){ if ( seq[i]!=i ){ j = i; k = -1; while ( k!=i ){ k = seq[j]; seq[j] = j; cnt++; j = k; } cnt++; } } return cnt; } int main() { int N, i; scanf("%d", &N); int seq[N]; for ( i=0;i<N;i++ ){ scanf("%d", &seq[i]); } printf("%d", tableSort(seq, N)); return 0; }
11-散列1 电话聊天狂人
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <math.h> #include <string.h> #define KEYLENGTH 11 #define MAXTABLESIZE 1000000 typedef char ElementType[KEYLENGTH+1]; typedef int Index; typedef struct LNode *PtrToLNode; struct LNode{ int count; ElementType Data; PtrToLNode Next; }; typedef PtrToLNode Position; typedef PtrToLNode List; typedef struct TblNode * HashTable; struct TblNode{ int TableSize; List Heads; }; int NextPrime(int N) { int i, p = (N%2) ? N+2 : N+1; while ( p<=MAXTABLESIZE ) { for ( i=(int)sqrt(p);i>2;i-- ){ if ( !(p%i) ) break; } if ( i==2 ) break; else p+=2; } return p; } HashTable CreateTable(int TableSize) { HashTable H; int i; H = (HashTable)malloc(sizeof(struct TblNode)); H->TableSize = NextPrime(TableSize); H->Heads = (List)malloc(H->TableSize*sizeof(struct LNode)); for ( i=0;i<H->TableSize;i++ ){ H->Heads[i].count = 0; H->Heads[i].Data[0] = '\0'; H->Heads[i].Next = NULL; } return H; } Index Hash(ElementType Key, int TableSize) { Index res=0; Key = Key + 6; while (*Key){ res = res*10 + (*Key++ -'0'); } return res%TableSize; } Position Find(HashTable H, ElementType Key) { Position P; Index Pos; Pos = Hash(Key, H->TableSize); P = H->Heads[Pos].Next; while( P&&strcmp(P->Data, Key) ){ P = P->Next; } return P; } Position Insert(HashTable H, ElementType Key) { Position P, NewCell; Index Pos; P = Find(H, Key); if ( !P ){ NewCell = (Position)malloc(sizeof(struct LNode)); NewCell->count = 1; strcpy(NewCell->Data, Key); Pos = Hash(Key, H->TableSize); NewCell->Next = H->Heads[Pos].Next; H->Heads[Pos].Next = NewCell; return NewCell; } else{ P->count++; return P; } } void DestroyTable(HashTable H) { int i; Position P, Tmp; for ( i=0;i<H->TableSize;i++ ){ P = H->Heads[i].Next; while (P){ Tmp = P->Next; free(P); P = Tmp; } } free(H->Heads); free(H); } int main() { int N; scanf("%d", &N); HashTable H = CreateTable(2*N); char Phone[12]; char Most[12] = "\0"; int PhoneCount = 0, ManCount = 1; int i; Position P; for ( i=0;i<2*N;i++ ){ scanf("%s", Phone); P = Insert(H, Phone); if ( P->count > PhoneCount ){ strcpy(Most, P->Data); PhoneCount = P->count; ManCount = 1; } else if ( P->count==PhoneCount ){ ManCount++; if ( strcmp(P->Data, Most)<0 ){ strcpy(Most, P->Data); } } } printf("%s %d", Most, PhoneCount); if ( ManCount!=1 ){ printf(" %d", ManCount); } printf("\n"); DestroyTable(H); return 0; }
11-散列2 Hashing
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <math.h> #define MAXTABLESIZE 100000 typedef int ElementType; typedef int Index; typedef Index Position; typedef enum{Legitimate, Empty, Deleted} EntryType; typedef struct HashEntry Cell; struct HashEntry{ ElementType Data; EntryType Info; }; typedef struct TblNode * HashTable; struct TblNode{ int TableSize; Cell * Cells; }; int NextPrime(int N) { if(N<=2) return 2; if(N==3) return 3; int i, p = (N%2) ? N : N+1; while ( p<=MAXTABLESIZE ){ for ( i=(int)sqrt(p);i>2;i-- ){ if ( !(p%i) ) break; } if ( i==2 ) break; else p+=2; } return p; } HashTable CreateTable(int TableSize) { HashTable H; int i; H = (HashTable)malloc(sizeof(struct TblNode)); H->TableSize = NextPrime(TableSize); H->Cells = (Cell*)malloc(H->TableSize*sizeof(Cell)); for ( i=0;i<H->TableSize;i++ ){ H->Cells[i].Info = Empty; } return H; } Position Hash(ElementType Key, int TableSize) { return Key%TableSize; } Position Find(HashTable H, ElementType Key) { Position CurrentPos, NewPos; int CNum=0; NewPos = CurrentPos = Hash(Key, H->TableSize); while ( H->Cells[NewPos].Info!=Empty && H->Cells[NewPos].Data!=Key && CNum<H->TableSize-1 ){ CNum++; NewPos = CurrentPos + CNum * CNum; if ( NewPos>=H->TableSize ){ NewPos = NewPos%H->TableSize; } } if ( CNum==H->TableSize-1 ){ return -1; } else { return NewPos; } } bool Insert(HashTable H, ElementType Key) { Position Pos = Find(H, Key); if ( Pos==-1 ){ return false; } else if (H->Cells[Pos].Info != Legitimate ){ H->Cells[Pos].Info = Legitimate; H->Cells[Pos].Data = Key; return true; } else { return false; } } int main() { int MSize, N; scanf("%d %d", &MSize, &N); HashTable H = CreateTable(MSize); int Key[N]; int i; for ( i=0;i<N;i++ ){ scanf("%d", &Key[i]); Insert(H, Key[i]); } Position p; for ( i=0;i<N;i++ ){ if (i){ printf(" "); } p = Find(H, Key[i]); if ( p==-1 ){ printf("-"); } else { printf("%d", p); } } return 0; }
11-散列3 QQ帐户的申请与登陆
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <stdbool.h> #define MAXTABLESIZE 1e6 #define IDLENGTH 10 #define PWLENGTH 16 typedef char QIDType[IDLENGTH+1]; typedef char PWType[PWLENGTH+1]; typedef int Index; typedef struct LNode * PtrToLNode; typedef PtrToLNode List; typedef PtrToLNode Position; struct LNode{ QIDType ID; PWType PW; PtrToLNode Next; }; typedef struct TblNode * HashTable; struct TblNode{ int TableSize; List heads; }; int NextPrime(int N) { int i, p = (N%2) ? N+2 : N+1; while ( p<MAXTABLESIZE ){ for ( i=(int)sqrt(p);i>2;i-- ){ if ( !(p%i) ) break; } if ( i==2 ) break; else p += 2; } return p; } HashTable CreateTable(int TableSize) { HashTable H; int i; H = (HashTable)malloc(sizeof(struct TblNode)); H->TableSize = NextPrime(TableSize); H->heads = (List)malloc(H->TableSize*sizeof(struct LNode)); for ( i=0;i<H->TableSize;i++ ){ H->heads[i].ID[0] = '\0'; H->heads[i].PW[0] = '\0'; H->heads[i].Next = NULL; } return H; } Index Hash(QIDType Key, int TableSize) { Index res = 0; int l = strlen(Key); if ( l>6 ){ Key = Key + l - 6; } while ( *Key ){ res = res*10 + (*Key++ - '0'); } return res%TableSize; } Position Find(HashTable H, QIDType Key) { Position P; Index Pos; Pos = Hash(Key, H->TableSize); P = H->heads[Pos].Next; while ( P && strcmp(P->ID, Key)){ P = P->Next; } return P; } bool Insert(HashTable H, QIDType Key, PWType Pw) { Position P, NewCell; Index Pos; P = Find(H, Key); if ( !P ){ NewCell = (Position)malloc(sizeof(struct LNode)); strcpy(NewCell->ID, Key); strcpy(NewCell->PW, Pw); Pos = Hash(Key, H->TableSize); NewCell->Next = H->heads[Pos].Next; H->heads[Pos].Next = NewCell; return true; } else { return false; } } void DestroyTable(HashTable H) { Position P, Tmp; int i; for ( i=0;i<H->TableSize;i++ ){ P = H->heads[i].Next; while (P) { Tmp = P; P = P->Next; free(Tmp); } } free(H->heads); free(H); } void SignUp(HashTable H, QIDType Key, PWType Pw) { int l = strlen(Key); if ( l<4 ){ return; } bool isSuc = Insert(H, Key, Pw); if ( isSuc ){ printf("New: OK\n"); } else { printf("ERROR: Exist\n"); } } void SignIn(HashTable H, QIDType Key, PWType Pw) { Position P = Find(H, Key); if ( !P ){ printf("ERROR: Not Exist\n"); } else if(strcmp(Pw, P->PW)){ printf("ERROR: Wrong PW\n"); } else { printf("Login: OK\n"); } } int main() { int N; scanf("%d", &N); HashTable H = CreateTable(N); char sign; QIDType id; PWType pw; int i; for ( i=0;i<N;i++ ){ scanf("\n%c %s %s", &sign, id, pw); if ( sign=='N'){ SignUp(H, id, pw); } if ( sign=='L'){ SignIn(H, id, pw); } } DestroyTable(H); return 0; }
11-散列4 Hashing - Hard Version
#include <stdio.h> #include <stdlib.h> #include <math.h> #define inf 0xFFFF #define ERROR -1 typedef int Index; typedef enum{Empty, Can, Existed, No} EntryType; typedef struct HashEntry Cell; struct HashEntry{ int Data; EntryType Info; }; typedef struct TblNode * HashTable; struct TblNode{ int TableSize; Cell* Cells; }; HashTable CreateTable(int TableSize) { HashTable H; H = (HashTable)malloc(sizeof(struct TblNode)); H->TableSize = TableSize; H->Cells = (Cell*)malloc(H->TableSize*sizeof(struct HashEntry)); return H; } Index Hash(int Key, int TableSize) { return Key%TableSize; } void InitialTable(HashTable H) { int i; int v; for ( i=0;i<H->TableSize;i++ ){ scanf("%d", &v); H->Cells[i].Data = v; if ( v==-1 ){ H->Cells[i].Info = Empty; } else if ( Hash(v, H->TableSize)==i ){ H->Cells[i].Info = Can; } else { H->Cells[i].Info = No; } } } Index GetMin(HashTable H) { int Minv, v = inf; int i; for ( i=0;i<H->TableSize;i++ ){ if ( H->Cells[i].Info==Can && H->Cells[i].Data<v ){ Minv = i; v = H->Cells[i].Data; } } if ( v==inf ){ return ERROR; } else { return Minv; } } void RefreshInfo(HashTable H) { int i, j, k; int flag; for ( i=0;i<H->TableSize;i++ ){ if ( H->Cells[i].Info==No ){ flag = 1; j = Hash(H->Cells[i].Data, H->TableSize); for ( k=0;(j+k)%H->TableSize!=i;k++ ){ if ( H->Cells[(j+k)%H->TableSize].Info!=Existed ){ flag = 0; break; } } if ( flag ){ H->Cells[i].Info = Can; } } } } void Out(HashTable H) { Index Pos = GetMin(H); int flag=1; while ( Pos!=ERROR ){ if( flag ){ flag = 0; } else { printf(" "); } H->Cells[Pos].Info = Existed; RefreshInfo(H); printf("%d", H->Cells[Pos].Data); Pos = GetMin(H); } } int main() { int N; scanf("%d", &N); HashTable H = CreateTable(N); InitialTable(H); Out(H); }
KMP 串的模式匹配
#include <stdio.h> #include <stdlib.h> #include <string.h> #define StringSize 1000000 #define PatternSize 100000 typedef int Position; #define NotFound -1 void BuildMatch(char *pattern, int *match) { int i, j, l=strlen(pattern); match[0] = -1; for ( j=1;j<l;j++ ){ i = match[j-1]; while ( pattern[i+1]!=pattern[j] && i>=0 ){ i = match[i]; } if( pattern[i+1]==pattern[j] ){ match[j] = i+1; } else { match[j] = -1; } } } Position KMP(char *string, char *pattern) { int m = strlen(string); int n = strlen(pattern); int s, p, *match; if ( m<n ){ return NotFound; } match = (int*)malloc(n*sizeof(int)); BuildMatch(pattern, match); s = p = 0; while ( s<m && p<n){ if ( string[s]==pattern[p] ){ s++; p++; } else if ( p>0 ){ p = match[p-1] + 1; } else { s++; } } if ( p==n ){ return s-p; } else { return NotFound; } } int main() { char string[StringSize+1]; scanf("%s", string); int N; scanf("%d", &N); int i; char pattern[PatternSize+1]; Position res; for ( i=0;i<N;i++ ){ scanf("%s", pattern); res = KMP(string, pattern); if( res!=NotFound ){ printf("%s\n", string+res); } else { printf("Not Found\n"); } } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南