70分救救孩子

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

jiejielove @ 2023-01-28 22:38:52


#include<iostream>
#include<stdio.h>
#include<math.h>
#include<iomanip>
#include<string>
using namespace std;

int main()
{
    string s;
    getline(cin, s);
    int i,j;
    char p;
    for ( i = 0; i < s.length(); i++)
    {
        p = s[i];
        for ( j = i + 1; j < s.length(); j++)
        {
            if (p == s[j])
            {
                s[i] = '-1';
                s[j] = '-1';
            }
        }
    }
    for (i = 0; i < s.length(); i++)
    {
        if (!isdigit(s[i]))
        {
            cout << s[i];
            return 0;
        }

    }
    cout << "no";
    return 0;
}

by jiejielove @ 2023-01-28 22:48:18

前三个ER,前三个答案是no,直接打印no前三个通过,后七个输出字母都没错。这就有点不合理了


by VictoriaEVA @ 2023-01-28 23:21:33

@jiejielove

强推桶做法 ( O(len^2) => O(len) ):

#include<bits/stdc++.h>
using namespace std;
#define ff(r,n) for(int r=1;r<=n;++r)
string s;
int t[26];
int main()
{
    getline(cin,s);
    int len=s.length()-1;
    ff(i,len)
        t[s[i]-'a']++;
    ff(i,26)
        if(t[i]==1)
        {
            printf("%c",i+'a');
            return 0;
        }
    puts("no");
    return 0;
}

by Tsuki327 @ 2023-01-28 23:27:09

@jiejielove 您的代码估计是 getline 读入了换行字符,在所有都不止出现过一次的情况下把换行字符当作了答案,所以没有输出 no


by Tsuki327 @ 2023-01-28 23:27:49

改成 cin 就过了


by jiejielove @ 2023-01-29 01:03:15

@OctIsYourCSPJ 老哥一针见血的,谢谢老哥


by Lazy_King @ 2023-03-04 21:26:00

@OctIsYourCSPJ 老哥一针见血,牛逼!!


|