晴问算法刷题笔记
Notes
2023-01-10 7272字

[toc]

晴问算法——vector的常见用法详解

vector有通过下标访问和迭代器访问两种方式。

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<int> v;
    int n; cin >> n;
    for (int i = 0; i < n; i ++) {
        int tmp; cin >> tmp;
        v.push_back(tmp);
    }

    // 通过下标访问
    // for (int i = 0; i < n; i ++) {
    //     cout << v[i];
    //     if (i != n - 1) cout << " ";
    // }
    // cout << endl;

    // 通过迭代器访问
    for (vector<int>::iterator it = v.begin(); it != v.end(); it ++) {
        cout << *it;
        if (it != v.end() - 1) cout << " ";
    }
    cout << endl;

    return 0;
}
#include <iostream>
#include <vector>

using namespace std;

int main() {
    int n, k;
    cin >> n >> k;
    
    // 使用遍历来初始化
    vector<int> v;
    for (int i = 0; i < n; i ++)
        v.push_back(k);
    
    // 使用语法糖初始化
    // vector<int> v(n, k);
    
    for (vector<int>::iterator it = v.begin(); it != v.end(); it ++) {
        cout << *it;
        if (it != v.end() - 1) cout << " ";
    }

    return 0;
}
#include <iostream>
#include <vector>

using namespace std;

int main() {
    int n, k;
    cin >> n >> k;
    vector<int> v(n, 0);
    for (int i = 0; i < k; i ++)
        v.pop_back();
    cout << v.size() << endl;

    return 0;
}
#include <iostream>
#include <vector>

using namespace std;

int main() {
    int n; cin >> n;
    vector<int> v(n, 0);

    v.clear();
    cout << v.size() << endl;

    return 0;
}
#include <iostream>
#include <vector>

using namespace std;

int main() {
    int n; cin >> n;
    vector<int> v;
    for (int i = 0; i < n; i ++) {
        int tmp; cin >> tmp;
        v.push_back(tmp);
    }

    int x, k1, k2;
    cin >> x >> k1 >> k2;

    // 插入元素,插入后元素x占据插入位置
    v.insert(v.begin() + k1, x);
    
    // 删除元素,有针对迭代器单元素删除和区间删除,这里使用前者
    v.erase(v.begin() + k2);

    for (int i = 0; i < v.size(); i ++) {
        cout << v[i];
        if (i != v.size() - 1)
            cout << " ";
    }

    return 0;
}
#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<int> v1, v2;
    int n1, n2;
    cin >> n1 >> n2;

    int tmp;
    for (int i = 0; i < n1; i ++) {
        cin >> tmp;
        v1.push_back(tmp);
    }
    for (int i = 0; i < n2; i ++) {
        cin >> tmp;
        v2.push_back(tmp);
    }

    cout << (v1 < v2 ? "Yes" : "No") << endl;

    return 0;
}
#include <iostream>
#include <vector>

using namespace std;

int main() {
    int n; cin >> n;
    vector<int> vs[n];

    for (int i = 0; i < n; i ++) {
        int k; cin >> k;
        while (k --) {
            int tmp; cin >> tmp;
            vs[i].push_back(tmp);
        }
    }

    for (int i = 0; i < n; i ++) {
        for (int j = 0; j < vs[i].size(); j ++) {
            cout << vs[i][j];
            if (j != vs[i].size() - 1) 
                cout << " ";
        }
        cout << endl;
    }

    return 0;
}
#include <iostream>
#include <vector>

using namespace std;

int main() {
    int n; cin >> n;
    vector<vector<int> > vs(n, vector<int> ());

    for (int i = 0; i < n; i ++) {
        int k; cin >> k;
        while (k --) {
            int tmp; cin >> tmp;
            vs[i].push_back(tmp);
        }
    }

    for (int i = 0; i < n; i ++) {
        for (int j = 0; j < vs[i].size(); j ++) {
            cout << vs[i][j];
            if (j != vs[i].size() - 1) 
                cout << " ";
        }
        cout << endl;
    }

    return 0;
}

晴问算法——set的常见用法详解

#include <iostream>
#include <set>

using namespace std;

int main() {
    set<int> s;
    int n; cin >> n;
    int tmp;
    for (int i = 0; i < n; i ++) {
        cin >> tmp;
        s.insert(tmp);
    }

    for (set<int>::iterator it = s.begin(); it != s.end(); it ++) {
        if (it != s.begin()) 
            cout << " ";
        cout << *it;
    }

    return 0;
}
#include <iostream>
#include <set>

using namespace std;

int main() {
    set<int> s;
    int n, x; 
    cin >> n >> x;
    int tmp;
    for (int i = 0; i < n; i ++) {
        cin >> tmp;
        s.insert(tmp);
    }

    // find()函数返回寻找元素的迭代器位置,若不存在该元素,则返回迭代器end()
    // erase()函数可以借助迭代器删除某个位置或者直接删除某元素;也可以使用迭代器进行区间删除
    set<int>::iterator it = s.find(x);
    if (it != s.end()) s.erase(it);
    for (set<int>::iterator it = s.begin(); it != s.end(); it ++) {
        if (it != s.begin()) 
            cout << " ";
        cout << *it;
    }

    return 0;
}
#include <iostream>
#include <set>

using namespace std;

int main() {
    set<int> s;
    int n, x; 
    cin >> n >> x;
    int tmp;
    for (int i = 0; i < n; i ++) {
        cin >> tmp;
        s.insert(tmp);
    }

    set<int>::iterator it = s.find(x);
    if (it != s.end()) s.erase(it);
    for (set<int>::iterator it = s.begin(); it != s.end(); it ++) {
        if (it != s.begin()) 
            cout << " ";
        cout << *it;
    }

    return 0;
}
#include <iostream>
#include <set>

using namespace std;

int main() {
    set<int> s;
    int n; cin >> n;
    int tmp;
    for (int i = 0; i < n; i ++) {
        cin >> tmp;
        s.insert(tmp);
    }

    s.clear();
    cout << s.size() << endl;

    return 0;
}

晴问算法——string-输入与输出

#include <iostream>
#include <cstring>

using namespace std;

int main() {
    string s;
    cin >> s;
    cout << s << endl;

    return 0;
}
#include <iostream>
#include <cstring>

using namespace std;

int main() {
    string s;
    getline(cin, s);
    cout << s << endl;

    return 0;
}
#include <iostream>
#include <cstring>

using namespace std;

int main() {
    string s1, s2;
    cin >> s1 >> s2;
    cout << s1 + s2 << endl;

    return 0;
}
#include <iostream>
#include <cstring>

using namespace std;

int main() {
    string s1, s2;
    cin >> s1 >> s2;
    if (s1 == s2) cout << 0 << endl;
    else cout << (s1 > s2 ? 1 : -1) << endl;

    return 0;
}
#include <iostream>
#include <cstring>

using namespace std;

int main() {
    string s; cin >> s;
    cout << s.length() << " ";
    s.clear();
    cout << s.length() << endl;

    return 0;
}
#include <iostream>
#include <cstring>

using namespace std;

int main() {
    string s; cin >> s;
    int k; char c;
    cin >> k >> c;
    s.insert(s.begin() + k, c);
    cin >> k;
    s.erase(s.begin() + k);
    cout << s << endl;

    return 0;
}
#include <iostream>
#include <cstring>

using namespace std;

int main() {
    string s; cin >> s;
    int k, len; cin >> k >> len;
    cout << s.substr(k, len);

    return 0;
}
#include <iostream>
#include <cstring>

using namespace std;

int main() {
    string s1, s2;
    cin >> s1 >> s2;

    // find会返回找到的第一个位置,没找到则返回string::npos,所以需要先使用一个int去接收
    int pos = s1.find(s2);
    cout << pos << endl;

    return 0;
}
#include <iostream>

using namespace std;

int main() {
    string s; cin >> s;
    int k, len; cin >> k >> len;
    string s1; cin >> s1;

    // replace是将某个区间取代为某字符串
    s.replace(k, len, s1);
    cout << s << endl;

    return 0;
}

晴问算法——map的常见用法详解

#include <iostream>
#include <map>

using namespace std;

int main() {
    int n; cin >> n;
    map<char, int> mp;
    char c; int tmp;
    for (int i = 0; i < n; i ++) {
        cin >> c >> tmp;
        mp[c] = tmp;
    }

    for (map<char, int>::iterator it = mp.begin(); it != mp.end(); it ++)
        cout << it->first << " " << it->second << endl;
    
    return 0;
}
#include <iostream>
#include <map>

using namespace std;

int main() {
    int n; cin >> n;
    map<char, int> mp;
    char c; int tmp;
    for (int i = 0; i < n; i ++) {
        cin >> c >> tmp;
        mp[c] = tmp;
    }

    char key; cin >> key;
    map<char, int>::iterator it = mp.find(key);
    if (it == mp.end()) cout << -1 << endl;
    else cout << it->second << endl;
    
    return 0;
}
#include <iostream>
#include <map>

using namespace std;

int main() {
    int n; cin >> n;
    map<char, int> mp;
    char c; int tmp;
    for (int i = 0; i < n; i ++) {
        cin >> c >> tmp;
        mp[c] = tmp;
    }

    char key; cin >> key;
    mp.erase(key);
    for (map<char, int>::iterator it = mp.begin(); it != mp.end(); it ++)
        cout << it->first << " " << it->second << endl;
    
    return 0;
}
#include <iostream>
#include <map>

using namespace std;

int main() {
    int n; cin >> n;
    map<char, int> mp;
    char c; int tmp;
    for (int i = 0; i < n; i ++) {
        cin >> c >> tmp;
        mp[c] = tmp;
    }

    cout << mp.size() << " ";
    mp.clear();
    cout << mp.size() << endl;
    
    return 0;
}
#include <iostream>
#include <map>

using namespace std;

int main() {
    int n; cin >> n;
    map<string, int> mp;
    string str;
    for (int i = 0; i < n; i ++) {
        cin >> str;
        map<string, int>::iterator it = mp.find(str);
        if (it == mp.end()) mp[str] = 1;
        else mp[str] ++;
    }
    for (map<string, int>::iterator it = mp.begin(); it != mp.end(); it ++)
        cout << it->first << " " << it->second << endl;
    
    return 0;
}