第一行,一个正整数T(T≤20) ,表示输入数据组数。
之后T行,每行一个字符串S。( 1≤S 的长度≤1000000 ,保证字符串中出现的字符的ASCII码在[0x21,0x7F)范围内,即均为可显示的非空白符,同时保证一定有解)
一共T 行,每行一个字符C ,表示所给的相应字符串中第一个只出现一次的字符。
2 abaccdeff testonline
b s
from collections import defaultdict
num = int(input())
for _ in range(num):
a, dd = input(), defaultdict(int)
for i in a:
dd[i] += 1
for i in a:
dd[i] += 1
if dd[i] == 2:
print(i)
break
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.LinkedHashSet;
public class CrackMe {
private static BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
private static String t;
static{
try {
t=in.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
int T=Integer.parseInt(t);
char[] result=new char[T];
for(int i=0;i<T;i++){
try {
result[i]=findFirstUnique(in.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for(char a:result){
System.out.println(a);
}
}
private static char findFirstUnique(String string) {
char[] chars=string.toCharArray();
HashSet<Character> doorSet=new HashSet<Character>();
LinkedHashSet<Character> resultSet=new LinkedHashSet<Character>();
char c;
for(int i=0;i<chars.length;i++){
c=chars[i];
if(!doorSet.contains(c)){
if(!resultSet.add(c)){
resultSet.remove(c);
doorSet.add(c);
}
}
}
return resultSet.iterator().next();
}
}
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
// 使用字符流输入
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
int n = Integer.parseInt(reader.readLine());
for (int i = 0; i < n; i++) {
String str = reader.readLine();
System.out.println(serch(str));
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static char serch(String str) {
// 只出现一次的字符记录组
ArrayList<Character> one = new ArrayList<>();
// 不止出现一次的记录set
HashSet<Character> set = new HashSet<>();
for (char c : str.toCharArray()) {
if (!set.contains(c)) {// 不是出现多次的
if (!one.contains(c)) {// 不存在与字符组中
one.add(c);// 入列
} else {// 存在字符组中
one.remove(one.indexOf(c));// 出列
set.add(c);// 入回收记录set
}
}
}
if (one.size() > 0) {// 存在只出现一次的,输出第一个
return one.get(0);
}
return '\0';
}
}
#include <cstdio>
void init(int *ct){
for(int i=0x21;i<0x7f-1;++i){
ct[i]=0;
}
};
int main(){
//init
int ct[0x7f];
int n;
int ser;
int *r;
int t;
scanf("%d",&n);
getchar();
//fflush(stdin);
while(n--){
init(ct);
ser=1;
do{
t=getchar();
//putchar(t);
//printf("%d\n",t);
//++ct[t];
ct[t]=ct[t]?-1:ser++;
}while(t>=0x21);
//fflush(stdin);
int *res=ct;
int min=0xff;
for(r=ct+0x21;r-ct<0x7f-1;++r){
if(*r>0 && *r < min){
min=*r;
res=r;
}
}
printf("%c\n",res-ct);
}
}
利用桶排序的原理。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node{
int val;
int first;
};
int main(){
int n;
scanf("%d",&n);
int pos;
while(n--){
char a[1000001];
node b[95] ;
for(int i = 0; i < 95; i++){
b[i].val = 0;
b[i].first = -1;
}
scanf("%s",a);
for(int i = 0; i < strlen(a); i++){
pos = ((int)a[i])-33;
(b[pos].val)++;
if(b[pos].first == -1) b[pos].first = i;
}
int minpos = 100000000;
for(int i = 0; i < 94; i++){
if(b[i].val == 1 && b[i].first != -1){
if(b[i].first < minpos) minpos = b[i].first;
}
}
//printf("%d",minpos);
printf("%c\n",a[minpos]);
}
return 0;
}
PHP 的来一发
<?php
$n = trim(fgets(STDIN));
$arr = [];
for($i=0; $i<$n; $i++){
$arr[] = trim(fgets(STDIN));
}
$r = [];
for($j=0; $j<$n; $j++){
$res = [];
for($m=0; $m<strlen($arr[$j]); $m++){
if(array_key_exists($arr[$j][$m], $res)){
$res[$arr[$j][$m]]++;
}else{
$res[$arr[$j][$m]]=1;
}
}
foreach($res as $k=>$v){
if($v==1){
$r[]=$k;
break;
}
}
}
foreach($r as $v){
echo $v.PHP_EOL;
}
#include <stdio.h>
#include <string>
#include <iostream>
#include <map>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
string str;
while(n--)
{
cin>>str;
map<char,pair<int,int>> mymap;
for(int i=0;i<str.size();i++)
{
if(mymap.count(str[i]))mymap[str[i]].first++;
else mymap.insert(make_pair(str[i],make_pair(1,i)));
}
int index = str.size();
char ans;
for(auto it:mymap)
{
if(it.second.first == 1&& it.second.second<index){ans = it.first;index = it.second.second;}
}
cout<<ans<<endl;
}
}
return 0;
}
}
import sysn=int(sys.stdin.readline().strip())for i in range(n):line=sys.stdin.readline().strip()d={}for i in line:if d.has_key(i):d[i]+=1else:d[i]=1for i in line:if d[i]==1:print ibreak
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef long long ll;
const int MAXN=1000000;
int idx[256];
int T;
char str[MAXN+5];
int main(){
for(scanf("%d",&T);T--;){
scanf("%s",str+1);
memset(idx,0,sizeof(idx));
for(int i=1;str[i];i++){
if(idx[str[i]]==0){
idx[str[i]]=i;
}else{
idx[str[i]]=-1;
}
}
char ans='\0';
for(int i=0x21;i<0x7F;i++){
if(idx[i]>0){
if(ans==0||idx[ans]>idx[i]) ans=i;
}
}
printf("%c\n",ans);
}
return 0;
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
int T = 0;
// InputStreamReader 是字节流通向字符流的桥梁;
// 为了达到最高效率,可要考虑在 BufferedReader 内包装 InputStreamReader。例如:
// BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
try {
T = Integer.parseInt(bufferedReader.readLine());
for(int i=0; i<T; i++){
String str = bufferedReader.readLine();
System.out.println(firstAppearsOnlyonce(str));
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 找字符串中第一个只出现一次的字符
*/
private static char firstAppearsOnlyonce(String str) {
int[] hash = new int[256]; //记录每个字符对应的个数,共有256个ASCII码
for(int i=0; i<256; i++){
hash[i] = 0;
}
for(int i=0; i<str.length(); i++){
hash[str.charAt(i)] ++; //建立一个字符与个数反映射关系!
}
for(int i=0; i<str.length(); i++){ //再遍历一遍字符串,找第一个出现一次字符
if(hash[str.charAt(i)] == 1){
return str.charAt(i);
}
}
return '\0';
}
}
#include<iostream>
#include<string>
#include<unordered_map>
using namespace std;
int main(){
int i,n;
cin >> n;
while( n-- ){
unordered_map<char,int> mm;
string str;
cin >> str;
for(i=0;i<str.size();i++)
mm[ str[i] ] ++ ;
for(i=0;i<str.size();i++){
if(mm[ str[i] ] == 1){
cout << str[i] << endl;
break;
}
}
}
return 0;
}
//
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(){
int i,n;
scanf("%d",&n);
while( n-- ){
char ans='\0';
int mm[256]={0};
char str[1000005];
scanf("%s",str);
for(i=0;str[i];i++){
if(mm[ str[i] ] == 0)
mm[ str[i] ] = i+1;//首次出现过,记录其下标。
else
mm[ str[i] ] = -1;//重复出现。则不用再记录。
}
for(i=0x21;i<0x7f;i++){
if(mm[ i ] > 0 ){
if(ans == 0 || mm[i]<mm[ans])
ans = i ;
}
}
printf("%c\n",ans);
}
return 0;
}
//
#include<stdio.h>
#include<string.h>
int main(){
int i,n;
scanf("%d",&n);
while( n-- ){
int mm[130]={0};
char str[1000005];
scanf("%s",str);
for(i=0;str[i];i++)
mm[ str[i] ] ++ ;
for(i=0;str[i];i++){
if(mm[ str[i] ] == 1){
printf("%c\n",str[i]);
break;
}
}
}
return 0;
}