70分

B2110 找第一个只出现一次的字符

VastUniverse_Hory @ 2022-09-20 13:19:21

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios :: sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    unordered_map <int, int> t;
    string a;
    set <int> o;
    getline(cin, a);
    for (char & i: a)
    {
        t[i] ++;
    }
    for (int i = 0; i <= 1000; i ++)
        if (t[i] == 1)
            o.insert(i);
    bool has = false;
    for (int i = 0; i < a.size(); i ++)
        if (o.count(a[i]))
        {
            cout << a[i] << endl;
            has = true;
            break;
        }
    if (! has)
        cout << "no" << endl;
    return 0;
}

C++


by VastUniverse_Hory @ 2022-09-20 13:29:23

但似乎用cin就可以

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios :: sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    unordered_map <int, int> t;
    string a;
    cin >> a;
    for (char & i: a)
    {
        t[i] ++;
    }
    bool has = false;
    for (int i = 0; i < a.size(); i ++)
        if (t[a[i]] == 1)
        {
            has = true;
            cout << a[i] << endl;
            break;
        }
    if (! has)
        cout << "no" << endl;
    return 0;
}

|