本节课的内容面向复杂模拟题,以A题为例重点讲解了面向对象的方法
A 咕咕东的目录管理器
具体参考PPT,题目链接https://vjudge.net/contest/368284#problem/A
题目描述:
咕咕东的雪梨电脑的操作系统在上个月受到宇宙射线的影响,时不时发生故障,他受不了了,想要写一个高效易用零bug的操作系统 —— 这工程量太大了,所以他定了一个小目标,从实现一个目录管理器开始。前些日子,东东的电脑终于因为过度收到宇宙射线的影响而宕机,无法写代码。他的好友TT正忙着在B站看猫片,另一位好友瑞神正忙着打守望先锋。现在只有你能帮助东东!
初始时,咕咕东的硬盘是空的,命令行的当前目录为根目录 root。
目录管理器可以理解为要维护一棵有根树结构,每个目录的儿子必须保持字典序。
输入:
输入文件包含多组测试数据,第一行输入一个整数表示测试数据的组数 T (T <= 20);
每组测试数据的第一行输入一个整数表示该组测试数据的命令总数 Q (Q <= 1e5);
每组测试数据的 2 ~ Q+1 行为具体的操作 (MKDIR、RM 操作总数不超过 5000);
面对数据范围你要思考的是他们代表的 “命令” 执行的最大可接受复杂度,只有这样你才能知道你需要设计的是怎样复杂度的系统。
输出:
每组测试数据的输出结果间需要输出一行空行。注意大小写敏感。
整个解题过程:
框架
一开始拿道题无从下手,可以自顶向下地设计整个程序框架,从程序的入口开始
#include <bits/stdc++.h>
using namespace std;
char tmps[20];//step3.声明一个辅助输入的字符串数组
void solve()
{
int n;scanf("%d",&n);//step2.每组数据有n行命令
while(n--){
scanf("%d",tmps);
//step4.一条命令不是单纯的字符串,可能涉及到如何undo,将其封装成Command,以便于复用/扩展
Command* cmd=new Command(tmps);
...
}
}
int main()
{
int T; scanf("%d",&T);//step1.测试有T组数据
while(T--) solve();
return 0;
}
封装
考虑有什么和做什么。比如命令字符串“tmps”就表示一条命令,同时考虑一条命令不但有命令形式,还有命令参数,比如"MKDIR s",还要考虑参数的分离,同类信息最好内聚,所以封装。
//step5.封装Command
struct Command
{
const string CMDNAMES[7]={"MKDIR","RM","CD","SZ","LS","TREE","UNDO"};
int type;//命令的类型
string arg;//命令的参数
Command(string s){//构造函数
for (int i=0; i<7; i++){
if(CMDNAMES[i]==s){
type=i;
if(i<3){//前三个都有参数
scanf("%s",tmps);
arg=tmps;
return;;
}
}
}
}
}
树形结构
然后对于目录管理维护一棵目录树是必然的,所以我们究竟是选择ds的二叉树还是选择凸轮上的多叉树由题目要求决定。题目要求一个目录能够更具子目录的名字取到它的子目录,所以要用map<string,目录>,可以根据key即string在内部进行排序,这样每次可以log级别复杂度取到子目录。树的具体设计如下
//step6.树的设计
struct Directory
{
string name;//当前目录的名字
map<string ,Directory*> children;//用指针为了避免复制构造造成的内存浪费
Directory* parent;//便于"CD.."要返回上级目录
int subtreeSize;//便于“SZ”要输出子树大小
Directory(string name,Directory* parent){
this->name=name;
this->parent=parent;
this->subtreeSize=1;
}
}
解题框架
对solve函数进行完善,每条指令的执行对象均为当前目录“now”
void solve()
{
int n;scanf("%d",&n);
Directory* now new Directory("root",nullptr);
while(n--){
scanf("%s",tmps);
Command* cmd=new Command(tmps);
switch(cmd->type){
case 0: now->mkdir(cmd->arg); break;
case 1: now->rm(cmd->arg); break;
case 2: now->cd(cmd->arg); break;
case 3: now->sz(); break;
case 4: now->ls(); break;
case 5: now->tree(); break;
case 6://UNDO先放在一边
}
}
}
到这里还要在Directory结构体里写具体的实现
struct Directory(){
/*省略已阅部分*/
Directory* mkdir(string name);
Directory* rm(string name);
Directory* cd(string name);
void sz();
void ls();
void tree();
}
然后继续分析undo指令,可以执行undo必然是MKDIR、RM、CD之一,且成功完成,如果要undo的话必须要保存每条指令的执行结果,以及各个操作的目录节点,修改如下
struct Command
{
/*省略已阅*/
Directory* tmpDir;//记录刚刚操作设计的目录节点
}
然后对于solve()函数再次进行完善
void solve()
{
int n;scanf("%d",&n);
Directory* now=new Directory("root",nullptr);
vector<Command*> cmdList;//新增加的数组,用来存成功执行的命令以便于"UNDO"
while(n--){
scanf("%s",tmps);
Command* cmd=new Command(tmps);
switch(cmd->type){
case:0:case 1://MKDIR,RM
cmd->tmpDir=cmd->type==0 ? now->mkdir(cmd->arg) : now->rm(cmd->arg);
if(cmd->tmpDir==nullptr) printf("ERR\n");
else {
printf("OK\n");
cmdList.push_back(cmd);
}
break;
case 2:
Directory* ch=now->cd(cmd->arg);
if(ch==nullptr) printf("ERR\n");
else {
printf("OK\n");
cmd->tmpDir=now;
now=ch;
cmdList.push_back(cmd);
}
break;
case 3: now->sz(); break;
case 4: now->ls(); break;
case 5: now->tree(); break;
case 6:{
bool success=false;//用来记录UNDO是否执行成功
while(!success && !cmdList.empty()){
cmd=cmdList.back(); cmdList.pop_back();
switch(cmd->type){
case 0: success=now->rm(cmd->arg)!=nullptr; break;//undo MKDIR
case 1: success=now->addChild(cmd->tmpDir); break;//undo RM
case 2: now=cmd->tmpDir; success=true; break;//undo CD
}
}
printf(success ? "OK\n" : "ERR\n");
}
}
}
}
整体框架明晰之后,就是讲各个子函数逐一进行细节的完善填充即可
struct Directory
{
string name;//当前目录的名字
map<string ,Directory*> children;//用指针为了避免复制构造造成的内存浪费
Directory* parent;//便于"CD.."要返回上级目录
int subtreeSize;//便于“SZ”要输出子树大小
Directory(string name,Directory* parent){
this->name=name;
this->parent=parent;
this->subtreeSize=1;
}
public:
Directory* getChild(string name){//取得子目录并返回,不存在就返回空指针
auto it =children.find(name);
if(it==children.end()) return nullptr;
else return it->second;
}
Directory* mkdir(string name){//创建子目录并返回,创建失败返回空指针
if(children.find(name) != children.end()) return nullptr;
Directory* ch=new Directory(name,this);
children[name]=ch;
maintain(+1);
return ch;
}
Directory* rm(string name){//删除子目录并返回,删除失败则返回空指针
auto it=children.find(name);
if(it==children.end()) return nullptr;
maintain(-1*it->second->subtreeSize);
children.erase(it);
return it->second;
}
Directory* cd(string name){
if(name=="..") return this->parent;
return getChild(name);
}
bool addChild(Directory* ch){//加入子目录并返回成功与否
if(children.find(ch->name)!=children.end()) return false;
children[ch->name]=ch;
maintain(+ch->subtreeSize);
return true;
}
void maintain(int delta){//向上维护子树的大小,便于实现"SZ"
updated=true;
subtreeSize+=delta;
if(parent!=nullptr) parent->maintain(delta);
}
void sz(){
printf("%d\n",this->subtreeSize);
}
void ls(){
int sz=children.size();
if(sz==0) printf("EMPTY\n");
else if(sz<=10){
for (auto &entry : children)
printf("%s\n",entry.first.c_str());
}
else {
auto it=children.begin();
for (int i=0; i<5; i++,it++) printf("%s\n",it->first.c_str());
printf("...\n");
it=children.end();
for (int i=0; i<5; i++) it--;
for (int i=0; i<5; i++,it++)printf("%s\n",it->first.c_str());
}
}
void tree();
};
注入灵魂
最后要实现TREE命令,是最难的。所以我们采取的方式是缓存(懒更新),节点数远少于TREE的操作数,指不定还有重复询问,对于目录相同期间问过的相同问题,可以只计算一次。
struct Directory
{
/*其他*/
vector<string>* tenDescendants;//保存当前节点的“十个后代”
bool updated; //记录当前节点的子树有无变动,无变动则其“十个后代”无需更新
public:
void tree(){
if(subtreeSize==1) printf("EMPTY\n");
else if(subtreeSize<=10){
if(this->updated){
tenDescendants->clear();
treeAll(tenDescendants);
this->updated=false;
}
for (int i=0; i<subtreeSize; i++) printf("%s\n",tenDescendants->at(i).c_str());
}
else {
if(this->updated){
tenDescendants->clear();
treeFirstSome(5,tenDescendants);
treeLastSome(5,tenDescendants);
this->updated=false;
}
for (int i=0; i<5; i++) printf("%s\n",tenDescendants->at(i).c_str());
printf("...\n");
for (int i=9; i>=5; i--) printf("%s\n",tenDescendants->at(i).c_str());
}
}
private:
void treeAll(vector<string>* bar) {//全部后代加入桶
//更新整个桶
bar->push_back(name);
for (auto &entry: children) entry.second->treeAll(bar);
}
void treeFirstSome(int num,vector<string>* bar) {//前序遍历并加入首要的num个后代
bar->push_back(name);
if(--num==0) return;
int n=children.size();
auto it=children.begin();
while(n--){
int sts=it->second->subtreeSize;
if(sts>=num){
it->second->treeFirstSome(num,bar);
return;
}
else {
it->second->treeFirstSome(sts,bar);
num-=sts;
}
it++;
}
}
void treeLastSome(int num,vector<string>* bar) {//后序遍历并加入首要的num个后代
int n=children.size();
auto it=children.end();
while(n--){
it--;
int sts=it->second->subtreeSize;
if(sts>=num){
it->second->treeLastSome(num,bar);
return;
}
else {
it->second->treeLastSome(sts,bar);
num-=sts;
}
}
bar->push_back(name);
}
}
教训
对于一个“功能”不要上来就写细节,而是假装已经封装写好了,从而先避免细节的实现,来关注整体设计。
B 东东学打牌
最近,东东沉迷于打牌。所以他找到 HRZ、ZJM 等人和他一起打牌。由于人数众多,东东稍微修改了亿下游戏规则:
所有扑克牌只按数字来算大小,忽略花色。
每张扑克牌的大小由一个值表示。A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K 分别指代 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13。
每个玩家抽得 5 张扑克牌,组成一手牌!(每种扑克牌的张数是无限的,你不用担心,东东家里有无数副扑克牌)
理所当然地,一手牌是有不同类型,并且有大小之分的。
举个栗子,现在东东的 “一手牌”(记为 α),瑞神的 “一手牌”(记为 β),要么 α > β,要么 α < β,要么 α = β。
那么这两个 “一手牌”,如何进行比较大小呢?首先对于不同类型的一手牌,其值的大小即下面的标号;对于同类型的一手牌,根据组成这手牌的 5 张牌不同,其值不同。下面依次列举了这手牌的形成规则:
- 大牌:这手牌不符合下面任一个形成规则。如果 α 和 β 都是大牌,那么定义它们的大小为组成这手牌的 5 张牌的大小总和。
- 对子:5 张牌中有 2 张牌的值相等。如果 α 和 β 都是对子,比较这个 “对子” 的大小,如果 α 和 β 的 “对子” 大小相等,那么比较剩下 3 张牌的总和。
- 两对:5 张牌中有两个不同的对子。如果 α 和 β 都是两对,先比较双方较大的那个对子,如果相等,再比较双方较小的那个对子,如果还相等,只能比较 5 张牌中的最后那张牌组不成对子的牌。
- 三个:5 张牌中有 3 张牌的值相等。如果 α 和 β 都是 “三个”,比较这个 “三个” 的大小,如果 α 和 β 的 “三个” 大小相等,那么比较剩下 2 张牌的总和。
- 三带二:5 张牌中有 3 张牌的值相等,另外 2 张牌值也相等。如果 α 和 β 都是 “三带二”,先比较它们的 “三个” 的大小,如果相等,再比较 “对子” 的大小。
- 炸弹:5 张牌中有 4 张牌的值相等。如果 α 和 β 都是 “炸弹”,比较 “炸弹” 的大小,如果相等,比较剩下那张牌的大小。
- 顺子:5 张牌中形成 x, x+1, x+2, x+3, x+4。如果 α 和 β 都是 “顺子”,直接比较两个顺子的最大值。
- 龙顺:5 张牌分别为 10、J、Q、K、A。
作为一个称职的魔法师,东东得知了全场人手里 5 张牌的情况。他现在要输出一个排行榜。排行榜按照选手们的 “一手牌” 大小进行排序,如果两个选手的牌相等,那么人名字典序小的排在前面。
不料,此时一束宇宙射线扫过,为了躲避宇宙射线,东东慌乱中清空了他脑中的 Cache。请你告诉东东,全场人的排名
输入
输入包含多组数据。每组输入开头一个整数 n (1 <= n <= 1e5),表明全场共多少人。
随后是 n 行,每行一个字符串 s1 和 s2 (1 <= |s1|,|s2| <= 10), s1 是对应人的名字,s2 是他手里的牌情况。
输出
对于每组测试数据,输出 n 行,即这次全场人的排名。
思路
思路过于啰嗦= =
首先一个结构体记录该玩家的牌的情况,具体变量含义见注释,按照手牌类型由大到小进行判断
顺子:判断五个数字是否连续,并存储最大值,A的话存14,便于判断。
炸弹:判断是否有数字出现次数为4,存储炸弹的值和另一个数字;出现次数为5,将该数字再存一遍。
三:分为三带二和三个,判断是否由数字出现次数为3,有则看是否有数字出现次数为2,满足则为三带二,反之则为三个,对剩余两个数字求和。
对子:遍历看出现次数为2的牌有几个,2个则为两对,1个则为对子,再将剩余所需数据计算存储
均不满足则为大牌,求和。
然后设计cmp函数 我设计的过于啰嗦了
调用sort函数排序,输出即可
#include <stdio.h>
#include <algorithm>
#include <string>
#include <string.h>
using namespace std;
int n;
struct people
{
char name[20];
string s;
char card[20];
int pai[15];
int lei;
int shunzi;
int shunzi_max;
int bomb[2];//一个炸弹和剩余的牌
int three[3];//一个triple和剩余牌的和
int two[2];//一个对子和剩余牌的和
int double2[3];//两个对子和剩余的牌
int sum;
}human[100005];
void initial()
{
for(int i=0;i<n;i++)
{
human[i].shunzi=0;
human[i].shunzi_max=0;
human[i].lei=0;
human[i].sum=0;
for (int k=0;k<2;k++)
{
human[i].bomb[k]=0;
human[i].double2[k]=0;
human[i].three[k]=0;
human[i].two[k]=0;
}
human[i].double2[2]=0;
human[i].three[2]=0;
memset(human[i].card,0,sizeof(human[i].card));
memset(human[i].name,0,sizeof(human[i].name));
memset(human[i].pai,0,sizeof(human[i].pai));
}
}
void trans(people& p)
{
for(int i=0;i<10;i++)
{
if(p.card[i]=='A') p.pai[1]++,p.pai[14]++;
if(p.card[i]=='2') p.pai[2]++;
if(p.card[i]=='3') p.pai[3]++;
if(p.card[i]=='4') p.pai[4]++;
if(p.card[i]=='5') p.pai[5]++;
if(p.card[i]=='6') p.pai[6]++;
if(p.card[i]=='7') p.pai[7]++;
if(p.card[i]=='8') p.pai[8]++;
if(p.card[i]=='9') p.pai[9]++;
if(p.card[i]=='1'&&p.card[i+1]=='0') p.pai[10]++;
if(p.card[i]=='J') p.pai[11]++;
if(p.card[i]=='Q') p.pai[12]++;
if(p.card[i]=='K') p.pai[13]++;
if(p.card[i]=='0') continue;
}
p.s=p.name;
}
void shunzi(people& p)
{
bool flag=0;
for(int h=1;h<=10;h++)
{
if((p.pai[h]==1&&p.pai[h+1]==1)&&(p.pai[h+1]==1&&p.pai[h+2]==1)&&
(p.pai[h+2]==1&&p.pai[h+3]==1)&&(p.pai[h+3]==1&&p.pai[h+4]==1))
{
p.shunzi_max=h+4;
p.shunzi=1;
flag=true;
}
}
if(!flag)
{
p.shunzi_max=0;
p.shunzi=0;
}
}
void zhadan(people& p)
{
for(int i=1;i<14;i++){
if(p.pai[i]==4){
p.bomb[0]=i;
break;
}
}
for(int i=1;i<14;i++){
if(p.pai[i]==5){
p.bomb[0]=i;
p.bomb[1]=i;
break;
}
}
if(p.bomb[0]!=0){
for(int i=1;i<14;i++){
if(p.pai[i]==1){
p.bomb[1]=i;
break;
}
}
}
}
void triple(people& p)
{
for(int i=1;i<14;i++){
if(p.pai[i]==3){
p.three[0]=i;
break;
}
}
if(p.three[0]!=0){
for(int i=1;i<14;i++){
if(p.pai[i]==1)
p.three[1]+=i;//表示剩下两个的和
else if(p.pai[i]==2)//三带二
p.three[2]=i;
}
}
}
void duizi(people& p)
{
int num=0,tmp[3];//num对子个数
memset(tmp,0,sizeof(tmp));
for(int i=13;i>0;i--){
if(p.pai[i]==2){
tmp[num]=i;
num++;
}
}
if(num==2){
for(int i=0;i<num;i++){
p.double2[i]=tmp[i];
}
for(int i=1;i<14;i++){
if(p.pai[i]==1){
p.double2[2]=i;
}
}
}
else if(num==1){
p.two[0]=tmp[0];
for(int i=1;i<14;i++){
if(p.pai[i]==1)
p.two[1]+=i;
}
}
}
void sum(people& p)
{
for(int i=1;i<14;i++)
if(p.pai[i]==1) p.sum+=i;
}
void leixing(people& p)
{
shunzi(p);
zhadan(p);
triple(p);
duizi(p);
sum(p);
if(p.shunzi!=0&&p.shunzi_max==14) p.lei=8;
else if(p.shunzi!=0) p.lei=7;
else if(p.bomb[0]!=0) p.lei=6;
else if(p.three[0]!=0 && p.three[2]!=0) p.lei=5;
else if(p.three[0]!=0) p.lei=4;
else if(p.double2[0]!=0) p.lei=3;
else if(p.two[0]!=0) p.lei=2;
else p.lei=1;
}
bool cmp(people a,people b)
{
if(a.lei!=b.lei) return a.lei>b.lei;
else if(a.shunzi_max!=b.shunzi_max) return a.shunzi_max>b.shunzi_max;
else if(a.bomb[0]!=b.bomb[0]) return a.bomb[0]>b.bomb[0];
else if(a.bomb[1]!=b.bomb[1]) return a.bomb[1]>b.bomb[1];
else if(a.three[0]!=b.three[0]) return a.three[0]>b.three[0];
else if(a.three[2]!=b.three[2]) return a.three[2]>b.three[2];
else if(a.three[1]!=b.three[1]) return a.three[1]>b.three[1];
else if(a.double2[0]!=b.double2[0]) return a.double2[0]>b.double2[0];
else if(a.double2[1]!=b.double2[1]) return a.double2[1]>b.double2[1];
else if(a.double2[2]!=b.double2[2]) return a.double2[2]>b.double2[2];
else if(a.two[0]!=b.two[0]) return a.two[0]>b.two[0];
else if(a.two[1]!=b.two[1]) return a.two[1]>b.two[1];
else if(a.sum!=b.sum) return a.sum>b.sum;
else return a.s<b.s;
}
int main()
{
//freopen("test.txt","r",stdin);
while(~scanf("%d",&n))
{
initial();
for(int j=0;j<n;j++)
{
scanf("%s",human[j].name);
scanf("%s",human[j].card);
trans(human[j]);
leixing(human[j]);
}
sort(human,human+n,cmp);
for(int j=0;j<n;j++)
{
printf("%s\n",human[j].name);
}
}
return 0;
}
C 公园长椅(签到题)
SDUQD 旁边的滨海公园有 x 条长凳。第 i 个长凳上坐着 a_i 个人。这时候又有 y 个人将来到公园,他们将选择坐在某些公园中的长凳上,那么当这 y 个人坐下后,记k = 所有椅子上的人数的最大值,那么k可能的最大值mx和最小值mn分别是多少。
思路
椅子可以坐无数个人,所以y个人坐下后的k的最大值即所有人都坐在了原本人数最多的椅子上,求和即可。
最小值的话则现将所有椅子人数进行排序,每次坐在人数最少的椅子上,即min+1,然后sort,最后最小值即为答案
#include <stdio.h>
#include <algorithm>
using namespace std;
int n,people;
int chair[105];
int main()
{
scanf("%d",&n);
scanf("%d",&people);
for(int i=0;i<n;i++)
{
scanf("%d",&chair[i]);
}
sort(chair,chair+n);
int max1=chair[n-1];
int max2=max1+people;
while(people--)
{
chair[0]++;
sort(chair,chair+n);
}
int min=chair[n-1];
printf("%d %d",min,max2);
return 0;
}