题目地址: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; }
原文:http://blog.csdn.net/jdplus/article/details/19290235