- 化学方程式
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++);
}