1717
This commit is contained in:
11
include/solution/1717.h
Normal file
11
include/solution/1717.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef INC_1717_H
|
||||||
|
#define INC_1717_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
int maximumGain(char *s, int x, int y);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
91
src/1717.c
Normal file
91
src/1717.c
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
#include <solution/1717.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
char *base;
|
||||||
|
size_t size;
|
||||||
|
size_t top;
|
||||||
|
} stack_t;
|
||||||
|
|
||||||
|
void init_stack(stack_t *s)
|
||||||
|
{
|
||||||
|
s->base = malloc(sizeof(char) * 10);
|
||||||
|
s->size = 10;
|
||||||
|
s->top = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void push(stack_t *s, char c)
|
||||||
|
{
|
||||||
|
if (s->top + 1 >= s->size)
|
||||||
|
{
|
||||||
|
s->size += 10;
|
||||||
|
s->base = realloc(s->base, sizeof(char) * s->size);
|
||||||
|
}
|
||||||
|
s->base[++(s->top)] = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
char pop(stack_t *s)
|
||||||
|
{
|
||||||
|
if (s->top == 0)
|
||||||
|
return '\0';
|
||||||
|
return s->base[(s->top)--];
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear(stack_t *s)
|
||||||
|
{
|
||||||
|
s->top = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
char top(stack_t *s)
|
||||||
|
{
|
||||||
|
return s->base[s->top];
|
||||||
|
}
|
||||||
|
|
||||||
|
char *sts(stack_t *s)
|
||||||
|
{
|
||||||
|
char *ret = malloc(sizeof(char) * (s->top + 1));
|
||||||
|
|
||||||
|
memcpy(ret, s->base + 1, s->top);
|
||||||
|
ret[s->top] = '\0';
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void delete_stack(stack_t *s)
|
||||||
|
{
|
||||||
|
free(s->base);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *del(char *s, char m1, char m2, int *score, int v)
|
||||||
|
{
|
||||||
|
stack_t st;
|
||||||
|
init_stack(&st);
|
||||||
|
while (*s)
|
||||||
|
{
|
||||||
|
if (top(&st) == m1 && *s == m2)
|
||||||
|
{
|
||||||
|
s += 1, *score += v;
|
||||||
|
pop(&st);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
push(&st, *(s++));
|
||||||
|
}
|
||||||
|
|
||||||
|
char *r = sts(&st);
|
||||||
|
delete_stack(&st);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
int maximumGain(char *s, int x, int y)
|
||||||
|
{
|
||||||
|
int score = 0;
|
||||||
|
if (x > y)
|
||||||
|
del(del(s, 'a', 'b', &score, x), 'b', 'a', &score, y);
|
||||||
|
else
|
||||||
|
del(del(s, 'b', 'a', &score, y), 'a', 'b', &score, x);
|
||||||
|
|
||||||
|
return score;
|
||||||
|
}
|
||||||
30
tests/test_1717.cpp
Normal file
30
tests/test_1717.cpp
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <solution/1717.h>
|
||||||
|
|
||||||
|
class MaximumGainTest : public ::testing::Test
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
void AssertMaximumGain(const std::string &input, int x, int y, int expected)
|
||||||
|
{
|
||||||
|
char *s = strdup(input.c_str()); // 复制字符串,防止修改原始数据
|
||||||
|
int result = maximumGain(s, x, y);
|
||||||
|
ASSERT_EQ(result, expected);
|
||||||
|
free(s);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 示例 1
|
||||||
|
TEST_F(MaximumGainTest, Example1)
|
||||||
|
{
|
||||||
|
// 输入:s = "cdbcbbaaabab", x = 4, y = 5
|
||||||
|
// 输出:19
|
||||||
|
AssertMaximumGain("cdbcbbaaabab", 4, 5, 19);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 示例 2
|
||||||
|
TEST_F(MaximumGainTest, Example2)
|
||||||
|
{
|
||||||
|
// 输入:s = "aabbaaxybbaabb", x = 5, y = 4
|
||||||
|
// 输出:20
|
||||||
|
AssertMaximumGain("aabbaaxybbaabb", 5, 4, 20);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user