首页 > 试题广场 >

单词识别

[编程题]单词识别
  • 热度指数:15319 时间限制: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)
把句子中的单词(不区分大小写)按出现次数按从多到少把单词和次数在屏幕上输出来,
这句话跟样例不符,样例是根据按字典序输出
发表于 2019-02-27 09:16:52 回复(6)
#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)
本题输入只有一行,输出按字典序排序
C++可以用strtok()将输入划分成单词,然后用map<string, int>统计词频
#include <bits/stdc++.h>
using namespace std;

char st[1000], *r;
map<string, int> c; // 如果用map<char*, int>,输出顺序是单词在原句中出现顺序而不是字典序

int main () {
    gets(st);
    r = strtok(st, " ,.\n");
    do {
        *r = tolower(*r);
        ++c[string(r)];
    } while (r = strtok(NULL, " ,.\n"));
    for (auto it = c.begin(); it != c.end(); ++it)
        cout << it -> first << ":" << it -> second << endl;
}
python自带Counter可以直接返回一个包含词频的dict(好长
from collections import Counter
print('\n'.join('%s:%d' % i for i in sorted(Counter(input().lower().replace(',', ' ').replace('.', ' ').split()).items())))


编辑于 2020-02-15 00:33:29 回复(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)
有人能告知哪里出错了吗?
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)
测试输出不对 可以参考我的代码和题干一样但是通过不了
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)
#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)
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)
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include <vector>
using namespace std;

struct Compare
{
    bool operator()(const pair<int, string>& l, const pair<int, string>& r)
    {
        return l.first > r.first || (l.first == r.first && l.second < r.second);
    }
};

int main() {
    string input;
    map<string, int> words;
    while (cin >> input)
    {
        //转小写
        transform(input.begin(), input.end(), input.begin(), ::tolower);

        //去除小写字母以外的输入
        string s;
        for (const auto& e : input)
        {
            if (e >= 'a' && e <= 'z')
                s.push_back(e);
        }

        words[s]++;
    }

    vector<pair<int, string>> v;
    for (const auto& e : words)
    {
        v.push_back(make_pair(e.second, e.first));
    }

    sort(v.begin(), v.end(), Compare());

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

发表于 2023-02-18 20:29:35 回复(0)
#include <iostream>
#include <cstring>
using namespace std;

int main() {
    char ch[1000][100];
    int n;                                          //单词个数
    for (int i = 0; i < 1000; i++) {
        cin >> ch[i];
        for (int j = 0; j < strlen(ch[i]); j++) {   //大写字母转小写
            ch[i][j] = tolower(ch[i][j]);
        }
        if (ch[i][strlen(ch[i]) - 1] == '.') {      //字符串以“.”结尾
            ch[i][strlen(ch[i]) - 1] = '\0';        //将末尾的“.”截断
            n = i + 1;
            break;
        }
    }
    //冒泡排序法将字符串排序
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (strcmp(ch[i], ch[j]) < 0) {
                char temp[100];
                strcpy(temp, ch[i]);
                strcpy(ch[i], ch[j]);
                strcpy(ch[j], temp);
            }
        }
    }
    for (int i = 0, count = 1; i < n; i++) {
        if (!strcmp(ch[i], ch[i + 1])) {
            count++;
        } else {
            cout << ch[i] << ":" << count << endl;
            count = 1;
        }
    }
}

发表于 2023-01-17 20:49:29 回复(0)
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
	string str;
	map<string, int> mp;
	while (getline(cin, str))
	{
		int slow = 0;   //单词的起始下标
		string s;       //存放单个单词
		for (int i = 0; i < str.size(); i++)
		{
			//大写变小写
			if ('A' <= str[i] && str[i] <= 'Z')
			{
				str[i] += 32;
			}
			//取单词放入mp中,插入+计数+自动降序排序
			if (str[i] == ' ' || str[i] == '.')
			{
				int num = i - slow;     //单词大小
				s = str.substr(slow, num);  //通过单词起始下标和大小取出来
				mp[s]++;
				slow = i + 1;  //跳过空格到下一个单词的起始位置
			}
		}

		//上述已经按字典序排好了---下面将次数大的放在前面
		//注意次数出现相等,防止去重,要用multimap
		//手动更改为降序
		multimap<int, string, greater<int>> intsort;
		for (const auto& e : mp)
		{
			//插入+排序
			intsort.insert(make_pair(e.second, e.first));
		}

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

	}
	return 0;
}

发表于 2022-08-03 19:12:41 回复(0)
#include<string>
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
typedef map<string,int>::iterator mapIterator;

struct cmpIterator
{
    bool operator()(const mapIterator& it1, const mapIterator& it2)
    {
        return it1->second > it2->second;
    }
};
int main()
{
    string str;
    getline(cin,str);
    //应该先把对应大写转成小写;
    for(int i = 0; i < str.size();i++)
    {
//         if(isupper(str[i])){
//             tolower(str[i]);
//         }
        if (str[i] >= 65 && str[i] <= 90)
		{
			str[i] += 32;
		}
    }
    vector<string> vWord;
    size_t startPos = 0;
    size_t endPos = 0;
    while(endPos != str.npos)
    {
        endPos = str.find(' ',startPos);
        vWord.push_back(str.substr(startPos,endPos - startPos));
        startPos = endPos + 1;
    }
    
    for(auto& word:vWord)
    {
        for(int i = 0; i < word.size();i++)
        {
            if(word[i] == '.')
            {
                word.erase(word.size() - 1); 
            }
        }
    }
    //处理完毕,下面统计次数
    map<string,int> countMap;
    for(auto word : vWord)
    {
        countMap[word]++;
    }
    
    //map<string,int>::iterator it = countMap.begin();
    mapIterator it = countMap.begin();
    vector<mapIterator> vPtr;
    while(it != countMap.end())
    {
        vPtr.push_back(it);
        it++;
    }
    
    stable_sort(vPtr.begin(),vPtr.end(),cmpIterator());
    
    for(auto iterator:vPtr)
    {
        cout<<iterator->first<<":"<<iterator->second<<endl;
    }
    
    return 0;
}

分享一个超越%0用户提交的代码,果然优秀的人走哪都是优秀的<狗头>
发表于 2022-08-03 10:53:21 回复(1)

问题信息

上传者:小小
难度:
66条回答 7227浏览

热门推荐

通过挑战的用户

查看代码