有什么问题?

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

oldnet @ 2023-02-17 20:21:54

30分

#include<bits/stdc++.h>

using namespace std;

int main() {
    string a;
    getline(cin, a);
    int n = 0;
    bool b = false;
    for (int i = 0; i < a.size(); i++) {
        if (a[i - 1] == a[i]) {
            n++;
            b = true;
        }
        if (a[i - 1] != a[i]) {
            n = 0;
            b = false;
        }
        if (n == 0 && !b) {
            cout << a[i];
            return 0;
        }
    }
    cout << "no";
    return 0;
}

by Martlet @ 2023-02-18 08:07:32

@oldnet 你里面a[i-1]会越界

#include<bits/stdc++.h>

using namespace std;
int k[100010];
int main() {
    string a;
    getline(cin, a);
    int n = 0;
    bool b = false;
    for (int i = 0; i < a.size(); i++){
         int vis = int(a[i]-'a');
         k[vis]++;
    }
    for(int i = 0;i < 26;i++){
        if(a[i] == 1){
            cout<<char(i+'a');
            return 0;
        }
    }
    cout << "no";
    return 0;
}

代码没交可能会有一点点错,因为是直接手码的。不过思路没有问题。


by Loser_Syx @ 2023-02-18 08:09:17

@Martlet 你这肯定是错的,题目找的不是字典序最小的


by Loser_Syx @ 2023-02-18 08:09:50

@Martlet 而且 a[i] == 1


by oldnet @ 2023-02-18 08:10:26

@Martlet 全部RE


by Loser_Syx @ 2023-02-18 08:12:46

@oldnet 其实就是先统计,然后遍历字符串找


by oldnet @ 2023-02-18 08:14:04

@Saint_ying_xtf 谢谢


by Martlet @ 2023-02-18 08:14:15

@oldnet

#include<bits/stdc++.h>

using namespace std;
int k[100010];
string a;
int main() {
    cin>>a;
    int n = 0;
    bool b = false;
    for (int i = 0; i < a.size(); i++){
         int vis = int(a[i]-'a');
         k[vis]++;
    }
    for(int i = 0;i < a.size();i++){
         int vis = int(a[i]-'a');
         if(k[vis] == 1){
             cout<<char(vis+'a');
             return 0;
         }
    }
    cout << "no";
    return 0;
}

改了过了


by Loser_Syx @ 2023-02-18 08:14:39

@oldnet 等下,我代码漏打了一个中括号


by Loser_Syx @ 2023-02-18 08:15:05

#include <bits/stdc++.h>
using namespace std;
int a[26];
char s[100001];
int main(){
    scanf("%s", s);
    bool f = false;
    int len = strlen(s);
    for(int i = 0; i < len; i++){
        a[s[i] - 97]++;
    }
    for(int i = 0; i < len; i++){
        if(a[s[i] - 97] == 1){
            cout << s[i];
            f = true;
            break;
        }
    }
    if(f == false){
        cout << "no";
    }
    return 0;
}

|