本周总结(7.13-7.19)(数据结构)


本周主要复习单链表,双链表,栈,队列,单调栈,单调队列,KMP,Trie,并查集,堆,哈希表,树状数组和线段树等内容。

算法总结

单链表

int idx, ne[N], e[N], h;

void add_h(int x) //头结点插入
{
    e[idx] = x;
    ne[idx] = h;
    h = idx ++ ;
}

void insert(int k, int x) //第k个后插入
{
    e[idx] = x;
    ne[idx] = ne[k];
    ne[k] = idx ++ ;
}

void re(int k) //删除第k个
{
    ne[k] = ne[ne[k]];
}

单调栈

常见模型:找出每个数左边离它最近的比它大/小的数

int tt = 0, stk[N];
for (int i = 1; i <= n; i ++ )
{
    while (tt && check(stk[tt], i)) tt -- ;
    stk[ ++ tt] = i;
}

单调队列

常见模型:找出滑动窗口中的最大值/最小值

int hh = 0, tt = -1, q[N];
for (int i = 0; i < n; i ++ )
{
    while (hh <= tt && check_out(q[hh])) hh ++ ;  // 判断队头是否滑出窗口
    while (hh <= tt && check(q[tt], i)) tt -- ;
    q[ ++ tt] = i;
}

KMP

求next数组
ne[i]表示以i结尾的字符串,前缀和后缀长度为ne[i]的子串相等,且该子串的长度最大

void cal_next(char a[]) //a从下标1开始记录
{
    for (int i = 0, j = 2; j <= n; j ++ ) //j指针从2开始,因为ne[1] = 0
    {
        while (i && a[i + 1] != a[j]) i = ne[i];
        if (a[i + 1] == a[j]) i ++ ;
        ne[j] = i;
    }
}

Trie

int tr[N][M]; N为字符串的总长度,M为可能出现的字符种数

void insert(char *str) // 插入一个字符串
{
    int p = 0;
    for (int i = 0; str[i]; i ++ )
    {
        int u = str[i] - 'a';
        if (!tr[p][u]) tr[p][u] = ++ idx;
        p = tr[p][u];
    }
    cnt[p] ++ ; //记录结束位置
}
int query(char *str) // 查询字符串出现的次数
{
    int p = 0;
    for (int i = 0; str[i]; i ++ )
    {
        int u = str[i] - 'a';
        if (!tr[p][u]) return 0;
        p = tr[p][u];
    }
    return cnt[p];
}

并查集

朴素并查集

int p[N]; //存储每个点的祖宗节点

int find(int x) // 返回x的祖宗节点
{
	if (p[x] != x) p[x] = find(p[x]);
	return p[x];
}

for (int i = 1; i <= n; i ++ ) p[i] = i; // 初始化,假定节点编号是1~n

p[find(a)] = find(b); // 合并a和b所在的两个集合:

维护size并查集

int p[N], size[N];
//p数组存储每个点的祖宗节点, size[]只有祖宗节点的有意义,表示祖宗节点所在集合中的点的数量

int find(int x) // 返回x的祖宗节点
{
	if (p[x] != x) p[x] = find(p[x]);
	return p[x];
}

for (int i = 1; i <= n; i ++ ) // 初始化,假定节点编号是1~n
{
	p[i] = i;
	size[i] = 1;
}

size[find(b)] += size[find(a)]; // 合并a和b所在的两个集合:
p[find(a)] = find(b);

哈希表

拉链法

int h[N], e[N], ne[N], idx;

void insert(int x) //插入数据
{
    int k = (x % N + N) % N; //哈希函数,N一般取100003
    e[idx] = x;
    ne[idx] = h[k];
    h[k] = idx ++ ;
}

bool find(int x) //查找数据
{
    int k = (x % N + N) % N;
    for (int i = h[k]; i != -1; i = ne[i])
    {
        if (e[i] == x) return true;
    }
    return false;
}

字符串哈希

求哈希值

for (int i = 1; i <= n; i ++ ) //s为字符串,P为进制,一般取131
{
	p[i] = p[i - 1] * P; //各数位上的进位
	h[i] = h[i - 1] * P + s[i]; //哈希值
}

返回指定区间的哈希值,若两区间哈希值相等,说明字符串相等

ull get(int l, int r)
{
    return h[r] - h[l - 1] * p[r - l + 1];
}

树状数组

int tr[N];

int lowbit(int x)
{
    return x & -x;
}

void add(int a, int b)
{
    for (int i = a; i <= n; i += lowbit(i))
        tr[i] += b;
}

int query(int a)
{
    int res = 0;
    for (int i = a; i; i -= lowbit(i))
        res += tr[i];
    return res;
}

其它总结

  1. map
    map是一个关联容器,它提供一对一的hash。
    第一个可以称为关键字(key),每个关键字只能在map中出现一次;
    第二个可能称为该关键字的值(value)。
    主要用法:
    插入: 通过下标mp[i] = i; 通过insert函数mp.insert(make_pair(i, i));
    读取和查找元素: 通过key值读取mp[key] 通过count(k)find(k)分别返回k出现的次数和指向该元素的迭代器。
    删除: 通过erase函数 erase(k)k为key值或迭代器。
  2. 用printf输出string类型时,需要用c_str()函数将其转换为char*类型。
  3. reverse(s.begin() + i, s.begin() + i + k);函数翻转部分序列时,可看作翻转从下标为i到下标为i + k - 1之间的元素。
  4. map和unordered_map的区别
mapunordered_map
排序
实施二叉搜索树哈希表
查找O(logn)平均O(1),最坏O(n)
插入O(logn) + 再平衡时间同查找
用处需要一组排序后的数据只需记录数据而不需排序
  1. 注意检查语句顺序。
  2. scanf读入字符数组时,注意长度+1,末尾有结束符。
  3. strcmp(str1, str2) == 0判断两个字符数组是否相等,等于0则相等。
  4. %*s表示跳过一个字符串,不做读入处理。
  5. 领接表的定义:vector<vector<int>> g; 二维vector,使用前先用resize确定大小或在定义时确定大小。

本周拓展题

单调栈:Acwing600
单调队列:Acwing652
并查集:Acwing914
树状数组: Acwing1267
线段树: Acwing 245
tire树: Acwing161
单链表 :Acwing1560
kmp:Acwing141
哈希表:Acwing1523

使用Oracle 设计DB存储如下xml 中数据,<msgBody><msgId>AMAT_1A19Y_02829.C00666.UR0ER6.20221020130718</msgId><eqpId>ASSORA-01</eqpId><carrierId>C00666</carrierId><runMode>Y</runMode><batchID>AMAT_1A19Y_02829</batchID><eapUserId>EAPAUTO</eapUserId><fabId>ZH</fabId><function>WAFERSTART</function><sorterAction>WAFERSTART</sorterAction><actionFlag>T</actionFlag><slotMapping>018,UR0ER6.18,C00666,001,ZA0162T.01,C00682;007,UR0ER7.07,C00674,002,ZA0162T.02,C00682;008,UR0ER7.08,C00674,003,ZA0162T.03,C00682;009,UR0ER7.09,C00674,004,ZA0162T.04,C00682;010,UR0ER7.10,C00674,005,ZA0162T.05,C00682</slotMapping><carrierArriveTime>2022-10-20 14:59:50</carrierArriveTime><clampTime>2022-10-20 15:00:32</clampTime><loadStartTime>2022-10-20 15:10:33</loadStartTime><loadCompleteTime>2022-10-20 15:20:33</loadCompleteTime><unloadCompleteTime>2022-10-20 15:40:40</unloadCompleteTime><carrierRemoveTime>2022-10-20 17:00:33</carrierRemoveTime><carrierList><carrierID>C00666</carrierID><carrierType>P</carrierType><lotIDList><lotID>UR0ER6</lotID></lotIDList></carrierList><carrierList><carrierID>C00674</carrierID><carrierType>P</carrierType><lotIDList><lotID>UR0ER7</lotID></lotIDList></carrierList><carrierList><carrierID>C00682</carrierID><carrierType>C</carrierType><lotIDList><lotID>ZA0162T.01</lotID></lotIDList></carrierList><sorterJobList><jobID>WAFERSTART-C00666-C00682</jobID><lotIDList><lotID>UR0ER6</lotID><lotID>ZA0162T.01</lotID></lotIDList><origLP>LP1</origLP><destnLP>LP2</destnLP><transferSlotMap><fromTo>018-001</fromTo></transferSlotMap><origCarrier><carrierID>C00666</carrierID><lotIDList><lotID>UR0ER6</lotID></lotIDList><category>C</category><carrierType>C</carrierType><location/><mesMapping>0000000000000000011110000</mesMapping><pSlotInfo><slotList><slotId>018</slotId><waferId>UR0ER6.18</waferId></slotList></pSlotInfo><cSlotInfo><slotList><slotId>019</slotId><waferId>UR0ER6.19</waferId></slotList><slotList><slotId>020</slotId><waferId>UR0ER6.20</waferId></slotList><slotList><slotId>021</slotId><waferId>UR0ER6.21</waferId></slotList></cSlotInfo></origCarrier><destnCarrier><carrierID>C00682</carrierID><lotIDList><lotID>ZA0162T.01</lotID></lotIDList><category>C</category><carrierType>C</carrierType><location>WS</location><mesMapping>0000000000000000000000000</mesMapping><pSlotInfo/><cSlotInfo><slotList><slotId>001</slotId><waferId>ZA0162T.01</waferId></slotList><slotList><slotId>002</slotId><waferId>ZA0162T.02</waferId></slotList><slotList><slotId>003</slotId><waferId>ZA0162T.03</waferId></slotList><slotList><slotId>004</slotId><waferId>ZA0162T.04</waferId></slotList><slotList><slotId>005</slotId><waferId>ZA0162T.05</waferId></slotList></cSlotInfo></destnCarrier></sorterJobList><sorterJobList><jobID>WAFERSTART-C00674-C00682</jobID><lotIDList><lotID>UR0ER7</lotID><lotID>ZA0162T.01</lotID></lotIDList><origLP>LP1</origLP><destnLP>LP2</destnLP><transferSlotMap><fromTo>007-002</fromTo><fromTo>008-003</fromTo><fromTo>009-004</fromTo><fromTo>010-005</fromTo></transferSlotMap><origCarrier><carrierID>C00674</carrierID><lotIDList><lotID>UR0ER7</lotID></lotIDList><category>C</category><carrierType>C</carrierType><location>RR</location><mesMapping>000000111111111111110000</mesMapping><pSlotInfo><slotList><slotId>007</slotId><waferId>UR0ER7.07</waferId></slotList><slotList><slotId>008</slotId><waferId>UR0ER7.09</waferId></slotList><slotList><slotId>009</slotId><waferId>UR0ER7.09</waferId></slotList><slotList><slotId>010</slotId><waferId>UR0ER7.10</waferId></slotList></pSlotInfo><cSlotInfo><slotList><slotId>011</slotId><waferId>UR0ER7.11</waferId></slotList><slotList><slotId>012</slotId><waferId>UR0ER7.12</waferId></slotList><slotList><slotId>013</slotId><waferId>UR0ER7.13</waferId></slotList><slotList><slotId>014</slotId><waferId>UR0ER7.14</waferId></slotList><slotList><slotId>015</slotId><waferId>UR0ER7.15</waferId></slotList><slotList><slotId>016</slotId><waferId>UR0ER7.16</waferId></slotList><slotList><slotId>017</slotId><waferId>UR0ER7.17</waferId></slotList><slotList><slotId>018</slotId><waferId>UR0ER7.18</waferId></slotList><slotList><slotId>019</slotId><waferId>UR0ER7.19</waferId></slotList><slotList><slotId>020</slotId><waferId>UR0ER7.20</waferId></slotList></cSlotInfo></origCarrier><destnCarrier><carrierID>C00682</carrierID><lotIDList><lotID>ZA0162T.01</lotID></lotIDList><category>C</category><carrierType>C</carrierType><location>WS</location><mesMapping>0000000000000000000000000</mesMapping><pSlotInfo/><cSlotInfo><slotList><slotId>001</slotId><waferId>ZA0162T.01</waferId></slotList><slotList><slotId>002</slotId><waferId>ZA0162T.02</waferId></slotList><slotList><slotId>003</slotId><waferId>ZA0162T.03</waferId></slotList><slotList><slotId>004</slotId><waferId>ZA0162T.04</waferId></slotList><slotList><slotId>005</slotId><waferId>ZA0162T.05</waferId></slotList></cSlotInfo></destnCarrier></sorterJobList><lotInfo><lotId>UR0ER6</lotId><CarrierID>C00666</CarrierID><lotType>Material</lotType><trackingUnit>Lot</trackingUnit><productId>SPAW000001.0</productId><stageId>00146000M1-WAT1</stageId><planId/><planType/><subPlanId/><subPlanType/><stepSeq/><stepId/><stepType/><stepDesc/><slotMap>0000000000000000011110000</slotMap><ppParameter/><isMonitor>N</isMonitor><processState>WaitForConsume</processState><processingStatus>Inventory</processingStatus><isEngJIP>F</isEngJIP><lotPriority>1</lotPriority><lotStartDate>2022-06-01 14:00:00</lotStartDate><lotContaminationLevel>3</lotContaminationLevel><carrierContaminationFlag>C</carrierContaminationFlag><angle/><mesBatchId>MESBatchID1111</mesBatchId><jobPrepOPId>User123</jobPrepOPId><jobPrepType>Auto</jobPrepType><jobInTime>2022-10-20 15:15:30</jobInTime><lotStartTime>2022-10-20 16:30:33</lotStartTime><lotEndTime>2022-10-20 16:50:33</lotEndTime><jobOutTime>2022-10-20 16:57:38</jobOutTime></lotInfo><lotInfo><lotId>UR0ER7</lotId><CarrierID>C00674</CarrierID><lotType>Material</lotType><trackingUnit>Lot</trackingUnit><productId>SPAW000001.0</productId><stageId>00146000M1-WAT1</stageId><planId/><planType/><subPlanId/><subPlanType/><stepSeq/><stepId/><stepType/><stepDesc/><slotMap>0000001111111111111100000</slotMap><ppParameter/><isMonitor>N</isMonitor><processState>WaitForConsume</processState><processingStatus>Inventory</processingStatus><isEngJIP>F</isEngJIP><lotPriority>1</lotPriority><lotStartDate>2022-06-01 14:00:00</lotStartDate><lotContaminationLevel>3</lotContaminationLevel><carrierContaminationFlag>C</carrierContaminationFlag><angle/><mesBatchId>MESBatchID1111</mesBatchId><jobPrepOPId>User123</jobPrepOPId><jobPrepType>Auto</jobPrepType><jobInTime>2022-10-20 15:15:30</jobInTime><lotStartTime>2022-10-20 16:30:33</lotStartTime><lotEndTime>2022-10-20 16:50:33</lotEndTime><jobOutTime>2022-10-20 16:57:38</jobOutTime></lotInfo><lotInfo><lotId>ZA0162T.01</lotId><CarrierID>C00682</CarrierID><lotType>ZL</lotType><trackingUnit>Lot</trackingUnit><productId>EH0650A06B.0</productId><stageId>APSORT11.0</stageId><planId>AEEH0650A02.0</planId><planType>Normal</planType><subPlanId>AEEH0650AGAN03.0</subPlanId><subPlanType>Normal</subPlanType><stepSeq>000100.10.0100</stepSeq><stepId>APSORT11.0</stepId><stepType>ScheduledSorter</stepType><stepDesc/><slotMap>0000001111111111111100000</slotMap><ppParameter/><isMonitor>N</isMonitor><processState>WaitForJobPrep</processState><processingStatus>Active</processingStatus><isEngJIP>F</isEngJIP><lotPriority>1</lotPriority><lotStartDate>2022-06-01 14:00:00</lotStartDate><lotContaminationLevel>3</lotContaminationLevel><carrierContaminationFlag>C</carrierContaminationFlag><angle/><mesBatchId>MESBatchID1111</mesBatchId><jobPrepOPId>User123</jobPrepOPId><jobPrepType>Auto</jobPrepType><jobInTime>2022-10-20 15:15:30</jobInTime><lotStartTime>2022-10-20 16:30:33</lotStartTime><lotEndTime>2022-10-20 16:50:33</lotEndTime><jobOutTime>2022-10-20 16:57:38</jobOutTime></lotInfo></msgBody>
07-25
为了设计一个适用于存储 XML 数据内容的 Oracle 数据库表结构,特别是针对 `msgBody` 中的元素如 `msgId`, `eqpId`, `carrierId`, `lotInfo`, 和 `sorterJobList`,可以考虑以下设计方法: ### 数据表设计 #### 1. **主消息表 (`msg_main`)** 该表用于存储消息的主记录,包括基本的标识符和元数据。 ```sql CREATE TABLE msg_main ( msg_id VARCHAR2(50) PRIMARY KEY, eqp_id VARCHAR2(50), carrier_id VARCHAR2(50), lot_info VARCHAR2(100), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); ``` - `msg_id`: 消息唯一标识符,作为主键。 - `eqp_id`: 设备标识符。 - `carrier_id`: 载具标识符。 - `lot_info`: 批次信息。 - `created_at`: 消息创建时间,默认值为当前时间。 #### 2. **排序任务列表表 (`sorter_job_list`)** 由于 `sorterJobList` 是一个列表结构,可以将其拆分为一个单独的表,并通过外键关联到 `msg_main` 表。 ```sql CREATE TABLE sorter_job_list ( job_id VARCHAR2(50) PRIMARY KEY, msg_id VARCHAR2(50), job_type VARCHAR2(50), priority NUMBER, FOREIGN KEY (msg_id) REFERENCES msg_main(msg_id) ); ``` - `job_id`: 排序任务唯一标识符,作为主键。 - `msg_id`: 外键,关联到 `msg_main` 表的消息标识符。 - `job_type`: 排序任务类型。 - `priority`: 任务优先级。 #### 3. **XML 数据存储表 (`xml_data_store`)** 如果需要存储完整的 XML 数据作为原始记录,可以使用 `XMLType` 数据类型来存储 XML 文档。 ```sql CREATE TABLE xml_data_store ( msg_id VARCHAR2(50) PRIMARY KEY, xml_content XMLType, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); ``` - `msg_id`: 消息唯一标识符,作为主键。 - `xml_content`: 存储完整的 XML 数据,使用 `XMLType` 类型。 - `created_at`: 记录插入时间,默认值为当前时间。 ### 数据库设计优势 - **规范化设计**: 通过将 `sorterJobList` 拆分为独立表,避免了数据冗余,同时支持多对一关系。 - **XML 数据存储**: 使用 `XMLType` 数据类型可以高效地存储和查询 XML 数据,Oracle 提供了丰富的 XML 操作函数,如 `extractValue` 和 `existsNode`,便于后续的数据处理。 - **扩展性**: 如果未来需要添加更多的消息元素或子列表,可以通过新增表或字段轻松扩展。 ### 查询示例 #### 查询消息及其所有排序任务 ```sql SELECT m.msg_id, m.eqp_id, m.carrier_id, m.lot_info, j.job_id, j.job_type, j.priority FROM msg_main m LEFT JOIN sorter_job_list j ON m.msg_id = j.msg_id WHERE m.msg_id = 'MSG123456'; ``` #### 查询 XML 数据中特定元素 假设需要从 `xml_content` 字段中提取 `eqpId` 元素的值: ```sql SELECT msg_id, xml_content.extract('//eqpId/text()').getStringVal() AS eqp_id FROM xml_data_store WHERE msg_id = 'MSG123456'; ``` ### 总结 通过上述设计,可以有效地存储和管理 XML 数据中的 `msgBody` 元素。主消息表 `msg_main` 用于存储核心消息信息,`sorter_job_list` 表用于存储 `sorterJobList` 列表数据,而 `xml_data_store` 表则用于存储完整的 XML 原始数据。这种设计兼顾了数据的结构化存储与非结构化数据的灵活性,便于后续的数据查询和处理。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值