【字符串】英文单词专题(统计、识别、反转、计数)

77. 翻转单词顺序

讲解视频:https://www.acwing.com/video/2728/
单词反转函数(字符串只能包含空格和字母)

class Solution {
public:
    string reverseWords(string s) {
        reverse(s.begin(), s.end());
        for (int i = 0; i < s.size(); i++)
        {
            int j = i;
            while (j < s.size() && s[j] != ' ') j++;
            reverse(s.begin() + i, s.begin() + j);
            i = j;
        }
        return s;
    }
};

B3640 T3 句子反转

#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;

void trans(string &s)
{
    for (char &x : s)
    {
        if (islower(x)) x = toupper(x);
        else if (isupper(x)) x = tolower(x);
    }
}

string reverse_words(string s)
{
    reverse(s.begin(), s.end());
    for (int i = 0; i < s.size(); i++)
    {
        int j = i;
        while (j < s.size() && s[j] != ' ') j++;
        if (isalpha(s[i])) reverse(s.begin() + i, s.begin() + j);
        i = j;
    }
    return s;
}

int main()
{
    string s;
    getline(cin, s);
    trans(s);
    cout << reverse_words(s);
    return 0;
}

3581. 单词识别

讲解视频:https://www.acwing.com/video/5473/
找单词函数写法一

#include <cstring>
#include <iostream>
#include <map>

using namespace std;

string s;
map<string, int> h;

void to_lower(string &s) // s记得传引用!!!不然改不了小写
{
    for (char &x : s) x = tolower(x);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    getline(cin, s);
    to_lower(s);
    for (int i = 0; i < s.size(); i++)
    {
        if (isalpha(s[i])) // 是字母
        {
            string word = "";
            int j = i;
            while (j < s.size() && isalpha(s[j])) word += s[j++];
            h[word]++;
            i = j;
        }
    }
    for (auto &[k, v] : h) cout << k << ":" << v << '\n';
    // for (auto &[k, v] : h) printf("%s:%d\n", k.c_str(), v);
    return 0;
}

找单词函数写法二

#include <cstring>
#include <iostream>
#include <map>

using namespace std;

string s;
map<string, int> h;

void to_lower(string &s) // s记得传引用!!!不然改不了小写
{
    for (char &x : s) x = tolower(x);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    getline(cin, s);
    to_lower(s);
    for (int i = 0; i < s.size(); i++)
    {
        if (isalpha(s[i])) // 是字母
        {
            string word = "";
            int j = i;
            while (j < s.size() && isalpha(s[j])) j++;
            word = s.substr(i, j - i);
            h[word]++;
            i = j;
        }
    }
    for (auto &[k, v] : h) cout << k << ":" << v << '\n';
    // for (auto &[k, v] : h) printf("%s:%d\n", k.c_str(), v);
    return 0;
}

3615. 单词个数统计

字符串转小写函数

void to_lower(string &s) // s记得传引用!!!不然改不了小写
{
    for (char &x : s) x = tolower(x);
}

单词统计函数写法一

    for (int i = 0; i < s.size(); i ++ ) 
    {
        if (isalpha(s[i]))
        {
            int j = i;
            string word;
            while (j < s.size() && isalpha(s[j])) j++;
            word = s.substr(i, j - i);
            wcnt++;
            //单词数++,别用map存单词,map会自动去重!除非题目说单词不重复
            // hash[word]++;
            i = j;
        }
    }

单词统计函数写法二

    for (int i = 0; i < s.size(); i ++ ) 
    {
        if (isalpha(s[i]))
        {
            int j = i;
            string word;
            while (j < s.size() && isalpha(s[j])) word += s[j++];
            wcnt++;
            //单词数++,别用map存单词,map会自动去重!除非题目说单词不重复
            // hash[word]++;
            i = j;
        }
    }

本题代码

#include <cstring>
#include <iostream>
#include <map>

using namespace std;

string s;
map<char, int> hc;
int ccnt, wcnt, maxcnt, cmax;

void to_lower(string &s) // s记得传引用!!!不然改不了小写
{
    for (char &x : s) x = tolower(x);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    getline(cin, s);
    to_lower(s);

    for (char &x : s)
        if (isalpha(x))
        {
            ccnt++; // 字母个数
            hc[x]++; // 记录每个字母出现次数
            if (hc[x] > cmax) cmax = hc[x]; // 出现最多字母的次数
        }

    for (int i = 0; i < s.size(); i++) // 找单词
    {
        if (isalpha(s[i]))
        {
            int j = i;
            string word;
            while (j < s.size() && s[j] != ' ') j++;
            word = s.substr(i, j - i);
            wcnt++;
            // 单词数++,别用map存单词,map会自动去重!除非题目说单词不重复
            i = j;
        }
    }

    cout << ccnt << '\n' << wcnt << '\n';
    for (auto &[k, v] : hc)
    {
        if (v == cmax) cout << k << ' ';
    }
    cout << '\n';
    cout << cmax << '\n';
    return 0;
}

P1308 [NOIP 2011 普及组] 统计单词数

法一:双指针

#include <iostream>

using namespace std;

const int N = 1e6 + 10;

string s, p;

void to_lower(string &s)
{
    for (char &x: s) x = tolower(x);
}

int main()
{
    getline(cin, p);
    getline(cin, s);
    to_lower(p), to_lower(s);

    int cnt = 0, k = -1;
    for (int i = 0; i < s.size(); i++)
    {
        if (s[i] == ' ') continue;
        int j = i + 1;
        while (j < s.size() && s[j] != ' ') j++;
        // printf("i:%d, j:%d, s[%d]=%c\n", i, j, j, s[j]);
        if (p == s.substr(i, j - i))
        {
            cnt++;
            if (k == -1) k = i;
        }
        i = j;
    }
    if (cnt == 0) printf("-1");
    else printf("%d %d", cnt, k);
    return 0;
}

法二:KMP特判单词前后空格

#include <iostream>
#include <cstring>

using namespace std;

const int N = 1e6 + 10;

int ne[N];
string s, p;
int cnt, ans[N];
bool flag = true;

void to_lower(string &s) //转小写
{
    for (char &x: s) x = tolower(x);
}

void strStr(string s, string p) //KMP,s为长串,p为短串
{
    int n = s.size(), m = p.size();
    s = ' ' + s, p = ' ' + p;
    
    for (int i = 2, j = 0; i <= m; i++)
    {
        while (j && p[i] != p[j + 1]) j = ne[j];
        if (p[i] == p[j + 1]) j++;
        ne[i] = j;
    }
    
    for (int i = 1, j = 0; i <= n; i++)
    {
        while (j && s[i] != p[j + 1]) j = ne[j];
        if (s[i] == p[j + 1]) j++;
        if (j == m) 
        {
            if (s[i + 1] != ' ') continue;//单词后空格
            if (s[i - m] != ' ') continue;//单词前空格
            ans[cnt++] = i - m;//记录单词出现位置
            flag = false;
        }
    }
}

int main()
{
    getline(cin, p);
    getline(cin, s);
    to_lower(p), to_lower(s);
    strStr(s, p);
    
    if (flag) printf("-1");
    else printf("%d %d\n", cnt, ans[0]);
    return 0;
}
posted @   Tshaxz  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
Language: HTML
点击右上角即可分享
微信分享提示