版本比较
题解 P5266 【【深基17.例6】学籍管理
正文
增加 55 行删除 57 行
1233455677899101111121314151616171818192021222 行未改动25262728292930313233333434353537383637384039424344404142434647484950515244454647484950545152565753545556575859636465666061626364656966677172686970747172767778737475768077787983808185868782838485899086878889909495969798919293949596100101102979899100104101102106103104108105106107111108109110111112113114看遍了所有题解发现没有写trie树的,那我就来写一篇吧。
### 题目要干啥?## 题目要干啥?
简单来说,就是让你写一个“学生成绩管理系统”,可以干四件事简单来说,就是干四件事。
1. 加人 / 改分:给你名字和分数,如果名字没出现过就加进去,出现过就把分数改成新的。成功就输出 OK1. 加人 / 改分:给你名字和分数,如果名字没出现过就加进去,出现过就把分数改成新的。成功就输出 OK。 1. 查分:给你名字,把对应的分数打出来。没这人就输出 Not found1. 查分:给你名字,把对应的分数打出来。没这人就输出 Not found。 1. 删人:给你名字,把这个人删掉。删成功输出 Deleted successfully,没有就 Not found1. 删人:给你名字,把这个人删掉。删成功输出 Deleted successfully,没有就 Not found。
1. 看人数:直接输出当前系统里总共有多少学生。
注意名字是要区分大小写的字母和数字注意名字是要区分大小写的字母和数字。
### 什么是trie?## 什么是trie?Trie 就像一棵倒着长的树,每个节点代表一个字符串前缀。从根出发,每走一条边就多加一个字符,走到某个节点如果被打上了标记,就说明这个位置上存了一个完整的学生名字。
举个例子,我们有一个名字 "lxl",它的 Trie 路径大概长这样:
2 行未改动 └─ l └─ x └─ l (在这里记录分数)```
### 完整代码## 完整代码```cpp#include<bits/stdc++.h>using namespace std;
long long q, idx = 0, cnt;long long q,idx=0,cnt;
struct node{ int son[70]; // 孩子节点编号,0 表示没有这个孩子 long long flag = -1; // 存分数,-1 表示这个位置上没有学生 int son[70]; // 孩子节点编号,0 表示没有这个孩子。 long long flag=-1; // 存分数,-1 表示这个位置上没有学生。}tr[100000];
int toint(char a){ if(a >= '0' && a <= '9') return a - '0'; if(a >= 'A' && a <= 'Z') return a - 'A' + 10; return a - 'a' + 36; if(a>='0'&&a<='9')return a-'0'; if(a>='A'&&a<='Z')return a-'A'+10; return a-'a'+36;}
void build(string s, long long x){ int now = 0; for(int i = 0; i < s.length(); i++){ int ch = toint(s[i]); if(tr[now].son[ch] == 0){ tr[now].son[ch] = ++idx; // 建新节点void build(string s,long long x){ int now=0; for(int i=0;i<s.length();i++){ int ch=toint(s[i]); if(tr[now].son[ch]==0){ tr[now].son[ch]=++idx; // 建新节点 } now = tr[now].son[ch]; now=tr[now].son[ch]; } if(tr[now].flag == -1) cnt++; // 新面孔,人数加一 cout << "OK" << endl; if(tr[now].flag==-1) cnt++; // 新面孔,人数加一 cout<<"OK"<<endl; tr[now].flag = x; // 记录或更新分数}//顺着名字走,如果半路走不下去了(son[ch] == 0),说明这个人根本不存在,直接 Not found。走完了到最后一个节点,如果 flag 还是 -1,那也只是路过,并没有在这个点存过学生,也是 Not found。只有 flag 不是 -1 时,才输出分数。void query(string s){ int now = 0; for(int i = 0; i < s.length(); i++){ int ch = toint(s[i]); if(tr[now].son[ch] == 0){ cout << "Not found" << endl; for(int i=0;i<s.length();i++){ int ch=toint(s[i]); if(tr[now].son[ch]==0){ cout<<"Not found"<<endl; return; } now = tr[now].son[ch]; now=tr[now].son[ch]; } if(tr[now].flag == -1) cout << "Not found" << endl; else cout << tr[now].flag << endl; if(tr[now].flag==-1)cout<<"Not found"<< endl; else cout<<tr[now].flag<<endl;}//和查询几乎一模一样,只不过找到后把 flag 重新设成 -1,并且 人数减 1,再输出 Deleted successfully。找不到当然就 Not found。//和查询几乎一模一样,只不过找到后把 flag 重新设成 -1,并且 人数减 1,再输出 Deleted successfully。找不到就 Not found。void query2(string s){ int now = 0; for(int i = 0; i < s.length(); i++){ int ch = toint(s[i]); int now=0; for(int i=0;i<s.length();i++){ int ch=toint(s[i]); if(tr[now].son[ch] == 0){ cout << "Not found" << endl; cout<<"Not found"<<endl; return; } now = tr[now].son[ch]; now=tr[now].son[ch]; } if(tr[now].flag != -1){ cout << "Deleted successfully" << endl; tr[now].flag = -1; if(tr[now].flag!=-1){ cout<<"Deleted successfully"<<endl; tr[now].flag=-1; cnt--; } else { cout << "Not found" << endl; }else{ cout<<"Not found"<<endl; }}int main(){ cin >> q; for(long long i = 1; i <= q; i++){ long long op, x; cin >> op; if(op == 1){ cin>>q; for(long long i=1;i<=q;i++){ long long op,x; cin>>op; if(op==1){ string s; cin >> s >> x; build(s, x); } else if(op == 2){ cin>>s>>x; build(s,x); }else if(op==2){ string s; cin >> s; cin>>s; query(s); } else if(op == 3){ } else if(op==3){ string s; cin >> s; cin>>s; query2(s); } else { cout << cnt << endl; cout<<cnt<<endl; } } return 0;}```**DeepSeek 辅助完成。**