第一个只出现一次的字符
Time Limit:1000MS Memory Limit:65536K
Total Submit:10 Accepted:7
Description
在一个字符串(1<=字符串长度<=10000,全部由大写字母组成)中找到第一个只出现一次的字符。
Input
输入有多组数据
每一组输入一个字符串。
Output
输出第一个只出现一次的字符下标,没有只出现一次的字符则输出-1。
Sample Input
ABACCDEFF
AA
Sample Output
1
-1
Source
#include<iostream>
using namespace std;
int f(string a)
{
int count=0;
int b[10001]= {0};
for(int i=0; i<a.size(); i++)//统计各个字母出现的次数
for(int j=0; j<a.size(); j++)
{
if(a[i]==a[j])
b[i]++;
}
for(int i=0; i<a.size(); i++)
{
if(b[i]==1)
{
count=i;
break;
}
}
if(count<a.size())return count;
else if(count>=a.size()) return false;
}
int main()
{
string a;
while(cin>>a)
{
if(f(a))cout<<f(a)<<endl;
else cout<<"-1"<<endl;
}
return 0;
}