刷好题,固基础-3

CSP 202309-3 梯度求解

知识点1:stringsteam

stringsteam:支持对string 对象更灵活的处理;

作用:类型转换,词句转换

利用stringstream 实现句子与单个单词之间的转换;

1. 句子 转为单个的词

string a ="how old are you , dear ?";

stringstream ss;

ss << a;

string b;

while(ss >> b)

{

cout <<b<<endl;

}

字符串切割还可以使用如下方法:

getline(ss, 切割出来的子串, “间隔字符”);

while(getline(ss, tmp, ' ')){
        vec.push_back(tmp);
    }

2. 单个的词拼接为句子

stringstream ss;

ss << "how"<<" "<<"old"<<" "<<"are"<<" "<<"you"<<" "<<","<<"dear"<<" "<<"?";

string b;

b = ss.str();

cout<<b<<endl;

AC代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
const int mod = 1e9 + 7;
using ll = long long;
ll coef[N];
int n, m, to_solve;
string s, tmp;
vector<string> vec;
void solve() {
    stack<map<ll, ll>> expr;
    for (auto& str: vec) {
        if (str.empty()) {continue;}
        if (str[0] == 'x') { // var
            ll index = atoi(str.substr(1).c_str()); //取变量编号
            map<ll, ll> tp;
            if (index == to_solve) {	//如果是目标变量,标记 
                tp[1] = 1;	//使用0和1将目标变量和普通变量分隔开 
            } else {
                tp[0] = coef[index] % mod;	//不是,记录变量系数 
            }
            expr.push(tp);
        } else if (str.size() == 1 && !isdigit(str[0])) { //是操作符 
            auto pre = expr.top(); expr.pop();
            auto suf = expr.top(); expr.pop();
            map<ll, ll> res;
            if (str == "+") {
                for(auto& p: pre) {
                    res[p.first] = (p.second % mod);	//提取变量系数 
                }
                for (auto& p: suf) {
                    res[p.first] = (res[p.first] + p.second) % mod;
                }
            } else if (str == "-") {	//注意处理顺序 
                for(auto& p: suf) {
                    res[p.first] = (p.second % mod);
                }
                for (auto& p: pre) {
                    res[p.first] = (res[p.first] - p.second) % mod;
                }
            } else {
                for (auto& p: pre) {
                    for (auto& q: suf) {
                        ll zs = p.first + q.first;
                        ll bs = (p.second * q.second) % mod;
                        res[zs] = (res[zs] + bs) % mod;
                    }
                }
            }
            expr.push(res);
        } else { // digit
            ll digit = atoi(str.c_str());
            digit %= mod;
            map<ll, ll> tp;
            tp[0] = digit;
            expr.push(tp);
        }
    }
    assert(expr.size() == 1);	//如果expr大小为1,终止执行 
    ll res = 0;
    while (!expr.empty()) {
        auto final_expr = expr.top();
        expr.pop();
        for (auto& p: final_expr) {
            ll pw = 1;
            ll bs = (p.second * p.first) % mod;
            ll c = p.first - 1;
            while (c > 0) {
                c--;
                pw = (pw * coef[to_solve]) % mod;
            }
            pw = (pw * bs) % mod;
            res = (res + pw) % mod;
        }
    }
    res %= mod;
    res = (res + mod) % mod;
    cout << res << '\n';
 
 
 
}
int main() {
    cin >> n >> m;
    getchar();	//回车 
    getline(cin, s);
    stringstream ss(s);
    while (getline(ss, tmp, ' ')) {
        vec.push_back(tmp);
    }
    for (int i = 1; i <= m; i++) {
        cin >> to_solve;
        for (int j = 1; j <= n; j++) {
            cin >> coef[j];
        }
        solve();
    }
}

  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值