Files
SH-Quizzes/ACMOJ-1729.hpp
2023-12-23 22:23:48 +08:00

60 lines
2.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <algorithm>
#include <cstring>
using namespace std;
void Init(char **&p, int n) { // 初始化n为string的个数,传入的p指向nullptr
// todo
p = new char *[n + 1];
p[0] = reinterpret_cast<char *>(new int[n + 1]);
for (int i = 1; i <= n; i++) reinterpret_cast<int *>(p[0])[i] = 0;
reinterpret_cast<int *>(p[0])[0] = n;
for (int i = 1; i <= n; i++) {
p[i] = new char[1];
p[i][0] = 0;
}
}
void Assign_String(char **&p, int x,
std::string str) { // 将str赋值给第x个string
// todo
delete[] p[x];
reinterpret_cast<int *>(p[0])[x] = str.length();
char *new_p = new char[str.length() + 1];
memcpy(new_p, str.c_str(), str.length() + 1);
p[x] = new_p;
}
void Merge_String(
char **&p, int x, int y,
int z) { // 把第y个string拼接到第x个string后面赋给第z个string(x,y,z互不相等)
// (第x和y个string不变)
// todo
reinterpret_cast<int *>(p[0])[z] =
reinterpret_cast<int *>(p[0])[x] + reinterpret_cast<int *>(p[0])[y];
delete[] p[z];
p[z] = new char[reinterpret_cast<int *>(p[0])[z] + 1];
memcpy(p[z], p[x], reinterpret_cast<int *>(p[0])[x]);
memcpy(p[z] + reinterpret_cast<int *>(p[0])[x], p[y],
reinterpret_cast<int *>(p[0])[y]);
p[z][reinterpret_cast<int *>(p[0])[z]] = 0;
}
void Double_String(
char **&p, int x) { // 把第x个string复制两遍前后拼接起来再赋给第x个string
// todo
char *new_p = new char[reinterpret_cast<int *>(p[0])[x] * 2 + 1];
memcpy(new_p, p[x], reinterpret_cast<int *>(p[0])[x]);
memcpy(new_p + reinterpret_cast<int *>(p[0])[x], p[x],
reinterpret_cast<int *>(p[0])[x]);
reinterpret_cast<int *>(p[0])[x] *= 2;
delete[] p[x];
p[x] = new_p;
p[x][reinterpret_cast<int *>(p[0])[x]] = 0;
}
char *Get_String(char **&p, int x) { // 返回第x个string
// todo
return p[x];
}
void Clear(char **&p, int n) { // 回收空间
// todo
for (int i = 1; i <= n; i++) delete[] p[i];
delete[] (reinterpret_cast<int *>(p[0]));
delete[] p;
}