首页 > 试题广场 >

单词识别

[编程题]单词识别
  • 热度指数:17315 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
输入一个英文句子,把句子中的单词(不区分大小写)按出现次数按从多到少把单词和次数在屏幕上输出来,次数一样的按照单词小写的字典序排序输出,要求能识别英文单词和句号

输入描述:
输入为一行,由若干个单词和句号组成


输出描述:
输出格式参见样例。
示例1

输入

A blockhouse is a small castle that has four openings through which to shoot.

输出

a:2
blockhouse:1
castle:1
four:1
has:1
is:1
openings:1
shoot:1
small:1
that:1
through:1
to:1
which:1
测试样例简直有毒。。4x4)和(at都算单词
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> typedef struct str0 {     char ch[1000];     int count;     int flag; } str1;
str1 str[1000]; str1 temp; int main() {
    int i = 0;     while(scanf("%s", str[i].ch) != EOF) {         i++;     }     int sum = i;     int j;     for(i = 0; i < sum; i++) {         str[i].flag = 1; //一开始都不是克隆的     }     for(i = 0; i < sum; i++) {         int len = strlen(str[i].ch);         for(j = 0; j < len; j++) {             if(str[i].ch[j] >= 'A' && str[i].ch[j] <= 'Z') {                 str[i].ch[j] = str[i].ch[j] - 'A' + 'a';                 //变成小写字母             } else if(str[i].ch[j] < 'a' || str[i].ch[j] > 'z') {                 //不是小写字母                 //删除这个字符                 if((str[i].ch[j]=='.'||str[i].ch[j]==',')&&j==len-1)                     str[i].ch[j] = '\0';         }       }     }
    for(i = 0; i < sum; i++) {         str[i].count = 1;         if(str[i].flag == 1) {  //这个不是克隆的,就可以记数了             for(j = i + 1 ; j < sum; j++) {                 if(strcmp(str[i].ch, str[j].ch) == 0) {                     str[i].count++;                     str[j].flag = 0; //这个是克隆的!                 }             }         }     }     //记数都完成了,现在要字典序输出!     //冒泡排序     for(i = 0; i < sum - 1; i++) {         for(j = 0; j < sum - 1 - i; j++) {             if(strcmp(str[j].ch, str[j + 1].ch) > 0) { //前面的字符串更大                 //交换                 temp = str[j];                 str[j] = str[j + 1];                 str[j + 1] = temp;             }         }     }     //现在开始输出     for(i = 0; i < sum; i++) {         if(str[i].flag == 1)             printf("%s:%d\n", str[i].ch, str[i].count);     }     return 0; }

发表于 2019-03-08 22:42:26 回复(1)
#include<bits/stdc++.h>

using namespace std;

void Trains(string& str)
{
    for(int i = 0;i < str.length(); i++)
    {
        if(isupper(str[i])) str[i] = tolower(str[i]);
        if(str[i] == ',' || str[i] == '.') str[i] = ' ';
    }
}

int main()
{
    ios::sync_with_stdio(false);
    string str;
    while(getline(cin, str))
    {
        map<string, int> m;
        Trains(str);
        stringstream ss(str);
        string word;
        while(ss >> word) m[word]++;
        for(pair<string, int> x : m)
            cout << x.first << ":" << x.second << "\n";
    }
    return 0;
}

编辑于 2021-01-19 19:20:03 回复(0)
强烈吐槽这测试用例,人才!
括号,数字也算单词;
按次数排序呢,只用了字典排序;
搞得正则表达式匹配老是出错。
还有什么叫对于一个句子的概念,醉了醉了
#include<iostream>
(720)#include<string>
#include<regex>
(1081)#include<vector>
#include<map>
using namespace std;
struct datas
{
	string s;
	int count;
	bool operator<(datas c)
	{
		//if (count == c.count)  
		return s < c.s;
		//else return count > c.count;
	}
};
int main()
{
	string s;
	//getline(cin, s, '.');
	while (getline(cin, s))
	{
		vector<datas> contain;
		map<string, int> sg;

		regex pattern("([)(a-zA-Z0-9]+)");   //匹配字符串
		smatch result;
		string::const_iterator str = s.begin();
		string::const_iterator str_end = s.end();
		while (regex_search(str, str_end, result, pattern))
		{
			string a = result[0];
			a[0] = tolower(a[0]);
			sg[a]++;
			str = result[0].second;    //无括号
		}
		map<string, int>::iterator it;
		for (it = sg.begin(); it != sg.end(); it++)
		{

			string k = it->first;
			//k[0] = tolower(k[0]);
			datas a;
			a.s = k;
			a.count = it->second;
			contain.push_back(a);

		}
		sort(contain.begin(), contain.end());
		for (int i = 0; i < contain.size(); i++)
		{

			cout << contain[i].s << ":" << contain[i].count << endl;

		}
	}
}

发表于 2020-03-17 17:49:07 回复(0)
注释掉了按次数排序的两行,另外我感觉应该要稳定排序吧?否则是不是会出现
cc:2
aa:2
这样的情况?
#include<iostream>
#include<map>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

void fun(pair<string,int> p)
{
	cout << p.first << ":" << p.second << endl;
}
bool cmp(pair<string,int> p1,pair<string,int> p2)
{
	return p1.second > p2.second;	
}
int main()
{
	string t;
	map<string,int> mp;
	pair<map<string,int>::iterator,int> pr;
	while(cin >> t){   
		if(t[t.length()-1] == ',' || t[t.length()-1] == '.')
			t = t.substr(0,t.length()-1);  // 左闭右开的区间 
		for(int i = 0; i < t.length(); i++)
        	t[i] = tolower(t[i]);
		pr = mp.insert(pair<string,int>(t,1));
		if(!pr.second)  //插入失败
			pr.first->second++; 
//		mp[t]++;  // 看大佬代码发现,以上三行可以用这一句直接代替 
	}
//	vector<pair<string,int> >  v(mp.begin(),mp.end()); //map to vector
//	stable_sort(v.begin(),v.end(),cmp);  // 个人认为排序需要稳定 
	for_each(v.begin(),v.end(),fun);  // for_each(mp.begin(),mp.end(),fun);
}


编辑于 2020-02-06 00:55:50 回复(0)
测试输出不对 可以参考我的代码和题干一样但是通过不了
a=input()
b=a.lower().replace(',',' ').replace('.',' ').split()
dict={}
for i in b:
    dict[i]=dict.get(i,0)+1
for j in sorted(dict.items(),key=lambda x:x[0],reverse=True):
    print("%s:%s"%(j[0],j[1]))


发表于 2019-03-22 12:57:12 回复(3)
import re

sentence = input()
sentence = sentence.lower()  #转大写用upper()
pattern = r'[,.\s]'
s_split = re.split(pattern, sentence)
s_none = list(filter(None, s_split)) 

result = {}
for i in s_none:
    result[i] = s_none.count(i)

for k in sorted(result):
    temp = str(k) + ':' + str(result[k])
    print(temp)

发表于 2019-03-15 11:49:12 回复(0)
//这道题的答案是按照字典序排列的,只要将map中的元素顺序输出即可。
#include <bits/stdc++.h>
using namespace std;

int main()
{
    string s;
    while(getline(cin, s))
    {
        map<string, int> mp;
        string temp;
        for(int i = 0; i < s.size(); i++)
        {
            if(s[i] == ' ' || s[i] == ',' || s[i] == '.')
            {
                if(temp != "")
                    mp[temp]++;
                temp = "";
            }
            else
            {
                temp += tolower(s[i]);
            }
        }
        for(auto it = mp.begin(); it != mp.end(); it++)
        {
            cout << it->first << ":" << it->second << endl;
        }
    }
    return 0;
}

发表于 2019-03-03 21:22:24 回复(15)
把句子中的单词(不区分大小写)按出现次数按从多到少把单词和次数在屏幕上输出来,
这句话跟样例不符,样例是根据按字典序输出
发表于 2019-02-27 09:16:52 回复(6)
#include<iostream>
#include<string>
#include<map>
#include<vector>
using namespace std;

int main()
{
    string str;
    string tmp;
    vector<string>arr;
    getline(cin,str);
     map<string,int> mp;
     multimap<int,string,greater<>> mp2;
    for(int i = 0;i<str.size();++i)
    {
            //统一小写;
        if(str[i]<='Z'&&str[i]>='A')
        {
            str[i]+='a'-'A';
        }
        if(str[i]!=' '&&str[i]!='.')
        {
             tmp+=str[i];          
        }   
        else
        {
            arr.push_back(tmp);
            tmp.clear();
        }
    }
    for(auto& e:arr)
    {
        mp[e]++;
    }
     for(auto& e:mp)
    {
        mp2.insert(make_pair(e.second,e.first));
    }
    for(auto& e:mp2)
    {
       cout<<e.second<<':'<<e.first<<endl;
    }
    
    
    return 0;
}
先过一遍map保证字节序并统计次数,再利用mutimap底层红黑树按照插入次序保持不变
(在key相等的情况下)遍历插入即可

发表于 2022-03-10 22:16:43 回复(0)
#include <iostream>
#include <map>
#include <string>
#include <cctype>
#include <algorithm>

int main() {
    std::map<std::string, int> m;
    std::string s;

    while (std::cin >> s) {
        // 转换为小写
        std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) {
            return std::tolower(c);
        });

        // 去除标点符号
        s.erase(std::remove_if(s.begin(), s.end(), [](unsigned char c) {
            return std::ispunct(c);
        }), s.end());
        m[s]++;
    }

    for (const auto& e : m) {
        std::cout << e.first << ":" << e.second << std::endl;
    }

    return 0;
}
发表于 2025-03-05 22:36:53 回复(1)
用map集合
#include <iostream>
#include <sstream>
using namespace std;
#include<map>
string upToLow(string &str){//大写变小写
    string res="";
    for(char c : str){
         if (c >= 'A' && c <= 'Z') {
            c += 32;
        }
        res+=c;
    }
    return res;
}
int main() {
    map<string,int>m;
    string str;
    getline(cin,str);
    str = upToLow(str);
    stringstream ss(str);//得到小写字符串的输入流
    string word;
    while(ss>>word){
        if(word[word.size()-1]=='.'){//去掉句号
            word = word.substr(0,word.size()-1);
        }
        m[word]+=1;
    }
    for(auto a:m){
        cout<<a.first<<":"<<a.second<<endl;
    }
}


发表于 2025-02-10 16:27:31 回复(0)
有人能告知哪里出错了吗?
import java.io.*;😪
import java.util.*;
import java.util.Map.Entry;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        while ((line = reader.readLine()) != null) {
            line = line.replace('.', ' ').toLowerCase();
            line = line.replace(',', ' ');
            String[] arr = line.split(" ");
            Set<String> set = new TreeSet();
            Map<String, Integer> map = new TreeMap<>();
            for (int i = 0; i < arr.length; i++) {
                set.add(arr[i]);
            }
            for (String s : set) {
                int a = 0;
                for (int i = 0; i < arr.length; i++) {
                    if (arr[i].equals(s)) {
                        a++;
                    }
                }
                map.put(s, a);
            }
            Comparator<Map.Entry<String, Integer>> valueComparator = new Comparator<Map.Entry<String, Integer>>() {
                @Override
                public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
                    // TODO Auto-generated method stub
                    return o2.getValue() - o1.getValue();
                }
            };
            // map转换成list进行排序
            List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
            // 排序
            Collections.sort(list, valueComparator);
            // 默认情况下,TreeMap对key进行升序排序
            for (Map.Entry<String, Integer> entry : list) {
                System.out.println(entry.getKey() + ":" + entry.getValue());
            }
        }
    }
}
发表于 2019-03-20 17:00:29 回复(1)
#include <bits/stdc++.h>
using namespace std;

int main(){
string str;
map<string ,int>word;
getline(cin,str);
for(int i=0,j=0;i<str.size();i++){
    if(str[i]==' '||str[i]=='.'){
        string s=str.substr(j,i-j);
        s[0]=tolower(s[0]);
        word[s]++;
        j=i+1;
    }
}
for(auto it=word.begin();it!=word.end();it++){
    cout<<it->first<<':'<<it->second<<endl;
}

}
发表于 2025-03-25 15:14:26 回复(0)
#include <iostream>
#include<string>
#include<map>
#include<sstream>
using namespace std;

int main() {
    string a;
    map<string,int>mp;
    getline(cin,a);
    while(a.find('.')!=-1)
        a.erase(a.find('.'));
    istringstream s(a);
    while(s>>a){
        if(a[0]<'a')
            a[0]=char(a[0]+32);
        mp[a]++;
    }

    for(map<string,int>::iterator it=mp.begin();it!=mp.end();it++){
        cout<<it->first<<":"<<it->second<<endl;
    }
    return 0;
}

发表于 2025-02-19 02:51:45 回复(0)
哈希表+优先级队列+仿函数Compare
#include <iostream>
#include <unordered_map>
#include <utility>
#include <string>
#include <queue>
#include <unordered_map>
#include <vector>
using namespace std;

int main() 
{
    // 仿函数比较,建大堆,返回小于的比较(孩子比较父亲)
    struct Compare
    {
        bool operator()(const pair<string, int>& p1, const pair<string, int>& p2)
        {
            return p1.second < p2.second || (p1.second == p2.second && p1.first > p2.first);
        }
    };
    string s;
    getline(cin, s);
    vector<string> word;
    string tmp;
    for (size_t i = 0; i < s.size(); ++i) {
        if(s[i] != ' ' && s[i] != '.')
        {
            tmp += tolower(s[i]);
        }
        else {
            if(tmp.size() >= 1)
                word.push_back(tmp);
            tmp.clear();
        }
    }
    // 创建一个哈希表
    unordered_map<string, int> hash;
    for(auto e : word)
    {
        hash[e]++;
    }
    // 创建优先级队列
    priority_queue<pair<string, int>, vector<pair<string, int>>, Compare> q(hash.begin(), hash.end(), Compare());
    for(size_t i = 0; i < hash.size(); ++i)
    {
        cout << q.top().first << ":" << q.top().second << endl;
        q.pop();
    }
}
// 64 位输出请用 printf("%lld")


发表于 2024-11-26 19:48:39 回复(0)
from collections import Counter

word=input().split()
# 去句号
if word[-1][len(word[-1])-1] =='.':
    word[-1]=word[-1][0:len(word[-1])-1]

# 转小写
words = [i.lower() for i in word]
# 统计词频
dic = Counter(words)

dic=sorted(dic.items(),key=lambda x:(x[0],x[1]),reverse=True)
#for kv in dic:
#print('{}:{}'.format(kv[0],kv[1]))
# 反向遍历
for x in dic[::-1]:
    print("{}:{}".format(x[0],x[1]))


编辑于 2024-03-19 20:58:00 回复(0)
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;

struct Word{
    string str;
    int cnt;
};

bool cmp(Word lhs, Word rhs){
    return lhs.cnt>rhs.cnt;
}

int main(){
    string str, temp;
    map<string, int> mymap;
    while(str.back()!='.'){
        cin >> str;
        if(str.front()>='A'&& str.front()<='Z'){
            string rep;
            rep.push_back(str.front()-'A'+'a');
            str.replace(0,1, rep);
        }
        temp = str;
        if(temp.back()=='.') temp.pop_back();
        mymap[temp]++;
    }
    vector<Word> res;
    Word t;
    for(auto it=mymap.begin(); it!=mymap.end(); it++){
        t.str = it->first;
        t.cnt = it->second;
        res.push_back(t);
    }
    sort(res.begin(), res.end(), cmp);
    for(int i=0; i<res.size(); ++i){
        cout << res[i].str << ":" << res[i].cnt << endl;
    }
}

编辑于 2024-03-16 18:59:05 回复(0)
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;

//仿函数控制比较
struct comp
{
    bool operator()(const pair<string, int>& kv1, const pair<string, int>& kv2)
    {
        return kv1.second > kv2.second || (kv1.second == kv2.second && kv1.first < kv2.first);
    }

};

int main()
{
    //输入字符串
    string s;
    getline(cin, s);

    //转小写
    for (auto& ch : s)
    {
        if (ch >= 'A' && ch <= 'Z')
            ch += 32;
    }

    //将单词放进数组
    vector<string> v;
    for (int i = 0; i < s.size(); ++i)
    {
        string tmp;
        while(s[i] != ' ' && s[i] != '.')
        {
            tmp += s[i];
            ++i;
        }
        v.push_back(tmp);
    }
    //map统计次数
    map<string, int> m;
    for (auto ch : v)
    {
        m[ch]++;
    }

    //sort+仿函数排序
    vector<pair<string, int>> vv(m.begin(), m.end());
    sort(vv.begin(), vv.end(), comp());
    //输出
    for (int i = 0; i < vv.size(); ++i)
    {
        cout << vv[i].first << ":" << vv[i].second << endl;
    }
    return 0;
}

发表于 2023-06-04 20:16:51 回复(0)
有大佬知道啥情况吗
发表于 2023-03-08 09:54:30 回复(0)
//用map来排序很容易
#include <iostream>
#include <map>
using namespace std;

int main() {
    string s1;
    string s2;
    map<string,int> m1;
    getline(cin,s1);
    for(const auto& e : s1)
    {
        if(e == ' '|| e == '.')
        {
            m1[s2]++;
            s2.clear();
            continue;
        }
        s2.push_back(tolower(e));
    }
    for(const auto& e: m1)
    {
        cout<<e.first<<":"<<e.second<<endl;
    }

    return 0;
}

发表于 2023-02-27 15:10:14 回复(0)