题目地址:http://ac.jobdu.com/problem.php?pid=1283
-
题目描述:
-
在一个字符串(1<=字符串长度<=10000,全部由大写字母组成)中找到第一个只出现一次的字符。
-
输入:
-
输入有多组数据
每一组输入一个字符串。
-
输出:
-
输出第一个只出现一次的字符下标,没有只出现一次的字符则输出-1。
-
样例输入:
-
ABACCDEFF AA
-
样例输出:
-
1 -1
#include <stdio.h>
int main(void){
char str[10001];
int hash[256];
int i;
while (scanf ("%s", str) != EOF){
for (i=0; i<256; ++i)
hash[i] = 0;
i=0;
while (str[i]){
++hash[str[i]];
++i;
}
i = 0;
while (str[i] && hash[str[i]] != 1)
++i;
if (str[i])
printf ("%d\n", i);
else
printf ("-1\n");
}
return 0;
}
参考资料: 何海涛 -- 程序员面试题精选100题(13)-第一个只出现一次的字符[算法]