给定一个字符串,请写一段代码找出这个字符串中首先出现三次的那个英文字符(需要区分大小写)。
数据范围:字符串长度满足 
//用map<char,int> 存储数据,对每一个读取的数据进行判断
#include<iostream>
#include <map>
using namespace std;
int main()
{
map<char, int> letter;
char l;
while (cin >>l)
{
if((l>='a'&&l<='z')||l>='A'&&l<='Z')
{
++letter[l];
if (letter[l] == 3)
{
cout << l;
break;
}
}
}
system("pause");
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
getline(cin, str);
int a[1000] = { 0 };
for (int i = 0;i<str.size();++i)
{
if(str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z')
++a[str[i]];
if(a[str[i]]==3)
{
printf("%c\n",str[i]);
break;
}
}
return 0;
} 用ASCII码哈希 O(n)
#include <iostream>
#include <string>
using namespace std;
int ha[256];
int main(){
string s;
while(cin>>s){
for(int i = 0; i < s.length(); i++){
if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z')){
ha[int(s[i])]++;
if(ha[int(s[i])] == 3){printf("%c\n", s[i]); return 0;}
}
}
}
return 0;
}
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
char[] arr = sc.nextLine().replaceAll("[^a-zA-Z]", "").toCharArray();
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < arr.length; i ++) {
map.put(arr[i], map.containsKey(arr[i]) ? map.get(arr[i]) + 1 : 1);
if(map.get(arr[i]) == 3) {
System.out.println(arr[i]);
break;
}
}
}
}
}
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
StringBuilder str = new StringBuilder();
str.append(sc.nextLine());
char[] c = str.toString().trim().toCharArray();
int[] a = new int[127];
for(char s : c){
a[s]++;
if((s >= 'a' && s <= 'z' || s >= 'A' && s <= 'Z') && a[s] == 3){
System.out.print(s);
break;
}
}
}
} 好像看了下没用正则的?
import re
print(re.match('.*([a-zA-Z]).*\\1.*\\1.*', input()[::-1]).group(1))
#include<stdio.h>
#include<string.h>
char s[1000000];
int book[500],i,res;
int main(){
while(gets(s)){
memset(book,0,sizeof(book));
for(i=0;s[i]!='\0';i++)
if(('0'<=s[i]&&s[i]<='9'||
('a'<=s[i]&&s[i]<='z'||'A'<=s[i]&&s[i]<='Z'))
&&++book[s[i]]==3){
printf("%c\n",s[i]);
break;
}
}
}
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
string s;
while(getline(cin,s))
{
map<char, int> m;
for (int i = 0; i < s.length(); i++)
{
m[s[i]]++;
//if(((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z'))&&m[s[i]]>=3)
//tolower(),大写转小写,同toupper(),小写转大写
if(tolower(s[i])>='a'&&tolower(s[i])<='z'&&m[s[i]]>=3)
{
cout<<s[i]<<endl;
break;
}
}
/*map输出的一般用法举例
map<char, int>::iterator iter;
for (iter = m.begin(); iter !=m.end(); iter++)
{
if (((iter->first>='a'&&iter->first<='z')||(iter->first>='A'&&iter->first<='Z'))&&iter->second>=3)
{
cout <<iter->first<< endl;
break;
}
}
*/
}
return 0;
} 注意,是字母,其他的不予以统计,坑逼题。调试了半天,怀疑人生
<?php
$str = trim(fgets(STDIN));
$arr = [];
for($i=0; $i<strlen($str); $i++){
if($str[$i]>='A' && $str[$i]<='z'){
if(array_key_exists($str[$i], $arr)){
$arr[$str[$i]]++;
if($arr[$str[$i]] == 3){
echo $str[$i];
break;
}
}else{
$arr[$str[$i]]=1;
}
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String sentence;
while((sentence = br.readLine()) != null)
solve(sentence.toCharArray());
}
private static char solve(char[] sentence) {
int[] counter = new int[128];
for(int i = 0; i < sentence.length; i++){
if((sentence[i] >= 'a' && sentence[i] <= 'z') ||
(sentence[i] >= 'A' && sentence[i] <= 'Z')){
counter[sentence[i]] ++;
if(counter[sentence[i]] == 3){
System.out.println(sentence[i]);
break;
}
}
}
return ' ';
}
}
import java.io.BufferedInputStream;
import java.io.IOException;
public class Main {
public static void getChar(String s) {
char[] c = s.toCharArray();
for (int i = 2; i < s.length(); i++) {
if (c[i] < 65 || c[i] > 122) {
continue;
}
String sb = s.substring(0, i);
int l1 = sb.indexOf(c[i]);
if (l1 >= 0) {
int l2 = sb.lastIndexOf(c[i]);
if (l1 != l2) {
System.out.println(c[i]);
break;
}
}
}
}
public static void main(String[] args) {
BufferedInputStream bis = new BufferedInputStream(System.in);
byte[] b = new byte[1024];
try {
bis.read(b);
String str = new String(b);
getChar(str);
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.util.Scanner;
import java.util.Map;
import java.util.TreeMap;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
char[] ch = sc.nextLine().toCharArray();
Map<Character,Integer> map = new TreeMap<>();
for(int i=0;i<ch.length;i++){
if((ch[i] >='a' && ch[i]<='z') || (ch[i] >='A' && ch[i] <='Z')){
if(map.containsKey(ch[i])){
map.put(ch[i],map.get(ch[i])+1);
if(map.get(ch[i]) == 3){
System.out.println(ch[i]);
break;
}
}else{
map.put(ch[i],1);
}
}
}
}
}
}
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
int main() {
unordered_map<char, int> dir;
string s;
getline(cin, s);
for (int i = 0, m = s.size(); i < m; i++) {
if (isalpha(s[i]) && ++dir[s[i]] == 3) {
cout << s[i] << endl;
break;
}
}
return 0;
}