统计单词个数

题目传送门

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <sstream>
#include <iostream>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
using namespace std;
#define ll long long
#define re register
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define P pair<int,int>
void read(int &a)
{
    a=0;
    int d=1;
    char ch;
    while(ch=getchar(),ch>'9'||ch<'0')
        if(ch=='-')
            d=-1;
    a=ch-'0';
    while(ch=getchar(),ch>='0'&&ch<='9')
        a=a*10+ch-'0';
    a*=d;
}
void write(int x)
{
    if(x<0)
        putchar(45),x=-x;
    if(x>9)
        write(x/10);
    putchar(x%10+'0');
}
int f[205][25],w[205][205],T;///f[i][j]表示前i字符里面分j段最多的单词数,w[i][j]表示在i——j里面的单词数
char A[205],s[25],word[10][205];
bool judge(int x,int y)
{
    for(re int i=1;i<=T;i++)
    {
        char *p=strstr(&A[x],word[i]);///搜单词表里有没有A[x]开头的字母
        if(p!=NULL&&p-&A[x]==0&&x+strlen(word[i])-1<=y)///p-&A[x]表示可能有A[x]开头的单词,但是这个字母不在&A[x]这个位置
            return 1;
    }
    return 0;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int p,k;
    read(p);
    read(k);
    int len=20*p;
    for(re int i=0;i<p;i++)
    {
        scanf("%s",s);
        strcat(&A[1],s);
    }
    read(T);
    for(re int i=1;i<=T;i++)
        scanf("%s",word[i]);
    for(re int i=len;i>=1;i--)
        for(re int j=i;j>=1;j--)
            if(judge(j,i))
                w[j][i]=w[j+1][i]+1;
            else
                w[j][i]=w[j+1][i];
    for(re int i=1;i<=len;i++)///枚举前i段
        for(re int j=1;j<=k&&j<=i;j++)///枚举分段的个数,最多为k或者i
            for(re int l=j-1;l<i;l++)///前面已分段数-1就是当前开始位置
                f[i][j]=max(f[i][j],f[l][j-1]+w[l+1][i]);
    write(f[len][k]);
    return 0;
}

 

转载于:https://www.cnblogs.com/acm1ruoji/p/10683067.html

### 实现Python统计单词个数的功能 在Python中,可以通过多种方式实现统计单词个数的功能。以下是一个完整的解决方案,涵盖了字符串处理、分割以及字典的应用。 #### 方法一:使用基础字符串操作和字典 可以利用字符串的 `split()` 方法将句子分割为单词列表,并通过字典统计每个单词出现的次[^3]。 ```python def count_words(text): # 将文本转换为小写并移除标点符号 for char in ",.;:?!" : text = text.replace(char, " ") # 分割字符串为单词列表 words = text.lower().split() word_dict = {} for word in words: if word in word_dict: word_dict[word] += 1 else: word_dict[word] = 1 return word_dict # 示例 text = "Hello world! Hello Python. Python is great, and the world agrees." result = count_words(text) print("单词统计结果:", result) ``` #### 方法二:使用正则表达式 对于更复杂的字符串处理,可以使用 `re` 模块中的正则表达式来分割单词[^4]。 ```python import re def count_words_regex(text): # 使用正则表达式匹配单词 words = re.findall(r'\b\w+\b', text.lower()) word_dict = {} for word in words: if word in word_dict: word_dict[word] += 1 else: word_dict[word] = 1 return word_dict # 示例 text = "Hello world! Hello Python. Python is great, and the world agrees." result = count_words_regex(text) print("单词统计结果:", result) ``` #### 方法三:使用 `collections.Counter` Python 的 `collections` 模块提供了 `Counter` 类,能够简化单词统计过程[^2]。 ```python from collections import Counter import re def count_words_counter(text): # 使用正则表达式提取单词并转换为小写 words = re.findall(r'\b\w+\b', text.lower()) # 使用 Counter 统计单词频率 word_count = Counter(words) return dict(word_count) # 示例 text = "Hello world! Hello Python. Python is great, and the world agrees." result = count_words_counter(text) print("单词统计结果:", result) ``` 以上三种方法均能实现单词统计功能,但推荐使用 `Counter`,因为它代码简洁且效率较高。 #### 注意事项 - 在处理文本时,需要先清理标点符号以确保统计准确性[^5]。 - 如果输入包含大小写混合的单词,则需统一转换为小写以避免重复统计。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值