Init
This commit is contained in:
60
ACMOJ-1729.hpp
Normal file
60
ACMOJ-1729.hpp
Normal file
@ -0,0 +1,60 @@
|
||||
#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;
|
||||
}
|
Reference in New Issue
Block a user