3 Star 14 Fork 6

徒步天下 / 程序设计与算法一OpenJudge题解

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
041:找第一个只出现一次的字符 1.09 KB
一键复制 编辑 原始数据 按行查看 历史
# 041:找第一个只出现一次的字符
## 总时间限制: 1000ms 内存限制: 65536kB
## 描述
给定一个只包含小写字母的字符串,请你找到第一个仅出现一次的字符。如果没有,输出no。
### 输入
一个字符串,长度小于100000。
### 输出
输出第一个仅出现一次的字符,若没有则输出no。
### 样例输入
abcabd
### 样例输出
c
### 全局题号
7804
## 题解
```
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
char s[100001];
int a[26], i, b, x=-2, y=-2;
cin.get(s,100000);
for (i=0;i<26;i++)
a[i]=-2;
for (i=0;i<100000;i++)
{
if (s[i]=='\0')
break;
b=s[i]-'a';
if (a[b]==-2)
a[b]=i;
else if (a[b]>=0)
a[b]=-1;
}
for (i=0;i<26;i++)
{
if (a[i]>=0 && (a[i]<x || x==-2))
{
x=a[i];
y=i;
}
}
if (y==-2)
cout << "no" << endl;
else
cout << (char)('a'+y) <<endl;
return 0;
}
```
C++
1
https://gitee.com/se17a/c01.git
git@gitee.com:se17a/c01.git
se17a
c01
程序设计与算法一OpenJudge题解
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891