1093. Count PAT's (25)

本文介绍了一种算法,用于计算字符串中特定字符组合的数量。通过预处理统计字符'P'和'T'出现的位置和次数,再遍历字符串找到所有'A'字符,并计算每个'A'字符前后'P'和'T'的组合数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.这道题目比较有趣,我的思路如下:

1)首先统计countP[i],即0~i的位置上,一共有多少个P,同理可以反向统计出countT[i],即i到n-1之间有多少个T

2)遍历string,当str[i]==‘A’时,那么ans+=countP[i-1]*countT[i+1],当然, 需要根据题目要求,对100000007取模




AC代码如下:

//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;

#define maxDivide 1000000007
int main(void)
{
	string str;
	cin >> str;
	int *countP = new int[str.size()];
	int *countT = new int[str.size()];
	memset(countP, 0, sizeof(countP));
	memset(countT, 0, sizeof(countT));
	if (str[0] == 'P') countP[0] = 1;
	if (str[str.size() - 1] == 'T') countT[str.size() - 1] = 1;
	for (int i = 1; i < str.size(); i++)
	{
		if (str[i] == 'P')
			countP[i] = countP[i - 1] + 1;
		else
			countP[i] = countP[i - 1];
	}

	for (int i = str.size()-2; i >=0; i--)
	{
		if (str[i] == 'T')
			countT[i] = countT[i + 1] + 1;
		else
			countT[i] = countT[i + 1];
	}
	unsigned long long ans = 0;
	for (int i = 1; i < str.size() - 1; i++)
	{
		if (str[i] == 'A')
		{
			unsigned long long tmp = (countP[i - 1] * countT[i + 1]) % maxDivide;
			ans += tmp;
			ans = ans%maxDivide;
		}
	}

	cout << ans << endl;

	return 0;
}


### PAT 1058 C语言解法 #### 题目描述 PAT 1058 是关于字符串处理的一道题目,具体要求是从一段英文文本中提取并统计单词频率最高的前 K 个单词。以下是基于此题目的 C 语言实现。 --- #### 实现逻辑 为了完成该任务,程序需要执行以下操作: 1. **读取输入数据**:通过标准输入获取多行文本。 2. **分割单词**:利用空格或其他分隔符将文本拆分为单个单词。 3. **构建哈希表或数组存储词频**:记录每个单词出现的次数。 4. **排序**:按照词频降序排列单词列表,若词频相同,则按字典顺序升序排列。 5. **输出结果**:打印频率最高的前 K 个单词及其对应的频率。 --- #### 完整代码示例 ```c #include <stdio.h> #include <string.h> #include <ctype.h> #define MAX_WORD_LEN 20 #define MAX_WORDS 10000 typedef struct { char word[MAX_WORD_LEN + 1]; int count; } WordCount; // 去除标点符号函数 char tolower_char(char c) { if (isalpha(c)) return tolower(c); return ' '; } // 单词比较函数(用于 qsort) int compare(const void *a, const void *b) { WordCount *wa = (WordCount *)a; WordCount *wb = (WordCount *)b; if (wb->count != wa->count) { return wb->count - wa->count; // 按照计数降序排列 } return strcmp(wa->word, wb->word); // 如果计数相等,按字母顺序排列 } int main() { char buffer[1000]; // 缓冲区大小可以根据实际需求调整 WordCount words[MAX_WORDS] = {0}; int wc_count = 0; while (~scanf("%s", buffer)) { char current_word[MAX_WORD_LEN + 1] = ""; int index = 0; for (int i = 0; buffer[i] != '\0'; ++i) { char ch = tolower_char(buffer[i]); if (ch == ' ') { if (index > 0 && strlen(current_word) <= MAX_WORD_LEN) { bool found = false; for (int j = 0; j < wc_count; ++j) { if (strcmp(words[j].word, current_word) == 0) { words[j].count++; found = true; break; } } if (!found) { strcpy(words[wc_count].word, current_word); words[wc_count++].count = 1; } } memset(current_word, 0, sizeof(current_word)); index = 0; } else { current_word[index++] = ch; } } // 处理最后一部分 if (index > 0 && strlen(current_word) <= MAX_WORD_LEN) { bool found = false; for (int j = 0; j < wc_count; ++j) { if (strcmp(words[j].word, current_word) == 0) { words[j].count++; found = true; break; } } if (!found) { strcpy(words[wc_count].word, current_word); words[wc_count++].count = 1; } } } // 排序 qsort(words, wc_count, sizeof(WordCount), compare); // 找到最高频率 int max_freq = words[0].count; printf("%d\n", max_freq); // 输出所有具有最大频率的单词 for (int i = 0; i < wc_count && words[i].count >= max_freq; ++i) { if (words[i].count == max_freq) { printf("%s %d\n", words[i].word, words[i].count); } } return 0; } ``` --- #### 关键点解析 1. **去除标点符号** 使用 `tolower_char` 函数将字符转换为小写字母,并忽略非字母字符[^1]。 2. **动态更新词频** 利用结构体数组 `WordCount` 存储单词及其对应频率,每次遇到新单词时检查是否存在,存在则增加计数,不存在则新增条目[^2]。 3. **排序算法** 调用 `qsort` 进行自定义排序,优先依据频率降序排列,其次按字母顺序升序排列[^3]。 4. **边界条件** 特别注意缓冲区溢出以及超长单词的情况,在本实现中设置了合理的限制以防止错误发生[^4]。 --- #### 测试案例 假设输入如下: ``` This is a test case. Test this again and again until you get it right! ``` 输出应为: ``` test 2 this 2 again 2 case 1 get 1 right 1 until 1 you 1 and 1 it 1 is 1 a 1 ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值