注意题目并没有限制字符串中所有字符均为字母,所以要考虑到所有字符的情况。
#include<bits/stdc++.h> using namespace std; const int N =1010; int cnt[N]; int main() { string s; for (; cin >> s; memset(cnt, 0, sizeof(cnt))) { for (const auto &c : s) cnt[c]++; bool is_found = false; for (const auto &c : s) { if (cnt[c] == 1) { is_found = true; cout << c << endl; break; } } if (!is_found) cout << -1 << endl; } return 0; }