开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 127

ZhangHao/数据结构(C++模板实现)

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (3)
master
Prometheus
dev
master
分支 (3)
master
Prometheus
dev
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (3)
master
Prometheus
dev
data-structures-cpp
/
String
/
src
/
cyber_dash_string.h
data-structures-cpp
/
String
/
src
/
cyber_dash_string.h
cyber_dash_string.h 31.68 KB
一键复制 编辑 原始数据 按行查看 历史
Y_Dash 提交于 2023年06月23日 00:53 +08:00 . 字符串优化doxygen
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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
/*!
* @file cyber_dash_string.h
* @author CyberDash计算机考研, cyberdash@163.com(抖音id:cyberdash_yuan)
* @brief 字符串匹配模板类
* @version 0.2.1
* @date 2021年07月29日
*/
#ifndef CYBER_DASH_STRING_H
#define CYBER_DASH_STRING_H
#include <iostream>
#include <cstdlib>
using namespace std;
const size_t DEFAULT_SIZE = 128;
/*!
* @brief **字符串类**
* @note
* 字符串类
* -------
* -------
*
* -------
*/
class String {
public:
explicit String(size_t size = DEFAULT_SIZE); // 构造函数(字符串长度)
explicit String(const char* mem_data); // 构造函数(字符串数组)
String(const String& str); // 复制构造函数
~String() { delete[] mem_data_; } // 析构函数
/*!
* @brief **长度**
* @return 长度
*/
int Length() const { return this->length_; }
/*!
* @brief **最大长度**
* @return 最大长度
*/
size_t Size() const { return this->size_; }
const char& operator[](size_t index) const; // 重载[]
char& operator[](size_t index); // 重载[]
String& operator=(const String& src_str); // 重载=
String& operator+=(String& str); // 重载+=
friend ostream& operator<<(ostream& os, const String& str); // 重载<<
friend bool operator==(const String& str1, const String& str2); // 重载==
friend bool operator!=(const String& str1, const String& str2); // 重载!=
friend bool operator<(const String& str1, const String& str2); // 重载<
friend bool operator<=(const String& str1, const String& str2); // 重载<=
friend bool operator>(const String& str1, const String& str2); // 重载>
friend bool operator>=(const String& str1, const String& str2); // 重载>=
int BruteForceMatch(const String& pattern, int offset) const; // BF字符串匹配
int KmpMatchByCyberdash(const String& pattern, int offset) const; // KMP字符串匹配查找
int KmpMatch(const String& pattern, int offset) const; // KMP字符串匹配查找(使用KMPNextByCyberDash生成的next数组)
int* KmpNext() const; // 求next数组
int* KmpNextByCyberDash() const; // 求next数组(CyberDash版)
private:
char* mem_data_; //!< **字符串数组**
int length_; //!< **当前字符串长度**
size_t size_; //!< **最大长度**
};
/*!
* @brief **构造函数(最大长度)**
* @param size 字符串最大长度
* @note
* 构造函数(最大长度)
* ---------------
* ---------------
*
* ---------------
* + **1 设置属性**\n
* 设置size_\n
* 设置length_\n\n
* + **2 设置mem_data_**\n
* mem_data_分配内存并初始化\n
* **if** 内存分配失败 :\n
* &emsp; 抛出bad_alloc()\n\n
* mem_data_数据置0\n
*
*
* ---------------
*/
String::String(size_t size) {
// ---------- 1 设置属性 ----------
this->size_ = size; // 设置size_
this->length_ = 0; // 设置length_
// ---------- 2 设置mem_data_ ----------
this->mem_data_ = new char[size + 1]; // mem_data_分配内存并初始化
if (!this->mem_data_) { // if 内存分配失败
throw bad_alloc(); // 抛出bad_alloc()
}
memset(this->mem_data_, 0, sizeof(char) * (this->size_ + 1)); // mem_data_数据置0
}
/*!
* @brief **构造函数(字符串)**
* @param mem_data 字符串
* @note
* 构造函数(字符串)
* --------------
* --------------
*
* --------------
*/
String::String(const char* mem_data) {
size_t str_len = strlen(mem_data);
if (str_len > DEFAULT_SIZE) {
this->size_ = str_len;
} else {
this->size_ = DEFAULT_SIZE;
}
this->mem_data_ = new char[this->size_ + 1];
if (!this->mem_data_) {
throw bad_alloc();
}
this->length_ = (int)str_len;
memset(this->mem_data_, 0, sizeof(char) * (this->size_ + 1)); // 先全部置0
memcpy(this->mem_data_, mem_data, sizeof(char) * str_len); // 再复制字符串内容
}
/*!
* @brief **复制构造函数**
* @param str 源字符串
* @note
* 复制构造函数
* ----------
* ----------
*
* ----------
* + **1 自身函数处理**\n
* **if** 复制自身 :\n
* &emsp; 退出函数\n\n
* + **2 复制**\n
* 设置length_\n
* 设置size_\n\n
* mem_data_分配内存并初始化\n
* **if** 内存分配失败 :\n
* &emsp; 抛出bad_alloc()\n\n
* **for loop** 遍历源字符串 :\n
* &emsp; 设置mem_data_[i]\n
* mem_data_[this->Length()]置'0円'\n
*
*
* ----------
*/
String::String(const String& str) {
// ---------- 1 自身函数处理 ----------
if (&str == this) { // if 复制自身
return; // 退出函数
}
// ---------- 2 复制 ----------
this->length_ = str.Length(); // 设置length_
this->size_ = str.Size(); // 设置size_
this->mem_data_ = new char[str.Length() + 1]; // mem_data_分配内存并初始化
if (!this->mem_data_) { // if 内存分配失败
throw bad_alloc(); // 抛出bad_alloc()
}
for (int i = 0; i < str.Length(); i++) { // for loop 遍历源字符串
this->mem_data_[i] = str[i]; // 设置mem_data_[i]
}
this->mem_data_[this->Length()] = '0円'; // mem_data_[this->Length()]置'0円'
}
/*!
* @brief **重载=**
* @param src_str 源字符串
* @return 自身字符串
* @note
* **重载=**
* --------
* --------
*
* 与复制构造函数基本相同
*
* --------
*/
String& String::operator=(const String& src_str) {
if (&src_str == this) {
return *this;
}
if (!this->mem_data_) {
delete[] this->mem_data_;
}
this->length_ = src_str.Length();
this->mem_data_ = new char[src_str.Length() + 1];
if (!mem_data_) {
throw bad_alloc();
}
for (int i = 0; i < src_str.Length(); i++) {
this->mem_data_[i] = src_str[i];
}
this->mem_data_[this->Length()] = '0円';
return *this;
}
/*!
* @brief **重载+=**
* @param str 字符串
* @return 新字符串
* @note
* 重载+=
* -----
* -----
*
* -----
* + **1 拼接空字符串处理**\n
* **if** 拼接空字符串 :\n
* &emsp; 返回自身\n\n
* + **2 执行**\n
* 初始化new_length为字符串拼接后的长度\n
* **if** 当前字符串Size() >= 拼接后的字符串长度 :\n
* &emsp; **for loop** 遍历str :\n
* &emsp;&emsp; mem_data_[i + this->Length()] = str[i]\n
* &emsp; mem_data_[new_length]置'0円'\n
* **else**\n
* &emsp; 分配内存并初始化new_mem_data(新的字符数组)\n\n
* &emsp; **for loop** 遍历当前字符串 :\n
* &emsp;&emsp; 设置new_mem_data[i]\n\n
* &emsp; **for loop** 遍历str :\n
* &emsp;&emsp; 设置new_mem_data[Length() + i]\n
* &emsp; new_mem_data[new_length]置'0円'\n\n
* &emsp; 释放this->mem_data_\n
* &emsp; this->mem_data_指向new_mem_data\n\n
* 更新length_\n
* 更新size_\n
* + **3 返回自身**\n
* 返回*this\n
*
*
* -----
*/
String& String::operator+=(String& str) {
// ---------- 1 拼接空字符串处理 ----------
if (str.Length() == 0) { // if 拼接空字符串
return *this; // 返回自身
}
// ---------- 2 执行 ----------
int new_length = this->Length() + str.Length(); // 初始化new_length为字符串拼接后的长度
if ((int)this->Size() >= new_length) { // if 当前字符串Size() >= 拼接后的字符串长度
for (int i = 0; i < str.Length(); i++) { // for loop 遍历str
this->mem_data_[i + this->Length()] = str[i]; // mem_data_[i + this->Length()] = str[i]
}
this->mem_data_[new_length] = '0円'; // mem_data_[new_length]置'0円'
} else { // else
char* new_mem_data = new char[new_length + 1]; // 分配内存并初始化new_mem_data(新的字符数组)
for (int i = 0; i < this->Length(); i++) { // for loop 遍历当前字符串
new_mem_data[i] = this->operator[](i); // 设置new_mem_data[i]
}
for (int i = 0; i < str.Length(); i++) { // for loop 遍历str
new_mem_data[this->Length() + i] = str[i]; // 设置new_mem_data[Length() + i]
}
new_mem_data[new_length] = '0円'; // new_mem_data[new_length]置'0円'
delete[] this->mem_data_; // 释放this->mem_data_
this->mem_data_ = new_mem_data; // this->mem_data_指向new_mem_data
}
this->length_ = new_length; // 更新length_
this->size_ = new_length; // 更新size_
// ---------- 3 返回自身 ----------
return *this; // 返回*this
}
/*!
* @brief **重载[]**
* @param index 索引
* @return 字符
* @note
* 重载[]
* -----
* -----
*
* -----
* **if** 索引值 >= 字符串长度 :\n
* &emsp; 抛出异常\n\n
* 返回mem_data_[index]\n
*
*
* -----
*/
char& String::operator[] (size_t index) {
if ((int)index >= Length()) { // if 索引值 >= 字符串长度
throw exception("Out of Range"); // 抛出异常
}
return this->mem_data_[index]; // 返回mem_data_[index]
}
/*!
* @brief **重载[]**
* @param str1 字符串1
* @param str2 字符串2
* @return 结果
* @note
* 重载[]
* -----
* -----
*
* -----
*/
const char& String::operator[] (size_t index) const {
if ((int)index >= Length()) {
throw exception("Out of Range");
}
return this->mem_data_[index];
}
/*!
* @brief **重载<&lt;**
* @param str1 字符串1
* @param str2 字符串2
* @return 结果
* @note
* 重载<<
* -----
* -----
*
* -----
*/
ostream& operator<<(ostream& os, const String& str) {
os << str.mem_data_;
return os;
}
/*!
* @brief **重载==**
* @param str1 字符串1
* @param str2 字符串2
* @return 结果
* @note
* 重载==
* -----
* -----
*
* -----
* **if** str1和str2的长度不同 :\n
* &emsp; 返回false\n\n
* **for loop** str1的长度 :\n
* &emsp; **if** 当前位置的str1[i] != str2[i] :
* &emsp;&emsp; 返回false\n\n
* 返回true\n
*
*
* -----
*/
bool operator==(const String& str1, const String& str2) {
if (str1.Length() != str2.Length()) { // if str1和str2的长度不同
return false; // 返回false
}
for (int i = 0; i < str1.Length(); i++) { // for loop str1的长度
if (str1[i] != str2[i]) { // if 当前位置的str1[i] != str2[i]
return false; // 返回false
}
}
return true; // 返回true
}
/*!
* @brief **重载!=**
* @param str1 字符串1
* @param str2 字符串2
* @return 结果
* @note
* 重载!=
* -----
* -----
*
* -----
*/
bool operator!=(const String& str1, const String& str2) {
return !(str1 == str2);
}
/*!
* @brief **重载>**
* @param str1 字符串1
* @param str2 字符串2
* @return 结果
* @note
* 重载>
* -----
* -----
*
* -----
*/
bool operator>(const String& str1, const String& str2) {
size_t size = str1.Size() >= str2.Size() ? str1.Size() : str2.Size();
if(strncmp(str1.mem_data_, str2.mem_data_, size) > 0) {
return true;
}
return false;
}
/*!
* @brief **重载>=**
* @param str1 字符串1
* @param str2 字符串2
* @return 结果
* @note
* 重载>=
* -----
* -----
*
* -----
*/
bool operator>=(const String& str1, const String& str2) {
size_t size = str1.Size() >= str2.Size() ? str1.Size() : str2.Size();
if(strncmp(str1.mem_data_, str2.mem_data_, size) >= 0) {
return true;
}
return false;
}
/*!
* @brief **重载&lt;**
* @param str1 字符串1
* @param str2 字符串2
* @return 结果
* @note
* 重载<
* -----
* -----
*
* -----
*/
bool operator<(const String& str1, const String& str2) {
if (str1 >= str2) {
return false;
}
return true;
}
/*!
* @brief **重载<=**
* @param str1 字符串1
* @param str2 字符串2
* @return 结果
* @note
* 重载<=
* -----
* -----
*
* -----
*/
bool operator<=(const String& str1, const String& str2) {
if (str1 > str2) {
return false;
}
return true;
}
/*!
* @brief **BF字符串匹配**
* @param pattern 模式串
* @param offset 目标串的起始偏移量
* @return 匹配位置索引
* @note
* BF字符串匹配
* ----------
* ----------
*
* ----------
* 初始化match_idx为-1\n\n
* **for loop** 遍历目标串 :\n
* &emsp; 初始化match_idx为-1\n
* &emsp; **for loop** 遍历模式串 :\n
* &emsp;&emsp; **if** 当前模式串字符 != 当前目标串字符 :\n
* &emsp;&emsp;&emsp; 跳出模式串遍历循环\n\n
* &emsp; **if** 模式串遍历完毕 :\n
* &emsp;&emsp; match_idx设为i\n
* &emsp;&emsp; 跳出目标串遍历循环\n\n
* 返回match_idx\n
*
*
* ----------
*/
int String::BruteForceMatch(const String& pattern, int offset) const {
int match_idx = -1; // 初始化match_idx为-1
for (int i = offset; i <= length_ - pattern.length_; i++) { // for loop 遍历目标串
int pattern_idx; // 初始化match_idx为-1
for (pattern_idx = 0; pattern_idx < pattern.length_; pattern_idx++) { // for loop 遍历模式串
if (mem_data_[i + pattern_idx] != pattern[pattern_idx]) { // if 当前模式串字符 != 当前目标串字符
break; // 跳出模式串遍历循环
}
}
if (pattern_idx == pattern.length_) { // if 模式串遍历完毕
match_idx = i; // match_idx设为i
break; // 跳出目标串遍历循环
}
}
return match_idx; // 返回match_idx
}
/*!
* @brief **求next数组**
* @return next数组
* @note
* 求next数组
* ---------
* ---------
*
* 1. <span style="color:#003153">为什么next[0] = -1, next[1] = 0 ?</span>\n
*
* A. 当pattern[1]失配时, 必然从pattern[0]开始重新进行匹配, 因此next[1] = 0\n
* B. 如果: next[i] == x && pattern[i] == pattern[x], 即pattern[i] == pattern[next[i]], 则: next[i + 1] = x + 1\n
*
* 因此:\n
* &emsp;&emsp; 令next[1] = next[0] + 1 = 0\n
* 得:\n
* &emsp;&emsp; next[0] = -1\n\n
* 2. <span style="color:#003153">demo</span>\n
* 遍历至i = 8, 此时starting_index = 2, 即<span style="color:#FF8100">next[8] = 2</span>\n
* ```
* a b c d 5 6 a b c d 7
* a b
* a b
* ^
* |
* |
*
* ```
* 由于: \n
* &emsp; pattern[i] == pattern[starting_index] = 'c'\n
* 因此: \n
* &emsp; i加1, i = 9;\n
* &emsp; starting_index加1, starting_index = 3\n
* &emsp; next[i] = starting_index, 即<span style="color:#D40000">next[9] = 3</span>\n
* ```
* a b c d 5 6 a b c d 7
* a b c
* a b c
* ^
* |
* |
*
* ```
*
* ---------
* + **1 初始化**\n\n
* 初始化i(模式化遍历索引)为0\n
* starting_index为-1\n
* next分配内存并初始化\n
* next[0] 设为 -1\n\n
* + **2 完成next数组设置**\n\n
* **while loop** 遍历模式串 :\n
* &emsp; **if** starting_index == -1 <b>||</b> 索引i和索引starting_index两个位置的字符相同 :\n
* &emsp;&emsp; i向后移动1位\n
* &emsp;&emsp; starting_index向后移动1位\n
* &emsp;&emsp; next[i] <-- starting_index\n
* &emsp; **else**\n
* &emsp;&emsp; starting_index <-- next[starting_index]\n\n
* + **3 退出函数**\n\n
* 返回next数组\n
*
*
* ---------
*/
int* String::KmpNext() const {
// ---------- 1 初始化 ----------
int i = 0; // 初始化i(模式化遍历索引)为0
int starting_index = -1; // starting_index为-1
int* next = new int[this->length_]; // next分配内存并初始化
next[i] = starting_index; // next[0] 设为 -1
// ---------- 2 完成next数组设置 ----------
while (i < this->length_) { // while loop 遍历模式串
if (starting_index == -1 || this->mem_data_[i] == this->mem_data_[starting_index]) { // if starting_index == -1 || 索引i和索引starting_index两个位置的字符相同
i++; // i向后移动1位
starting_index++; // starting_index向后移动1位
next[i] = starting_index; // next[i] <-- starting_index
} else { // else
starting_index = next[starting_index]; // starting_index <-- next[starting_index]
}
}
// ---------- 3 退出函数 ----------
return next; // 返回next数组
}
/*!
* @brief **求next数组(CyberDash版本)**
* @return next数组
* @note
* 求next数组(CyberDash版本)
* -----------------------
* -----------------------
*
* 1. <span style="color:#003153">为什么next[0] = -1, next[1] = 0 ?</span>\n
*
* A. 当pattern[1]失配时, 必然从pattern[0]开始重新进行匹配, 因此next[1] = 0\n
* B. 如果: next[i] == x && pattern[i] == pattern[x], 即pattern[i] == pattern[next[i]], 则: next[i + 1] = x + 1\n
*
* 因此:\n
* &emsp;&emsp; 令next[1] = next[0] + 1 = 0\n
* 得:\n
* &emsp;&emsp; next[0] = -1\n\n
* 2. <span style="color:#003153">demo</span>\n
* 遍历至i = 8, 此时starting_index = 2, 即<span style="color:#FF8100">next[8] = 2</span>\n
* ```
* a b c d 5 6 a b c d 7
* a b
* a b
* ^
* |
* |
*
* ```
* 由于: \n
* &emsp; pattern[i] == pattern[starting_index] = 'c'\n
* 因此: \n
* &emsp; i加1, i = 9;\n
* &emsp; starting_index加1, starting_index = 3\n
* &emsp; next[i] = starting_index, 即<span style="color:#D40000">next[9] = 3</span>\n
* ```
* a b c d 5 6 a b c d 7
* a b c
* a b c
* ^
* |
* |
*
* ```
*
* -----------------------
* + **1 初始化next数组**\n
* next分配内存并初始化\n
* next[0] 设为 -1\n\n
* + **2 模式串长度为1的情况处理**\n
* **if** 模式串长度为1 :\n
* &emsp; 直接返回next <span style="color:#FF8100">(next[0]已经设置完毕)</span>\n\n
* + **3 设置next[1]**\n
* next[1] 设为 0\n\n
* + **4 完成next数组设置**\n
* 初始化i <span style="color:#FF8100">(模式串遍历索引)</span>为1\n
* 初始化starting_index为0\n\n
* **while loop** 遍历模式串 :\n
* &emsp; **if** 索引i和索引starting_index两个位置的字符相同 :\n
* &emsp;&emsp; i向后移动1位\n
* &emsp;&emsp; starting_index向后移动1位\n
* &emsp;&emsp; next[i] <-- starting_index\n
* &emsp; **else**\n
* &emsp;&emsp; **if** starting_index == 0 <span style="color:#FF8100">(如果索引i字符发生失配,只能从头重新进行匹配)</span>:\n
* &emsp;&emsp;&emsp; i向后移动1位\n
* &emsp;&emsp;&emsp; next[i] <-- 0\n
* &emsp;&emsp; **else**\n
* &emsp;&emsp;&emsp; starting_index <-- next[starting_index]\n\n
* + **5 退出函数**\n
* 返回next数组\n
*
*
* -----------------------
*/
int* String::KmpNextByCyberDash() const {
// ---------- 1 初始化next数组 ----------
int* next = new int[this->length_]; // next分配内存并初始化
next[0] = -1; // next[0] 设为 -1
// ---------- 2 模式串长度为1的情况处理 ----------
if (length_ == 1) { // if 模式串长度为1
return next; // 直接返回next(next[0]已经设置完毕)
}
// ---------- 3 设置next[1] ----------
next[1] = 0; // next[1] 设为 0
// ---------- 4 完成next数组设置 ----------
int i = 1; // 初始化i(模式串遍历索引)为1
int starting_index = 0; // 初始化starting_index为0
while (i < length_) { // while loop 遍历模式串
if (this->mem_data_[i] == this->mem_data_[starting_index]) { // if 索引i和索引starting_index两个位置的字符相同
i++; // i向后移动1位
starting_index++; // starting_index向后移动1位
next[i] = starting_index; // next[i] <-- starting_index
} else { // else
if (starting_index == 0) { // if starting_index == 0 (如果索引i字符发生失配,只能从头重新进行匹配)
i++; // i向后移动1位
next[i] = 0; // next[i] <-- 0
} else { // else
starting_index = next[starting_index]; // starting_index <-- next[starting_index]
}
}
}
// ---------- 5 退出函数 ----------
return next; // 返回next数组
}
/*!
* @brief **KMP字符串匹配(Cyberdash版)**
* @param pattern 模式串
* @param offset 目标串偏移量
* @return 匹配位置索引
* @note
* KMP字符串匹配(Cyberdash版)
* ------------------------
* ------------------------
*
* 返回值为-1, 则为不匹配
*
* ------------------------
* + **1 生成模式串的next数组**\n
* pattern调用KmpNextByCyberDash, 获取next数组\n
* **if** next为NULL :\n
* &emsp; 抛出bad_alloc()\n\n
* + **2 使用next数组执行匹配**\n
* 初始化pattern_idx<span style="color:#FF8100">(模式串遍历索引)</span>为0\n
* 初始化target_idx<span style="color:#FF8100">(目标串遍历索引)</span>为offset\n\n
* **while loop** 模式串未遍历完 <b>&&</b> 目标串未遍历完 :\n
* &emsp; **if** 当前模式串字符 == 当前目标串字符 :\n
* &emsp;&emsp; pattern_idx向后移动1位\n
* &emsp;&emsp; target_idx向后移动1位\n
* &emsp; **else** (当前模式串字符 != 当前目标串字符) :\n
* &emsp;&emsp; **if** 模式串首个字符不匹配 :\n
* &emsp;&emsp;&emsp; target_idx向后移动1位\n
* &emsp;&emsp; **else** (模式串非首个字符不匹配) :\n
* &emsp;&emsp;&emsp; pattern_idx <--- next[pattern_idx]\n\n
* + **3 计算首字符匹配的索引**\n
* 初始化match_idx为-1\n
* **if** pattern_idx等于模式串长度(成功匹配) :\n
* &emsp; match_idx <--- target_idx - pattern.Length()\n\n
* + **4 返回结果**\n
* 返回match_idx\n
*
*
* ------------------------
*/
int String::KmpMatchByCyberdash(const String& pattern, int offset) const {
// ---------- 1 生成模式串的next数组 ----------
int* next = pattern.KmpNextByCyberDash(); // pattern调用KmpNext, 获取next数组
if (!next) { // if next为NULL
throw bad_alloc(); // 抛出bad_alloc()
}
// ---------- 2 使用next数组执行匹配 ----------
int pattern_idx = 0; // 初始化pattern_idx(模式串遍历索引)为0
int target_idx = offset; // 初始化target_idx(目标串遍历索引)为offset
while (pattern_idx < pattern.Length() && target_idx < this->length_) { // while loop 模式串未遍历完 && 目标串未遍历完
if (pattern[pattern_idx] == this->mem_data_[target_idx]) { // if 当前模式串字符 == 当前目标串字符
pattern_idx++; // pattern_idx向后移动1位
target_idx++; // target_idx向后移动1位
} else { // else (当前模式串字符 != 当前目标串字符)
if (pattern_idx == 0) { // if 模式串首个字符不匹配
target_idx++; // target_idx向后移动1位
} else { // else (模式串非首个字符不匹配)
pattern_idx = next[pattern_idx]; // pattern_idx <--- next[pattern_idx]
}
}
}
// ---------- 3 计算首字符匹配的索引 ----------
int match_idx = -1; // 初始化match_idx为-1
if (pattern_idx == pattern.Length()) { // if pattern_idx等于模式串长度(成功匹配)
match_idx = target_idx - pattern.Length(); // match_idx <--- target_idx - pattern.Length()
}
// ---------- 4 返回结果 ----------
return match_idx; // 返回match_idx
}
/*!
* @brief **KMP字符串匹配查找**
* @param pattern 模式串
* @param offset 目标串的起始偏移量
* @return 匹配位置索引
* @note
* KMP字符串匹配查找
* --------------
* --------------
*
* --------------
* + **1 生成模式串的next数组**\n
* pattern调用KmpNext, 获取next数组\n
* **if** next为NULL :\n
* &emsp; 抛出bad_alloc()\n\n
* + **2 使用next数组执行匹配**\n
* 初始化pattern_idx<span style="color:#FF8100">(模式串遍历索引)</span>为0\n
* 初始化target_idx<span style="color:#FF8100">(目标串遍历索引)</span>为offset\n\n
* **while loop** 模式串未遍历完 <b>&&</b> 目标串未遍历完 :\n
* &emsp; **if** 当前模式串字符 == 当前目标串字符 <b>||</b> 当前模式串字符 == 当前目标串字符 :\n
* &emsp;&emsp; pattern_idx向后移动1位\n
* &emsp;&emsp; target_idx向后移动1位\n
* &emsp; **else**\n
* &emsp;&emsp; pattern_idx <--- next[pattern_idx]\n\n
* + **3 计算首字符匹配的索引**\n
* 初始化match_idx为-1\n
* **if** pattern_idx等于模式串长度(成功匹配) :\n
* &emsp; match_idx <--- target_idx - pattern.Length()\n\n
* + **4 返回结果**\n
* 返回match_idx\n
*
*
* --------------
*/
int String::KmpMatch(const String& pattern, int offset) const {
// ---------- 1 生成模式串的next数组 ----------
int* next = pattern.KmpNext(); // pattern调用KmpNext, 获取next数组
if (!next) { // if next为NULL
throw bad_alloc(); // 抛出bad_alloc()
}
// ---------- 2 使用next数组执行匹配 ----------
int pattern_idx = 0; // 初始化pattern_idx(模式串遍历索引)为0
int target_idx = offset; // 初始化target_idx(目标串遍历索引)为offset
while (pattern_idx < pattern.Length() && target_idx < length_) { // while loop 模式串未遍历完 && 目标串未遍历完
if (pattern_idx == -1 || pattern[pattern_idx] == mem_data_[target_idx]) { // if 当前模式串字符 == 当前目标串字符 || 当前模式串字符 == 当前目标串字符
pattern_idx++; // pattern_idx向后移动1位
target_idx++; // target_idx向后移动1位
} else { // else
pattern_idx = next[pattern_idx]; // pattern_idx <--- next[pattern_idx]
}
}
// ---------- 3 计算首字符匹配的索引 ----------
int match_idx = -1; // 初始化match_idx为-1
if (pattern_idx == pattern.Length()) { // if pattern_idx等于模式串长度(成功匹配)
match_idx = target_idx - pattern.Length(); // match_idx <--- target_idx - pattern.Length()
}
// ---------- 4 返回结果 ----------
return match_idx; // 返回match_idx
}
#endif // CYBER_DASH_STRING_H
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

通用的C++数据结构代码实现,使用模板。代码完整,注释齐全,可直接运行,可使用doxygen生成网页和PDF文档,跨Windows/Linux/Mac平台兼容
暂无标签
MulanPSL-2.0
使用 MulanPSL-2.0 开源许可协议
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/getma/data-structures-cpp.git
git@gitee.com:getma/data-structures-cpp.git
getma
data-structures-cpp
数据结构(C++模板实现)
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /