首页 > 试题广场 >

三次字符

[编程题]三次字符
  • 热度指数:1213 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定一个英文字符串(包括空格和换行),请找出该字符串中首次出现三次的英文字母(字符需区分大小写) 。如果不存在则输出-1;

输入描述:
输入一个字符串,可包含数字、字母,长度不超过106 个字符


输出描述:
输出第一个出现三次的英文字母,不存在则输出“-1”
示例1

输入

i love Kingsoft Office

输出

f
示例2

输入

I love KingsoFt Office

输出

-1
#include <iostream>
#include <set>
int main()
{
    std::string str;
    getline(std::cin,str);

    std::multiset<char> chset;
    for (auto ch : str)
    {
        if((ch >='A'&&ch <='Z') ||(ch >='a'&&ch<='z'))
            chset.insert(ch);
        if (chset.count(ch) == 3)
        {
            std::cout << ch;
            return 0;
        }
    }
    std::cout << -1 << std::endl;

    return 0;

}
发表于 2022-03-07 17:51:54 回复(0)
#include<iostream>
#include<string>
using namespace std;

int main()
{
    string s;//定义一个字符串变量
    getline(cin,s);//从标准输入流中获取字符串
    int a[127]={0};//定义一个整型数组来存取字母出现次数
    for(int i=0;i<s.length();i++)
    {
        a[s[i]]++;//记录字符串中每个字符出现的次数
        if(a[s[i]]==3&&isalpha(s[i]))//判断字符出现次数是否为3以及该字符要为字母
        {
            //若是,则输出该字符,并结束程序
            cout<<s[i]<<endl;
            return 0;
        }
    }
    //程序运行到此处,则说明没有出现超高3次的英文字母
    cout<<-1<<endl;
}

发表于 2022-02-15 16:46:23 回复(0)

直接利用数组就可以解决了,很简单

#include<iostream>
#include<string>
#include<cstring>
#include<vector>

using namespace std;

int main(void){
    vector<int> container(127,0);
    string str;
    getline(cin,str);
    for(int pos = 0;pos<str.length();pos++){
        if(++container[str[pos]]>=3&&isalpha(str[pos])){
            cout<<str[pos]<<endl;
            return 0;
        }
    }
    cout<<-1<<endl;
    return 0;
}
  • 使用一个数组存下所有字符的个数,等于3的直接输出返回,结束程序
  • 时间复杂度为O(n2
发表于 2020-02-16 14:52:06 回复(2)
import sys

for line in sys.stdin:
    char_count={}
    result_char='-1'
    for char in line:
        if char.isalpha():
            char_count[char]=char_count.get(char,0)+1
            if char_count[char]==3:
                result_char=char
                break
    print(result_char)

发表于 2024-03-27 21:57:31 回复(0)
#include <iostream>
#include <string>
#include <unordered_map>

int main() {
    std::string inputString;
    std::getline(std::cin, inputString);

    std::unordered_map<char, int> charCount;

    for (char c : inputString) {
        if (std::isalpha(c)) {  // 检查字符是否是英文字母
            charCount[c]++;
            if (charCount[c] == 3) {
                std::cout << c << std::endl;
                return 0;
            }
        }
    }

    std::cout << "没有出现三次的英文字母。" << std::endl;
    return 0;
}
发表于 2023-09-14 10:55:13 回复(0)
#include <iostream>
#include <string>
#include <unordered_map>

bool findstr(const std::string &str,char &c)
{
    std::unordered_map<char, int>s_map;
    bool b_flag = false;
    for (auto it:str)
    {
        if ((it>='a' && it <='z') ||(it >='A' &&it<='Z'))
        {
            if (auto iter = s_map.find(it); iter != s_map.end())
            {
                if (iter->second == 2)
                {
                    c = it;
                    b_flag = true;
                    break;
                }
                else
                {
                    s_map.emplace(it, ++iter->second);
                    continue;
                }
            }
            else
                s_map.emplace(it, 1);
        }
        
    }
    return b_flag;
}

int main()
{
    char c;
    std::string str;
    getline(std::cin,str);
    if (findstr(str, c))
        std::cout << c;
    else
        std::cout << -1;
    return 0;
}
发表于 2022-03-15 22:50:20 回复(0)
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main(){
    char ch;
    map<char, int> counter;
    while (ch=cin.get()) {
        if (cin.eof())
            break;
        if (isalpha(ch)) {
            counter[ch]++;
            if (counter[ch] == 3) {
                cout << ch;
                return 0;
            }
        }
    }
    cout << "-1" << endl;
    return 0;
}

发表于 2020-06-04 19:47:50 回复(0)
python方法解决,使用字典的方式进行数据存储,使用ASCII码值判断字母 
string_content = input()
dict_str = {}
flag = 0 
for str in string_content:
    if (65 <= ord(str) and ord(str) <= 90)&nbs***bsp;(97 <= ord(str) and ord(str) <= 122):
        if str not in dict_str:
            dict_str[str] = 1
        else:
            dict_str[str] += 1
            if dict_str[str] == 3:
                print(str)
                flag = 1 
                break 
if flag == 0:
    print("-1")



发表于 2020-02-26 19:22:18 回复(0)