记录CSP题解

  1. 化学方程式
string fomular;
unordered_map<string, int> mymap;

struct Element
{
    string name;
    int num;
    Element(string n, int u = -1) :name(n), num(u) {};
};


void processWord(string word,int type)
{
    int lo = 0; int hi = word.size()-1;
    string tem;
    stack<Element> S;
    S.push(Element("("));
    int num = 0; bool flag = false;
    while (isdigit(word[lo]))
    {
        num = num * 10 + (word[lo] - '0');
        lo++;
        flag = true;
    }
    if (!flag) num = 1;
    while (lo <= hi)
    {

        if (word[lo] == '(')
        {
            S.push(Element("("));
            lo++;
        }
        else if (isupper(word[lo]))
        {
            tem = "";
            tem += word[lo];
            S.push({ tem,1 });
            lo++;
        }
        else if (islower(word[lo]))
        {
            S.top().name += word[lo];
            lo++;
        }
        else if (isdigit(word[lo]))
        {

            int num = 0;
            while (lo <= hi && isdigit(word[lo]))
            {
                num = num * 10 + (word[lo] - '0');
                lo++;

            }
  
            S.top().num = num;
        }
        else if (word[lo] == ')')
        {
            int count = 1;
            if (lo + 1 > hi || isdigit(word[lo + 1]) == false)
            {
                count = 1;
                lo++;
            }
            else
            {
                lo++; int num = 0;
                while (lo <= hi && isdigit(word[lo]))
                {
                    num = num * 10 + (word[lo] - '0');
                    lo++;
                }
                count = num;
            }
            vector<Element> tmp;
            while (S.top().name != "(")
            {
                Element old = S.top();
                old.num *= count;
                S.pop();
                tmp.push_back(old);
            }
            S.pop();
            for (int i = tmp.size()-1; i >= 0; i--)
            {
                S.push(tmp[i]);
            }

        }
    }
    //对stack中的数字做系数乘累加到map中
    while (!S.empty())
    {
        if (type == 0)
        {

            mymap[S.top().name] += S.top().num * num;
        }
        else 	mymap[S.top().name] -= S.top().num * num;
        S.pop();
    }
}

void handle(string s, int type)
{
    int lo = 0;
    while (lo < s.size())
    {

        int jia = s.find('+', lo);
        if (jia == s.npos) break;
        processWord(s.substr(lo,jia-lo)+')', type);
        lo = jia+1;
    }
    processWord(s.substr(lo)+')',type);
}

int main()
{
    std::ios::sync_with_stdio(false);
    //用等号分成两部分
    int n; cin >> n;
    while (n--)
    {
        cin >> fomular;
        int duan = fomular.find('=');
        handle(fomular.substr(0, duan), 0);
        handle(fomular.substr(duan + 1), 1);
        int flag = 0;
        //判断map是否全0
        for (auto& it : mymap)
        {
            if (it.second != 0)
            {
                cout << "N" << endl;
                flag = 1;
                break;
            }
        }
        if(!flag) cout << "Y"<<endl;
        mymap.clear();
    }
    return 0;
}

string的s.find(字符串或字符,index)表示从s的index开始处开始查找指定字符串或字符,返回匹配的下标,若无则返回s.npos

2.Markdown

bool judgeEmptyRow(string s)
{
    for (char i : s)
    {
        if (i != ' ')
            return false;
    }
    return true;

}

string reduce(string s)
{
    int i = 0; int n = s.size(); int j = s.size()-1;
    while (i < n && s[i] == ' ')
    {
        i++;
    }
    while (j >= 0 && s[j] == ' ')
    {
        j--;
    }
    return s.substr(i, j - i + 1);
}

int line = 1; int biao = 0;
void add(int w,char c=' ')
{
    biao++;
    if (biao > w)
    {
        biao = 1;
        line++;
    }
    if (c == ' ' && biao == 1) biao = 0;
}

void add2(int w, char c = ' ')
{
    biao++;
    if (biao > w)
    {
        biao = 4;
        line++;
    }
    if (c == ' ' && biao == 4) biao = 3;
}
int main()
{
    std::ios::sync_with_stdio(false);
    int w; cin >> w; string str; int duan = 1;//1则为空行状态 
    int xiang = 0;//0段落1项目列表
    
    int ans=0;
    while (getline(cin, str))
    {
        if (judgeEmptyRow(str) && duan==0)
        {
            line++; biao = w;
            duan = 1;
      //      cout << line<<endl;
            continue;
        }
        if (str.size() > 1)
        {
            if (str[0] == '*' && str[1] == ' ')
            {
                if (duan == 0 && xiang == 0) line++;
                xiang = 1;
                duan = 0;
                line++; biao = 3;  
                
                string tem = reduce(str.substr(2));
                for (int i = 0; i < tem.size(); i++)
                {
                    add2(w,tem[i]);
                }
         //       cout << line << endl;
                continue;
            }
            else if (xiang==1 && str[0] == ' ' && str[1] == ' ')
            {
                if (duan == 0) add2(w);
                string tem = reduce(str.substr(2));
                for (int i = 0; i < tem.size(); i++)
                {
                    add2(w,tem[i]);
                }
       //         cout << line << endl;
                continue;
            }
        }
        if (xiang == 1)
        {
            line++;
            biao = w;
            xiang = 0;
            duan = 1;
        }
        if (duan == 0) add(w);
        string tem = reduce(str);
        for (int i = 0; i < tem.size(); i++)
        {
            add(w,tem[i]);
            duan = 0;
        }
        
    //    cout << line << endl;
      

    }
    if (duan==1)
    {
        line--;
    }
    cout << line<<endl;
}

3.推荐系统

#include<stdio.h>
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<stack>
#include<algorithm>
#include<unordered_map>
#include<queue>
#include<set>
using namespace std;

struct commodity
{
    int label;
    int id;
    int score;
};

 bool operator < (commodity a,commodity b)
{
    if (a.score == b.score)
    {
        if (a.label == b.label)
        {
            return a.id < b.id;
        }
        
            return a.label < b.label;
    
        
    }
    return a.score > b.score;

    
}
int main()
{
   std::ios::sync_with_stdio(false);
   set<commodity> Q;
   
   int m; int n; int num;
   cin >> m >> n;
   vector<unordered_map<int, int>> store(m);
   
   for (int i = 0; i < n; i++)
   {
       int iid; int sscore;
       cin >> iid >> sscore;
       for (int j = 0; j < m; j++)
       {
           Q.insert({ j,iid,sscore });
           store[j][iid] = sscore;
       }
   }
   cin >> num;
   for (int i = 0; i < num; i++)
   {
       int type;
       cin >> type;
       switch (type)
       {
       case 1:
           int label; int id; int score;
           cin >> label >> id >> score;
           Q.insert({ label,id,score });
           store[label][id] = score;
           break;
       case 2:
           cin >> label >> id;
           Q.erase({ label,id,store[label][id] });
           break;
       case 3:
           int max_num;
           vector<int> require(m);
           vector<vector<int>> ans(m);
           cin >> max_num;
           for (int j = 0; j < m; j++)
           {
               cin >> require[j];
           }
           int count = 0;
           for (auto& it : Q)
           {
               if (max_num == 0 || count == m)
               {
                   break;
               }
               if (require[it.label] > 0)
               {
                   if (require[it.label] == 1) count++;
                   require[it.label]--;
                   max_num--;
                   ans[it.label].push_back(it.id);
               }
               
           }
           for (int j = 0; j < m; j++)
           {
               if (ans[j].empty())
               {
                   cout << "-1" ;
               }
               for (int it : ans[j])
               {
                   cout << it << " ";
               }
               cout << endl;
           }
           
       }
   }
}

对于重载<运算符,参数为a,b,a是大的

3.区块链

// Project2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include<stdio.h>
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<stack>
#include<algorithm>
#include<unordered_map>
#include<queue>
#include<set>
using namespace std;


bool tochange(vector<int>& old, vector<int>& newone)
{
    if (old.size() < newone.size()) return true;
    if (old.size() > newone.size()) return false;
    return old.back() > newone.back();
}


int main()
{
    std::ios::sync_with_stdio(false);
    int point_num; int times;
    cin >> point_num >> times;
    //时间-点列表(待加列表,添加节点列表)
    map<int, unordered_map<int, pair<vector<int>, vector<int>>>> store;
    vector<vector<int>> neibor(point_num+1);
    vector<vector<int>> ans(point_num+1, vector<int>(1, 0));
    for (int i = 0; i < times; i++)
    {
        int a; int b;
        cin >> a >> b;
        neibor[a].push_back(b);
        neibor[b].push_back(a);
    }
    int delay;
    cin >> delay >> times; string str;
    while (times--)
    {
        int node; int t; int id;
        cin >> node >> t;
        if (cin.get() == '\n' or cin.eof())
        {
            //search
            for (auto& it : store)
            {
                int cur_time = it.first;
                auto& temmap = it.second;


                if (cur_time > t) break;

                for (auto& iii : temmap)
                {
                    //先更新
                    int cur_point = iii.first;
                    bool flag = tochange(ans[cur_point], iii.second.first);
                    if (flag)
                    {
                        ans[cur_point] = iii.second.first;
                    }
                    //再增加
                    for (int aaa : iii.second.second)
                    {
                        ans[cur_point].push_back(aaa);
                    }

                    //传播
                    if (flag || !iii.second.second.empty())
                    {
                        for (int bbb : neibor[cur_point])
                        {
                            if (tochange(ans[bbb], ans[cur_point]) && tochange(store[cur_time + delay][bbb].first, ans[cur_point]))
                            {
                                store[cur_time + delay][bbb].first = ans[cur_point];
                            }
                        }
                    }

                }
                
               
            }
            //注意这里要删除前面时刻的,记住这里的用法
            store.erase(store.begin(),store.upper_bound(t));
            int nn = ans[node].size();
            cout << nn;
            for (int ccc : ans[node])
            {
                cout << " " << ccc;
            }
            cout << endl;
       }
       else
       {
           cin >> id;

           store[t][node].second.push_back(id);
           
       }

   }
}

注意不要轻易在迭代器中删除元素,实在要删只能使用如下形式。
上述程序中删除map的小于等于某元素的方法使用了map的upper_bound

for(iterator iter=mapTest.begin();iter!=mapTest.end();)
{
cout<<iter->first<<":"<<iter->second<<endl;
mapTest.erase(iter++);
}
CSP(Certified Software Professional,软件能力认证)是由中国计算机学会组织的一项专业评测活动,旨在评估个人的算法设计、分析及程序编写等综合能力。针对您提到的“CSP第35次认证题解”,以下是关于这类问题的一般解答框架: --- ### CSP 第35次认证概述 CSP 认证通常分为两个级别:基础级和提高级。每次考试包含若干道题目,涵盖数据结构、算法设计以及编程实现等内容。 对于第35次认证的具体题解,虽然无法提供完整的官方答案,但我可以为您解析常见的考点及其对应的解决方案思路: #### 常见题型与解决策略 1. **简单计算题** - 考查基本数学运算或模拟操作。 - 示例:输入一组数字并求其最大值、最小值或其他统计结果。 - 策略:直接遍历数组,并维护一个变量记录当前的最大/小值即可。 2. **字符串处理** - 涉及字符串匹配、替换或分割等问题。 - 示例:给定一段文本,删除其中的所有空格或将单词倒序排列。 - 答案示例代码片段: ```python s = input().strip() # 获取用户输入并去除首尾空白字符 words = s.split(" ") # 将字符串按空格分隔成列表 result = " ".join(reversed(words)) # 反转顺序后拼接回原字符串 print(result) ``` 3. **动态规划 (Dynamic Programming)** 或贪心算法 - 如果遇到优化类问题,则需要考虑是否能通过递归加记忆化搜索的方式降低时间复杂度;若存在明确优先规则则尝试采用贪婪思想构建全局最优解法。 4. **图论相关应用** - 包含最短路径寻找(BFS / Dijkstra) ,连通性判断(Union-Find Set),拓扑排序(Topological Sort)等经典模型的应用场景。 5. **高级数据结构运用** - 树状数组(Binary Indexed Tree),线段树(Segment Tree)常用于快速区间查询更新场合下发挥重要作用。 由于具体试题内容因版权原因不便复述完整版本,在此仅列举上述几大类别作为参考依据供考生复习备考之用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值