-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaximumScore.c
99 lines (90 loc) · 2.18 KB
/
MaximumScore.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char *data;
int top;
int size;
} Stack;
void initStack(Stack *stack, int size) {
stack->data = (char *)malloc(size * sizeof(char));
stack->top = -1;
stack->size = size;
}
void cleanupStack(Stack *stack) {
free(stack->data);
stack->data = NULL;
stack->top = -1;
stack->size = 0;
}
void push(Stack *stack, char value) {
if (stack->top < stack->size - 1) {
stack->top++;
stack->data[stack->top] = value;
}
}
void pop(Stack *stack) {
if (stack->top >= 0) {
stack->top--;
}
}
int isEmpty(Stack *stack) {
return stack->top == -1;
}
int maximumGain(char *s, int x, int y) {
int score = 0;
int len = strlen(s);
Stack stack1, stack2;
initStack(&stack1, len);
initStack(&stack2, len);
if (x > y) {
for (int i = 0; i<len; i++) {
if(!isEmpty(&stack1) && stack1.data[stack1.top] == 'a' && s[i] == 'b') {
pop(&stack1);
score += x;
}else{
push(&stack1, s[i]);
}
}
while (!isEmpty(&stack1)) {
char current = stack1.data[stack1.top];
pop(&stack1);
if (!isEmpty(&stack2) && stack2.data[stack2.top] == 'a' && current == 'b') {
pop(&stack2);
score += y;
}else{
push(&stack2, current);
}
}
}else if (y>x){
for (int i = 0; i<len; i++) {
if(!isEmpty(&stack1) && stack1.data[stack1.top] == 'b' && s[i] == 'a') {
pop(&stack1);
score += y;
}else{
push(&stack1, s[i]);
}
}
while (!isEmpty(&stack1)) {
char current = stack1.data[stack1.top];
pop(&stack1);
if(!isEmpty(&stack2) && stack2.data[stack2.top] == 'b' && current == 'a') {
pop(&stack2);
score += x;
}else{
push(&stack2, current);
}
}
}
cleanupStack(&stack1);
cleanupStack(&stack2);
return score;
}
int main() {
char s[] ="sdadsadsadsadadadaabababbababbbaaa";
int x = 6;
int y = 321;
int result = maximumGain(s, x, y);
printf("Pontuacao maxima: %d\n", result);
return 0;
}