多项式的加法和乘法计算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <stdio.h>
#include <stdlib.h>

struct Node;
struct LinkList;
typedef struct Node *Poly;
typedef struct LinkList *PLinkList;

struct Node {
int exp;
int coef;
Poly link;
};

struct LinkList {
Poly head;
};

void add(PLinkList A, PLinkList B);
PLinkList mul(PLinkList A, PLinkList B);
void printPolynomial(PLinkList A);

int main() {
PLinkList A = NULL, B = NULL;
A = (PLinkList)malloc(sizeof(LinkList));
B = (PLinkList)malloc(sizeof(LinkList));
A->head = NULL;
B->head = NULL;
if (A == NULL || B == NULL)
printf("err!");
Poly p = NULL, pr = NULL;
while (1) {
p = (Poly)malloc(sizeof(Node));
if (p == NULL)
printf("err!");
scanf("%d %d", &p->coef, &p->exp);
if (p->coef == 0 && p->exp == 0)
break;
if (pr == NULL) {
A->head = p;
} else {
pr->link = p;
}
pr = p;
}
if (pr != NULL)
pr->link = NULL;
Poly pB = NULL, prB = NULL;
while (1) {
pB = (Poly)malloc(sizeof(Node));
if (pB == NULL)
printf("err!");
scanf("%d %d", &pB->coef, &pB->exp);
if (pB->coef == 0 && pB->exp == 0)
break;
if (prB == NULL) {
B->head = pB;
} else {
prB->link = pB;
}
prB = pB;
}
if (prB != NULL)
prB->link = NULL;

PLinkList C = NULL, D = NULL;
C = mul(A, B);
add(A, B);
printPolynomial(A);
printf("\n");
printPolynomial(C);
}

void add(PLinkList A, PLinkList B) {
Poly pA = A->head, prA = A->head, pB = B->head, prB = B->head;
while (pA != NULL && pB != NULL) {
if (pA->exp == pB->exp) {
pA->coef += pB->coef;
pB = pB->link;
continue;
}
if (pA->exp < pB->exp) {
if (pA != A->head) {
prA->link = pB;
prB = pB->link;
pB->link = pA;
pA = pB;
pB = prB;
} else {
A->head = pB;
prB = pB->link;
pB->link = pA;
pA = pB;
prA = pB;
pB = prB;
}
continue;
}
if (pA->exp > pB->exp) {
if (prA != pA)
prA = prA->link;
pA = pA->link;
continue;
}
}
if (pB != NULL) {
if (prA != NULL)
prA->link = pB;
else {
A->head = pB;
}
}

}

PLinkList mul(PLinkList A, PLinkList B) {
Poly pB = B->head;
PLinkList D = NULL;
D = (PLinkList)malloc(sizeof(LinkList));
D->head = NULL;
while (pB != NULL) {
PLinkList C = NULL;
C = (PLinkList)malloc(sizeof(LinkList));
C->head = NULL;
Poly pA = A->head, pC = NULL, prC = NULL;
while (pA != NULL) {
pC = (Poly)malloc(sizeof(Node));
pC->coef = pA->coef * pB->coef;
pC->exp = pA->exp + pB->exp;
if (prC == NULL)
C->head = pC;
else {
prC->link = pC;
}
prC = pC;
pA = pA->link;
}
if (pC != NULL)
pC->link = NULL;
pB = pB->link;
add(D, C);
}

return D;

}

void printPolynomial(PLinkList A) {
int t = 0, c = 0;
Poly p = A->head;
while (p != NULL) {
if (p->coef != 0 && p->coef != 1 && p->coef != -1 && p->exp != 0 && p->exp != 1) {
if (t == 0) {
printf("%dx^%d", p->coef, p->exp);
t++;
c++;
} else {
if (p->coef > 0) {
printf("+%dx^%d", p->coef, p->exp);
c++;
} else {
printf("%dx^%d", p->coef, p->exp);
c++;
}
}
}
if (p->coef != 0 && p->exp == 0) {
if (t == 0) {
printf("%d", p->coef);
t++;
c++;
} else {
if (p->coef > 0) {
printf("+%d", p->coef);
c++;
} else {
printf("%d", p->coef);
c++;
}
}
}
if (p->coef != 0 && p->coef != 1 && p->coef != -1 && p->exp == 1) {
if (t == 0) {
printf("%dx", p->coef);
t++;
c++;
} else {
if (p->coef > 0) {
printf("+%dx", p->coef);
c++;
} else {
printf("%dx", p->coef);
c++;
}
}
}
if ((p->coef == 1 || p->coef == -1) && p->exp != 0 && p->exp != 1) {
if (t == 0) {
if (p->coef > 0)
printf("x^%d", p->exp);
else
printf("-x^%d", p->exp);
t++;
c++;
} else {
if (p->coef > 0) {
printf("+x^%d", p->exp);
c++;
} else {
printf("-x^%d", p->exp);
c++;
}
}
}
if ((p->coef == 1 || p->coef == -1) && p->exp == 1) {
if (t == 0) {
if (p->coef > 0)
printf("x", p->exp);
else
printf("-x", p->exp);
t++;
c++;
} else {
if (p->coef > 0) {
printf("+x");
c++;
} else {
printf("-x");
c++;
}
}
}
p = p->link;
}
if (c == 0)
printf("0");
}

拼写检查

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Node;
struct LinkList;
typedef struct Node *PNode;
typedef struct LinkList *PLinkList;

struct Node {
char word[20];
PNode link;
};

struct LinkList {
PNode head;
};
PLinkList dictionary = NULL;

int compare(char *a, char *b);
char *remove(char *word, int i);
char *change(char *word, char a, int i);
PLinkList selectSimWords(char *word);
PLinkList findCorrectWords(PLinkList simwords, char *word);
void printCorrectWords(PLinkList correctwords, char *word);

int main() {
dictionary = (PLinkList)malloc(sizeof(LinkList));
if (dictionary == NULL)
printf("err!");
dictionary->head = NULL;
PNode p1 = NULL, pr1 = NULL;
while (1) {
p1 = (PNode)malloc(sizeof(Node));
if (p1 == NULL)
printf("err!");
scanf("%s", p1->word);
if (strcmp(p1->word, "#") == 0)
break;
if (dictionary->head == NULL)
dictionary->head = p1;
else
pr1->link = p1;
pr1 = p1;
}
pr1->link = NULL;

PLinkList sample = NULL;
sample = (PLinkList)malloc(sizeof(LinkList));
if (sample == NULL)
printf("err!");
sample->head = NULL;
PNode p2 = NULL, pr2 = NULL;
while (1) {
p2 = (PNode)malloc(sizeof(Node));
if (p2 == NULL)
printf("err!");
scanf("%s", p2->word);
if (strcmp(p2->word, "#") == 0)
break;
if (sample->head == NULL)
sample->head = p2;
else
pr2->link = p2;
pr2 = p2;
}
pr2->link = NULL;

PNode p3 = sample->head;
while (p3 != NULL) {
if (p3 != sample->head)
printf("\n");
printCorrectWords(findCorrectWords(selectSimWords(p3->word), p3->word), p3->word);
p3 = p3->link;
}

return 0;
}

int compare(char *a, char *b) {
int i = 0, k = strlen(b);
while (b[i] != '\0') {
if (a[i] != b[i]) {
k = i;
break;
}
i++;
}
return k;
}

char *remove(char *word, int i) {
static char newword[20];
int j = 0;
while (j < i) {
newword[j] = word[j];
j++;
}
while (word[j + 1] != '\0') {
newword[j] = word[j + 1];
j++;
}
newword[j] = '\0';
return newword;
}

char *change(char *word, char a, int i) {
static char newword[20];
int j = 0;
while (j < i) {
newword[j] = word[j];
j++;
}
newword[j] = a;
j++;
while (word[j] != '\0') {
newword[j] = word[j];
j++;
}
newword[j] = '\0';

return newword;
}

PLinkList selectSimWords(char *word) {
int l = strlen(word), lw;
PLinkList simwords = NULL;
simwords = (PLinkList)malloc(sizeof(LinkList));
if (simwords == NULL)
printf("err!");
simwords->head = NULL;
PNode p = dictionary->head, p2 = NULL, pr2 = NULL;
while (p != NULL) {
p2 = (PNode)malloc(sizeof(Node));
if (p2 == NULL)
printf("err!");
lw = strlen(p->word);
if (lw - l >= -1 && lw - l <= 1) {
;
strcpy(p2->word, p->word);
if (simwords->head == NULL)
simwords->head = p2;
else
pr2->link = p2;
pr2 = p2;
}
p = p->link;
}
pr2->link = NULL;
return simwords;
}

PLinkList findCorrectWords(PLinkList simwords, char *word) {
PLinkList correctwords = NULL;
correctwords = (PLinkList)malloc(sizeof(LinkList));
if (correctwords == NULL)
printf("err!");
correctwords->head = NULL;
PNode p = simwords->head, p2 = NULL, pr2 = NULL;
int deltal, index;
while (p != NULL) {
deltal = strlen(p->word) - strlen(word);
if (deltal == 1) {
char changedword[20];
index = compare(p->word, word);
strcpy(changedword, remove(p->word, index));
if (strcmp(changedword, word) == 0) {
p2 = (PNode)malloc(sizeof(Node));
if (p2 == NULL)
printf("err!");
strcpy(p2->word, p->word);
if (correctwords->head == NULL)
correctwords->head = p2;
else
pr2->link = p2;
pr2 = p2;
}
} else if (deltal == -1) {
char changedword[20];
index = compare(word, p->word);
strcpy(changedword, remove(word, index));
if (strcmp(changedword, p->word) == 0) {
p2 = (PNode)malloc(sizeof(Node));
if (p2 == NULL)
printf("err!");
strcpy(p2->word, p->word);
if (correctwords->head == NULL)
correctwords->head = p2;
else
pr2->link = p2;
pr2 = p2;
}
} else if (deltal == 0) {
if (strcmp(p->word, word) == 0) {
correctwords->head = (PNode)malloc(sizeof(Node));
strcpy(correctwords->head->word, word);
correctwords->head->link = NULL;
return correctwords;
} else {
char changedword[20];
index = compare(p->word, word);
strcpy(changedword, change(word, p->word[index], index));
if (strcmp(changedword, p->word) == 0) {
p2 = (PNode)malloc(sizeof(Node));
if (p2 == NULL)
printf("err!");
strcpy(p2->word, p->word);
if (correctwords->head == NULL)
correctwords->head = p2;
else
pr2->link = p2;
pr2 = p2;
}
}
}
p = p->link;
}
if (pr2 != NULL)
pr2->link = NULL;
return correctwords;
}

void printCorrectWords(PLinkList correctwords, char *word) {
PNode p = correctwords->head;
if (p == NULL)
printf("%s:", word);
else if (strcmp(p->word, word) == 0)
printf("%s is correct", word);
else {
printf("%s:", word);
while (p != NULL) {
printf(" %s", p->word);
p = p->link;
}
}
}

字符串中的数字

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define JUDGEMENT1 (a[i]>='0'&&a[i]<='9')||a[i]=='-'
#define JUDGEMENT2 (a[i]>='0'&&a[i]<='9')||a[i]=='.'

void copy(char *t, char *p, int c, int j);

int main() {
char a[100000];
int n;
scanf("%s", a);
n = strlen(a);
int i = 0, c = 0, j = 0, t = 0;
char s;
while (a[i] != '\0') {
if (JUDGEMENT1) {
int k = 0;
char num[100000];
c = i;
i++;
while (JUDGEMENT2) {
if (a[c] == '-' && a[c + 1] == '.')
break;
if (a[i] == '.' && k == 0)
k = 1;
else if (a[i] == '.' && k == 1)
break;
i++;
j++;
}
j++;
i--;
if (!((j == 1 && a[c] == '-') || (a[c] == '-' && a[c + 1] == '.'))) {
if (a[c])
copy(num, a, c, j);
if (t != 0)
printf("\n");
printf("%s", num);
t = 1;
}
j = 0;
}
i++;
}

return 0;
}

void copy(char *t, char *p, int c, int j) {
for (int i = 0; i <= j - 1; i++) {
t[i] = p[c + i];
}
t[j] = '\0';
}

字符串的周期

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <string.h>

char s1[1000002];
int next[1000002];

int main() {
int t, l, m = 0, k, cul = 0;
scanf("%s", s1);
l = (int)strlen(s1);
next[0] = -1;
int p;
for (int i = 0; i <= l - 1; i++) {
p = next[i];
while (p >= 0 && s1[p] != s1[i])
p = next[p];
next[i + 1] = p + 1;
}
while (cul<l) {
k=next[l-cul];
t=l-k-cul;
if (m != 0)
printf(" ");
cul+=t;
printf("%d", cul);
m++;
}

return 0;
}

学生总分排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
char id[7];
char name[9];
int scores[3];
int total;
} Student;

int C;

int compare(const void *a, const void *b) {
const Student *studentA = (const Student *)a;
const Student *studentB = (const Student *)b;
if (C == 0) {
if(studentB->total - studentA->total!=0)
return studentB->total - studentA->total;
else
return strcmp(studentA->id,studentB->id);
} else if (C == 1) {
return strcmp(studentA->id, studentB->id);
} else if (C == 2) {
if(strcmp(studentA->name, studentB->name)!=0)
return strcmp(studentA->name, studentB->name);
else
return strcmp(studentA->id, studentB->id);
}

return 0;
}

int main() {
int N, M;
scanf("%d %d %d", &N, &M, &C);

Student students[N];
for (int i = 0; i < N; i++) {
scanf("%s %s", students[i].id, students[i].name);
students[i].total = 0;
for (int j = 0; j < M; j++) {
scanf("%d", &students[i].scores[j]);
students[i].total += students[i].scores[j];
}
}

qsort(students, N, sizeof(Student), compare);

for (int i = 0; i < N; i++) {
printf("%s %s", students[i].id, students[i].name);
for (int j = 0; j < M; j++) {
printf(" %d", students[i].scores[j]);
}
printf(" %d\n", students[i].total);
}

return 0;
}

字符串排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int countAON(char* s);
int compare(const void* a,const void* b);

int main() {
char str[100][50];
int m;
scanf("%d",&m);
for(int i=0;i<=m-1;i++) {
scanf("%s",str[i]);
}
qsort(str,m,sizeof(char)*50,compare);
for(int i=0;i<=m-1;i++) {
printf("%s\n",str[i]);
}
}

int countAON(char* s) {
int l=strlen(s),c=0;
for(int i=0;i<=l-1;i++) {
for(int j=i+1;j<=l-1;j++) {
if(s[i]>s[j])
c++;
}
}
return c;
}

int compare(const void* a,const void* b) {
int na,nb;
na=countAON((char *)a);
nb=countAON((char *)b);
return na-nb;
}

二叉树的操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include<stdio.h>
#include<stdlib.h>

typedef struct TreeNode{
int val;
struct TreeNode* lchild;
struct TreeNode* rchild;
}TreeNode;

TreeNode* createNode(int val);
TreeNode* buildTree(int n,int node[][3]);
TreeNode* findNode(TreeNode* root,int val);
TreeNode* findPreNode(TreeNode* root,int val);
void swapNodes(TreeNode* tree,int x,int y);
int findLeftmostChild(TreeNode* tree,int x);
int c;

int main() {
int t;
scanf("%d",&t);
for(int i=1;i<=t;i++) {
int n,m;
scanf("%d %d",&n,&m);
int node[n][3];
for(int j=0;j<=n-1;j++) {
scanf("%d %d %d",&node[j][0],&node[j][1],&node[j][2]);
}
TreeNode* newtree=NULL;
newtree=buildTree(n,node);
for(int j=1;j<=m;j++) {
int type;
scanf("%d",&type);
if(type==1) {
int x,y;
scanf("%d %d",&x,&y);
swapNodes(newtree,x,y);
}
else {
int x,leftmostChild;
scanf("%d",&x);
leftmostChild=findLeftmostChild(newtree,x);
printf("%d\n",leftmostChild);
}
}
free(newtree);
}
return 0;
}


TreeNode* createNode(int val) {
TreeNode* newnode=(TreeNode*)malloc(sizeof(TreeNode));
newnode->val=val;
newnode->lchild=NULL;
newnode->rchild=NULL;
return newnode;
}

TreeNode* buildTree(int n,int node[][3]) {
TreeNode* treeNodes[n];
for(int i=0;i<=n-1;i++) {
treeNodes[node[i][0]]=createNode(node[i][0]);
}
for(int i=0;i<=n-1;i++) {
if(node[i][1]!=-1) {
treeNodes[node[i][0]]->lchild=treeNodes[node[i][1]];
}
if(node[i][2]!=-1) {
treeNodes[node[i][0]]->rchild=treeNodes[node[i][2]];
}
}
return treeNodes[0];
}

TreeNode* findNode(TreeNode* root,int val) {
if(!root) return NULL;
if(root->val==val) return root;
TreeNode* left=findNode(root->lchild,val);
if(left) return left;
return findNode(root->rchild,val);
}

TreeNode* findPreNode(TreeNode* root,int val) {
if(root==NULL||(root->lchild==NULL&&root->rchild==NULL)) return NULL;
if(root->lchild&&root->lchild->val==val) {
c=1;
return root;
}
if(root->rchild&&root->rchild->val==val) {
c=-1;
return root;
}
TreeNode* left=findPreNode(root->lchild,val);
if(left) {
return left;
}
TreeNode* right=findPreNode(root->rchild,val);
return right;
}

void swapNodes(TreeNode* tree,int x,int y) {
int c1,c2;
TreeNode* preNodeX=findPreNode(tree,x);
c1=c;
TreeNode* preNodeY=findPreNode(tree,y);
c2=c;
TreeNode* nodeX=findNode(tree,x);
TreeNode* nodeY=findNode(tree,y);
if(c1==1) {
preNodeX->lchild=nodeY;
}
if(c1==-1) {
preNodeX->rchild=nodeY;
}
if(c2==1) {
preNodeY->lchild=nodeX;
}
if(c2==-1) {
preNodeY->rchild=nodeX;
}
}

int findLeftmostChild(TreeNode* tree,int x) {
TreeNode* nodeX=findNode(tree,x);
while(nodeX->lchild) nodeX=nodeX->lchild;
return nodeX->val;
}

NOIP2013T Day1 T2:火柴排队

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include<stdio.h>
#include<stdlib.h>

typedef struct {
int index;
long number;
}March;

int compare(const void* a,const void* b);
void resort(March* marchsA,March* marchsB,int n);
void merge(March* marchs,int l,int m,int r);
void mergeSort(March* marchs,int l,int r);
void printMarchs(March* marchs, int n);//测试用
March marchsA[100000],marchsB[100000],marchsC[100000];
int ans=0;

int main() {
int n;
scanf("%d",&n);
for(int i=0;i<=n-1;i++) {
scanf("%ld",&marchsA[i].number);
marchsA[i].index=i+1;
}
for(int i=0;i<=n-1;i++) {
scanf("%ld",&marchsB[i].number);
marchsB[i].index=i+1;
}
qsort(marchsA,n,sizeof(March),compare);
qsort(marchsB,n,sizeof(March),compare);
resort(marchsA,marchsB,n);
mergeSort(marchsC,0,n-1);
printf("%d",ans);

return 0;
}

int compare(const void* a,const void* b) {
March* marchA=(March*)a;
March* marchB=(March*)b;
int na,nb;
na=marchA->number;
nb=marchB->number;
return na-nb;
}

void resort(March* marchsA,March* marchsB,int n) {
for(int i=0;i<=n-1;i++) {
int k=marchsB[i].index;
marchsC[k-1]=marchsA[i];
}
}

void merge(March* marchs,int l,int m,int r) {
int n1=m-l+1;
int n2=r-m;
March L[n1],R[n2];
for(int i=0;i<=n1-1;i++) {
L[i]=marchs[l+i];
}
for(int i=0;i<=n2-1;i++) {
R[i]=marchs[m+1+i];
}

int i=0;
int j=0;
int k=l;
while(i<n1&&j<n2) {
if(L[i].index<=R[j].index) {
marchs[k]=L[i];
k++;
i++;
}
else {
marchs[k]=R[j];
k++;
j++;
ans+=n1-i;
ans%=99999997;
}
}

while(i<n1) {
marchs[k]=L[i];
i++;
k++;
}

while(j<n2) {
marchs[k]=R[j];
j++;
k++;
}
}

void mergeSort(March* marchs,int l,int r) {
if(l<r) {
int m=l+(r-l)/2;
mergeSort(marchs,l,m);
mergeSort(marchs,m+1,r);
merge(marchs,l,m,r);
}
}

void printMarchs(March* marchs,int n) {
for(int i=0;i<=n-1;i++) {
printf("index=%d number=%ld\n",marchs[i].index,marchs[i].number);
}
}

合并果子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <stdlib.h>

int compare(const void* a, const void* b) {
return (*(int*)a - *(int*)b);
}

int main() {
int n;
scanf("%d", &n);

unsigned long long* fruits = (unsigned long long*)malloc(n * sizeof(unsigned long long));
if (fruits == NULL) {
fprintf(stderr, "Memory allocation failed.\n");
return 1;
}

for (int i = 0; i < n; i++) {
scanf("%d", &fruits[i]);
}

qsort(fruits, n, sizeof(unsigned long long), compare);

unsigned long long totalCost = 0,ans=0;
while (n > 1) {
unsigned long long a = fruits[0];
unsigned long long b = fruits[1];
totalCost = a+b;
ans+=totalCost;
fruits[1] = totalCost;
fruits++;
n--;
for(int i=0;i<n-1;i++) {
if(fruits[i]>fruits[i+1]) {
unsigned long long t=fruits[i];
fruits[i]=fruits[i+1];
fruits[i+1]=t;
}
}
}

printf("%llu", ans);
return 0;
}

二叉搜索树的层次遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<cstdio>

struct BinTreeNode;
typedef struct BinTreeNode* PBinTreeNode;
struct BinTreeNode {
int n;
PBinTreeNode lchild;
PBinTreeNode rchild;
}BinTreeNode;
struct BinTree;
typedef struct BinTree* PBinTree;
struct BinTree {
PBinTreeNode root;
}BinTree;

struct Queue;
typedef struct Queue* PQueue;
struct Queue {
PBinTreeNode a[10000];
int top;
int tail;
}Queue;

int insertNode(PBinTreeNode root,int key) {
if(root==NULL) {
root=(PBinTreeNode)malloc(sizeof(BinTreeNode));
root->n=key;
root->lchild=NULL;
root->rchild=NULL;
}
if(root->n==key) return 0;
PBinTreeNode p=(PBinTreeNode)malloc(sizeof(BinTreeNode));
p->n=key;
p->lchild=NULL;
p->rchild=NULL;
if(root->n>key) {
if(root->lchild==NULL) {
root->lchild=p;
}
else {
free(p);
insertNode(root->lchild,key);
}
}
else {
if(root->rchild==NULL) {
root->rchild=p;
}
else {
free(p);
insertNode(root->rchild,key);
}
}
return 1;
}

void enQueue(PQueue list,PBinTreeNode x) {
if(x!=NULL) {
list->a[list->tail++]=x;
}
}

PBinTreeNode outQueue(PQueue list) {
PBinTreeNode p=list->a[list->top++];
return p;
}

int isEmptyQueue(PQueue list) {
if(list->tail==list->top) return 0;
return 1;
}

int main() {
int a[10000]={0},b[10000]={0},c=0;
PQueue list=NULL;
list=(PQueue)malloc(sizeof(Queue));
PBinTree tree=NULL;
tree=(PBinTree)malloc(sizeof(BinTree));
tree->root=NULL;
if(list==NULL|tree==NULL) {
printf("Out of memory!");
return 0;
}
int i=0,an;
while(std::cin >> an) {
a[i++]=an;
}
for(int j=0;j<i;j++) {
if(tree->root==NULL) {
tree->root=(PBinTreeNode)malloc(sizeof(BinTreeNode));
tree->root->n=a[j];
tree->root->lchild=NULL;
tree->root->rchild=NULL;
}
else {
insertNode(tree->root,a[j]);
}
}
enQueue(list,tree->root);
while(isEmptyQueue(list)==1) {
PBinTreeNode p=outQueue(list);
b[c++]=p->n;
enQueue(list,p->lchild);
enQueue(list,p->rchild);
}
for(int j=0;j<c-1;j++) {
printf("%d ",b[j]);
}
printf("%d",b[c-1]);
}

树的转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Stack;
typedef struct Stack* PStack;
struct Stack {
int a[10000];
int top;
}Stack;

void push(PStack stack, int x) {
stack->a[stack->top++]=x;
}

int pop(PStack stack) {
return stack->a[--stack->top];
}

int treeHight(char *s) {
int d=0,hight=0;
for(int i=0;i<strlen(s);i++) {
if(s[i]=='d') {
d++;
if(d>hight) {
hight=d;
}
}
if(s[i]=='u') {
d--;
}
}
return hight;
}

int binTreeHight(char* s) {
int d=0,hight=0;
PStack stack=(PStack)malloc(sizeof(Stack));
stack->top=0;
for(int i=0;i<strlen(s);i++) {
if(s[i]=='d') {
push(stack,d);
d++;
}
else {
if(s[i+1]=='d') {
d++;
i++;
}
else {
d=pop(stack);
}
}
if(d>hight) {
hight=d;
}
}
return hight;
}

char s[100001];

int main() {
int count=1;
while(1) {
scanf("%s",s);
if(s[0]=='#') break;
int treehight,bintreehight;
treehight=treeHight(s);
bintreehight=binTreeHight(s);
printf("Tree %d: %d => %d\n",count,treehight,bintreehight);
count++;
}
return 0;
}

正方形

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <stdio.h>
#include <stdlib.h>
#define MAX_NUM 25343

struct HashNode;
typedef struct HashNode* PHashNode;
struct HashNode {
int x,y;
PHashNode next;
}HashNode;

struct HashTable {
PHashNode* table;
}HashTable;
typedef struct HashTable* PHashTable;

int hash(int x,int y) {
return ((x+20000)*40001+(20000+y))%MAX_NUM;
}

PHashTable createEmptyHash() {
PHashTable p=(PHashTable)malloc(sizeof(HashTable));
p->table=(PHashNode*)malloc(sizeof(PHashNode)*MAX_NUM);
for(int i=0;i<MAX_NUM;i++) {
p->table[i]=NULL;
}
return p;
}

void insertNode(PHashTable p,int x,int y) {
int h=hash(x,y);
PHashNode point=(PHashNode)malloc(sizeof(HashNode));
point->x=x;
point->y=y;
point->next=NULL;
if(p->table[h]==NULL) {
p->table[h]=point;
}
else {
PHashNode node=p->table[h];
while(node->next!=NULL) {
node=node->next;
}
node->next=point;
}
}

int pointExist(PHashTable p,int x,int y) {
int h=hash(x,y);
if(p->table[h]==NULL) {
return 0;
}
else {
PHashNode node=p->table[h];
while(node!=NULL) {
if(node->x==x&&node->y==y) {
return 1;
}
node=node->next;
}
}
return 0;
}

void freeHashTable(PHashTable p) {
for(int i=0;i<MAX_NUM;i++) {
PHashNode node=p->table[i];
while(node!=NULL) {
PHashNode temp=node;
node=node->next;
free(temp);
}
}
free(p->table);
free(p);
}

int main() {
int n;
while(1) {
scanf("%d",&n);
if(n==0) break;
PHashTable p=createEmptyHash();
int points[n][2];
for(int i=0;i<n;i++) {
scanf("%d %d",&points[i][0],&points[i][1]);
insertNode(p,points[i][0],points[i][1]);
}

int sqearCount=0;
for(int i=0;i<n;i++) {
for(int j=i+1;j<n;j++) {
int x1,x2,y1,y2;
x1=points[i][0];
y1=points[i][1];
x2=points[j][0];
y2=points[j][1];

int dx=x2-x1;
int dy=y2-y1;

int x3=x2+dy;
int y3=y2-dx;
int x4=x1+dy;
int y4=y1-dx;
if(pointExist(p,x3,y3)==1&&pointExist(p,x4,y4)==1) {
sqearCount++;
}

x3=x2-dy;
y3=y2+dx;
x4=x1-dy;
y4=y1+dx;
if(pointExist(p,x3,y3)==1&&pointExist(p,x4,y4)==1) {
sqearCount++;
}
}
}
printf("%d\n",sqearCount/4);
freeHashTable(p);
}
return 0;
}

[USACO 2019 Jan Platinum P1] Redistricting

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include<stdio.h>
#include<stdlib.h>
#define MAX 300010

typedef struct{
int x,y;
}Node;
typedef struct{
Node queue[MAX];
int tail;
int top;
}Queue;
typedef Queue *PQueue;

int sum[MAX],f[MAX];
char s[MAX];
PQueue pq=NULL;

//比较两个元素的大小
int compare_nodes(Node a,Node b);
//在队列中插入元素并排序
void pq_push(Node node);
//弹出队列头元素并排序
void pq_pop();

int main()
{
int n,k;
scanf("%d %d",&n,&k);
scanf("%s",s);
sum[1]=s[0]=='H'?1:0;
for(int i=2;i<=n;i++)
{
sum[i]=s[i-1]=='H'?sum[i-1]+1:sum[i-1]-1;
}
pq=(PQueue)malloc(sizeof(Queue));
pq->top=0;
pq->tail=0;

f[0]=0;
pq_push((Node){0,0});
for(int i=1;i<=n;i++)
{
while(pq->queue[pq->top].y<i-k) pq_pop();
f[i]=(sum[i]-sum[pq->queue[pq->top].y])<=0?pq->queue[pq->top].x+1:pq->queue[pq->top].x;
pq_push((Node){f[i],i});
}

printf("%d",f[n]);

return 0;
}

int compare_nodes(Node a,Node b)
{
if(a.x==b.x)
return sum[b.y]-sum[a.y];
else{
return b.x-a.x;
}
}

void pq_push(Node node)
{
int i=pq->tail;
pq->queue[pq->tail++]=node;
while(i>0&& compare_nodes(pq->queue[i],pq->queue[(i-1)/2])>0)
{
Node temp=pq->queue[i];
pq->queue[i]=pq->queue[(i-1)/2];
pq->queue[(i-1)/2]=temp;
i=(i-1)/2;
}
}

void pq_pop()
{
pq->queue[pq->top]=pq->queue[--pq->tail];
int i=0;
while(2*i+1<pq->tail){
int left=2*i+1;
int right=2*i+2;
int j=left;
if(right<pq->tail&& compare_nodes(pq->queue[right],pq->queue[left])>0)
j=right;
if(compare_nodes(pq->queue[i],pq->queue[j])>=0)break;
Node temp=pq->queue[i];
pq->queue[i]=pq->queue[j];
pq->queue[j]=temp;
i=j;
}
}

N皇后问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdio.h>

void aviable(int a[],int c[],int row,int n) {
for(int i=0;i<row;i++) {
int k=c[i];
int l=c[i]-(row-i);
int m=c[i]+(row-i);
a[k]=1;
if(l>=0) a[l]=1;
if(m<n) a[m]=1;
}
}

void printList(int c[],int n) {
for(int i=0;i<n-1;i++) {
printf("%d ",c[i]+1);
}
printf("%d\n",c[n-1]+1);
}

void add(int c[],int *d,int row,int x) {
for(int i=0;i<row;i++) {
d[i]=c[i];
}
d[row]=x;
}

int count=0;
void dfs(int c[],int row,int n) {
if(row==n-1) {
int a[15]={0};
aviable(a,c,n-1,n);
for(int i=0;i<n;i++) {
if(a[i]==0) {
int d[15];
add(c,d,n-1,i);
if(count<3) printList(d,n);
count++;
}
}
}
else {
int a[15]={0};
aviable(a,c,row,n);
for(int i=0;i<n;i++) {
if(a[i]==0) {
int d[15]={0};
add(c,d,row,i);
dfs(d,row+1,n);
}
}
}
}

int main() {
int n;
scanf("%d",&n);
int c[15]={0};
dfs(c,0,n);
printf("%d",count);

return 0;
}

判断图连通

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <stdio.h>
#include <stdlib.h>
#define MAX_NUM 100000

struct Queue;
typedef struct Queue* PQueue;
struct Queue {
int a[MAX_NUM];
int top;
int tail;
}Queue;

struct Node;
typedef struct Node* PNode;
struct Node {
int x;
PNode next;
}Node;

int pop(PQueue list) {
return list->a[list->top++];
}

void push(PQueue list,int x) {
list->a[list->tail++]=x;
}

int isEmpty(PQueue list) {
if(list->tail==list->top) return 0;
return 1;
}

void add(PNode p,int x) {
PNode pr=p;
while(pr->next!=NULL) {
pr=pr->next;
}
PNode newNode=(PNode)malloc(sizeof(Node));
newNode->x=x;
newNode->next=NULL;
pr->next=newNode;
}

void buildConnect(int m,PNode* connect) {
for(int i=0;i<m;i++) {
int x,y;
scanf("%d %d",&x,&y);
x-=1;
y-=1;
PNode px=connect[x];
PNode py=connect[y];
add(px,y);
add(py,x);
}
}

void isConnect(PNode* connect,int n,int* visit) {
PQueue list=(PQueue)malloc(sizeof(Queue));
list->tail=0;
list->top=0;
push(list,connect[0]->x);
visit[connect[0]->x]=1;
while(isEmpty(list)) {
int p=pop(list);
PNode pr=connect[p];
while(pr!=NULL) {
if(visit[pr->x]==0) {
push(list,pr->x);
visit[pr->x]=1;
}
pr=pr->next;
}
}
int r=1;
for(int i=0;i<n;i++) {
if(visit[i]==0) {
r=0;
break;
}
}
if(r==1) {
printf("YES");
}
else {
printf("NO");
}
}

int main() {
int visit[MAX_NUM]={0};
int n,m;
scanf("%d %d",&n,&m);
PNode* connect=(PNode*)malloc(sizeof(PNode)*n);
for(int i=0;i<n;i++) {
connect[i]=(PNode)malloc(sizeof(Node));
connect[i]->x=i;
connect[i]->next=NULL;
}
buildConnect(m,connect);
isConnect(connect,n,visit);

return 0;
}

01数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>

#define MAX_N 1000001

int fenwick_tree[MAX_N];

void update(int index, int value, int N) {
while (index <= N) {
fenwick_tree[index] += value;
index += index & -index;
}
}

int query(int index) {
int sum = 0;
while (index > 0) {
sum += fenwick_tree[index];
index -= index & -index;
}
return sum;
}

void process_operations(int N, int Q) {
int l, r, x;
int op_type;

for (int i = 0; i <= N; i++) {
fenwick_tree[i] = 0;
}

for (int i = 0; i < Q; i++) {
scanf("%d", &op_type);
if (op_type == 1) {
scanf("%d %d", &l, &r);
update(l, 1, N);
if (r + 1 <= N) {
update(r + 1, -1, N);
}
} else if (op_type == 2) {
scanf("%d", &x);
int flips = query(x);
printf("%d\n", flips % 2);
}
}
}

int main() {
int N, Q;
scanf("%d %d", &N, &Q);

process_operations(N, Q);

return 0;
}