数据结构-1
补充线性表的串,
串,一般指字符串,(string),
子串:被另一个串所包含的串,
主串:与子串对应。一般说某子串的主串,
空串:"";//十分形象
空格串:“ ”或" ";
串相等:""=""或“1”=“1”或“12”=“12”;
模式匹配:对于一个子串在主串中的位置定位。
存储方式:<数组紧缩,数组非紧缩>(顺序存储结构),链表存储,指针+堆存储...
操作集:求长度GetLength,连接串CancatStr, 求子串SubStr, 比较串EqualStr, 查找子串 IndexStr,插入串InsertStr, 删除串DeleteStr...
顺序存储结构:(下面这俩都属于顺序存储结构)
数组紧缩 :一个字符贴着另外一个字符(难以定位)(c语言是这种模式)
数组非紧缩 :每个字符4个字节(浪费空间)(顺序存储结构,要求掌握)
ps:在这里可以看到,很多时候内存空间和处理时间是矛盾的,但是可以在更需要时间的时候,用空间来交换,反之亦然,很多算法都是建立在,以空间来换时间的基础之上的。
由于紧缩比较常用,也比较难,我就只实现紧缩的操作集:
#include <iostream> using namespace std; struct Queue { int arr[5]; int maxlen; int left;//队列尾巴 int right;//队列头 int len;//循环队列很多内容都可以通过len属性来减少很多不必要的计算 void Init() { maxlen = 5; left = 0; right = -1; len = 0; } bool IsNull() { if (len==0)return true; return false; } bool IsFull() { if (len == maxlen)return true; return false; } bool Enqueque(int num) { bool rt=false; if (!IsFull())//一定不能满 { right++; arr[right%maxlen] = num; len++; rt = true; } return rt; } int Dequeque() { int rt = 0x7fffffff; if (!IsNull())//一定不能是空的 { rt = arr[left % maxlen]; len--; left++; } return rt; } void Show() { for (int i=0; i < len ; i++) { cout<< arr[(left+i)%maxlen]<< " "; } cout << endl; } }; int main() { Queue q; q.Init(); q.Enqueque(1); q.Show(); q.Enqueque(2); q.Show(); q.Enqueque(3); q.Show(); q.Enqueque(4); q.Show(); q.Enqueque(5); q.Show(); cout << "入队6" << endl; q.Enqueque(6); q.Show(); cout << "出队" << q.Dequeque() << endl; q.Show(); cout << "出队" << q.Dequeque() << endl; q.Show(); cout << "出队" << q.Dequeque() << endl; q.Show(); cout << "出队" << q.Dequeque() << endl; q.Show(); q.Enqueque(7); cout << "入队7" << endl; q.Show(); cout << "出队" << q.Dequeque() << endl; q.Show(); return 0; }
有点小看KMP算法的难度,不过这也怪我通过搜索引擎搜到的资料代码太雷同了,已经是重度优化的代码,
啃起来真的很难理解,,,这里我在理解含义后,用自己的理解写了一次没有怎么优化的KMP算法。
本来还想直接忽略串这个内容的,现在啃完KMP觉得获益匪浅,还好没有跳过。

浙公网安备 33010602011771号