1032. Sharing (25)

本文介绍了一种使用链表进行数据存储的方法,并通过构建链表来查找两个链表中的共同节点。主要实现了链表的创建、遍历、释放及相同子链表的搜索功能。

考察链表以及相同子链表的搜索记录

#include<iostream>
#include<map>
#include<vector>
#include<set>

typedef struct Node
{
	int adress;
	Node* next;
	Node(int _a = 0, Node* _next = NULL):adress(_a),next(_next){};
}Node, *PNode;

void ReleaseList(Node* list)
{
	while(list)
	{
		Node* pNext = list->next;
		delete list;
		list = pNext;
	}
}

int FindCommonIndex(Node* list1, Node* list2)
{
	int res = -1;
	std::set<int> set;
	while(list1)
	{
		set.insert(list1->adress);
		list1 = list1->next;
	}
	std::set<int>::iterator it;
	while(list2)
	{
		it=set.find(list2->adress);
		if(it != set.end())
		{
			res = list2->adress;
			break;
		}
		list2 = list2->next;
	}
	return res;
}
void BuildList(Node* &list, std::map<int, int>& map)
{
	std::map<int, int>::iterator it;
	int key = list->adress;
	it=map.find(key);
	Node** p = &list->next;
	while(it != map.end())
	{
		(*p) = new Node;
		(*p)->adress = (*it).second;
		key = (*p)->adress;
		it=map.find(key);
		p = &(*p)->next;
	}
}
int main()
{
	int s1, s2, n;
	while( scanf("%d%d%d", &s1, &s2, &n) != EOF )
	{
		int a, b;
		char c;
		std::map<int, int> map;
		for(int i = 0; i < n; ++i)
		{
			scanf("%d%*c%c%d", &a, &c, &b);
			map[a] = b;
		}
		Node* list1 = new Node;
		list1->adress = s1;
		Node* list2 = new Node;
		list2->adress = s2;
		//build list
		BuildList(list1, map);
		BuildList(list2, map);
		//find common index
		int comIdx = FindCommonIndex(list1, list2);
		//release
		ReleaseList(list1);
		ReleaseList(list2);

		if(comIdx == -1)
			printf("-1\n");
		else 
			printf("%05d\n", comIdx);
	}
	return 0;
}


 

你的问题非常清晰,我们来逐条分析你这个 SQL 的逻辑,并解释为什么**有些 `sharing_scope` 为空的记录没有被更新**。 --- ## ✅ 你的 SQL 语句回顾 ```sql UPDATE testdup.dynamic_general_ledger dgl SET sharing_scope = COALESCE( SUBSTRING( ctn.sharing_scope FROM '(文档安全等级为A)([^(]*)厂长[^(]*((文档安全等级为[BC])[^(]*)*' ), '' ) FROM testdup."Category_Tree_Node" ctn WHERE dgl.file_type = ctn.category_name AND dgl.sharing_scope IS NULL AND ctn.sharing_scope ~ '(文档安全等级为A)'; ``` --- ## ✅ WHERE 条件解释 你问: > **这个 WHERE 条件是什么意思?为啥有些 sharing_scope 是空的没被更新?** ### 📌 条件详解: ```sql WHERE dgl.file_type = ctn.category_name ``` - 表示 `dynamic_general_ledger` 表中的 `file_type` 字段要与 `Category_Tree_Node` 表中的 `category_name` 相等。 - 这是两个表的连接条件。 ```sql AND dgl.sharing_scope IS NULL ``` - 只更新那些 `dynamic_general_ledger` 表中 `sharing_scope` **为空** 的记录。 ```sql AND ctn.sharing_scope ~ '(文档安全等级为A)' ``` - 表示只选择 `ctn.sharing_scope` 中**包含“(文档安全等级为A)”字符串** 的记录。 - 这是正则匹配,`~` 是 PostgreSQL 中的正则匹配操作符。 --- ## ❗ 你遇到的问题分析 > **“有些 sharing_scope 是空的但没被更新”**,说明: - 这些记录满足 `dgl.sharing_scope IS NULL` - 但是 **没有满足 `ctn.sharing_scope ~ '(文档安全等级为A)'`** - 或者 **没有和 `ctn` 表中 `category_name = file_type` 的记录匹配上** --- ## ✅ 举个例子说明 | dynamic_general_ledger (dgl) | Category_Tree_Node (ctn) | |-----------------------------|---------------------------| | file_type = '报告' | category_name = '报告' | | sharing_scope = NULL | sharing_scope = '普通共享'(不含“文档安全等级为A”) | 这个记录就不会被更新,因为虽然 `dgl.sharing_scope IS NULL`,但 `ctn.sharing_scope` 不包含“文档安全等级为A”。 --- ## ✅ 你可能没注意到的几个点 | 问题 | 说明 | |------|------| | 1. `ctn.sharing_scope` 没有“文档安全等级为A” | 不会匹配正则表达式 | | 2. `file_type` 和 `category_name` 不匹配 | 不会连接上,自然不会更新 | | 3. `ctn.sharing_scope` 虽然有内容但不满足正则 | 即使 dgl.sharing_scope 是 NULL,也不会更新 | | 4. `COALESCE(SUBSTRING(...), '')` 可能也返回空 | 即使匹配了,正则没提取到内容,也可能更新为空字符串 | --- ## ✅ 如何排查哪些记录没有被更新? 你可以先用 `SELECT` 查看将要更新的数据范围: ```sql SELECT dgl.id, dgl.file_type, dgl.sharing_scope AS dgl_sharing_scope, ctn.category_name, ctn.sharing_scope AS ctn_sharing_scope, SUBSTRING( ctn.sharing_scope FROM '(文档安全等级为A)([^(]*)厂长[^(]*((文档安全等级为[BC])[^(]*)*' ) AS extracted_sharing_scope FROM testdup.dynamic_general_ledger dgl JOIN testdup."Category_Tree_Node" ctn ON dgl.file_type = ctn.category_name WHERE dgl.sharing_scope IS NULL AND ctn.sharing_scope ~ '(文档安全等级为A)'; ``` 这样你可以看到: - 哪些记录满足条件; - 哪些记录虽然满足 `dgl.sharing_scope IS NULL`,但没满足 `ctn` 表的条件; - 正则是否提取到了内容。 --- ## ✅ 总结 | 条件 | 是否满足更新 | |------|---------------| | `dgl.file_type = ctn.category_name` ✅ | 必须匹配 | | `dgl.sharing_scope IS NULL` ✅ | 必须为空 | | `ctn.sharing_scope ~ '(文档安全等级为A)'` ✅ | 必须满足正则 | | `substring` 提取内容为空 ❌ | 更新为空字符串,但不会报错 | --- ## ✅ 建议 - 使用 `SELECT` 预览数据,确保更新范围正确; - 如果 `ctn.sharing_scope` 中可能没有“文档安全等级为A”,但你希望更新其他情况,可以考虑放宽正则; - 如果你希望即使 `ctn.sharing_scope` 没有匹配正则也更新某些字段,需要调整逻辑。 --- ## ✅ 相关问题 1. PostgreSQL 中 `~` 操作符的作用是什么? 2. 如何用 `SELECT` 查看 `UPDATE` 将影响哪些数据? 3. SQL 中 `COALESCE` 和 `SUBSTRING` 结合使用有什么注意事项? 4. 如何调试 PostgreSQL 中的正则表达式匹配? 5. SQL 更新语句中如何避免误更新空字段? ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI记忆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值