1093 Count PAT's (25 分)(逻辑题,水题)

本文介绍了一种算法,用于计算给定字符串中特定子串“PAT”的出现次数。通过遍历字符串并利用计数变量,算法能高效地找到所有符合条件的子串组合。输入为包含P、A、T字符的字符串,输出为子串PAT的总数,结果模1000000007。

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

The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th characters.

Now given any string, you are supposed to tell the number of PAT's contained in the string.

Input Specification:

Each input file contains one test case. For each case, there is only one line giving a string of no more than 10

5

characters containing only P, A, or T.

Output Specification:

For each test case, print in one line the number of PAT's contained in the string. Since the result may be a huge number, you only have to output the result moded by 1000000007.

Sample Input:

APPAPT

Sample Output:

2

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <cctype>
#include <string.h>
#include <cstdio>
#include <unordered_set>
using namespace std;
int main() {
    string s;
    cin>>s;
    int len=s.length(),cntt=0,cntp=0,sum=0;
    for(int i=0;i<len;i++){
        if(s[i]=='T')
            cntt++;
    }
    for(int i=0;i<len;i++){
        if(s[i]=='P') cntp++;
        if(s[i]=='T') cntt--;
        if(s[i]=='A') sum=(sum+cntt*cntp)%1000000007;
    }
    cout<<sum;
    return 0;
}

 

<think>嗯,用户想要找C语言编程相关的练习和答案,特别是提到“C语言编程库100答案”。首先,我需要确定用户的具体需求是什么。他们可能正在准备考试,比如计算机二级,或者在学习C语言的过程中需要练习来巩固知识。用户可能希望得到目和对应的解答,以便自己练习和核对。 接下来,我需要检查提供的引用内容,看看是否有相关的资源。引用[1]、[2]、[3]、[4]都提到了C语言的编程,例如按位或操作、组合数字、素数判断等。特别是引用[3]提到了计算机二级C语言编程库100,这可能正是用户需要的。引用[4]中的素数判断代码示例也是一个常见的练习,适合作为例子。 用户可能希望得到这些库的直接链接或具体目列表,但根据引用内容,这些可能是站内资源或文档,需要引导用户如何找到它们。同时,用户可能需要知道这些目的答案,所以需要确保提供的代码示例正确且符合C语言标准。 需要注意的是,用户可能没有访问这些引用文档的权限,所以提供一些典型例和解答会更实用。例如,引用[4]中的素数判断代码是一个很好的例子,可以详细解释其逻辑。此外,引用[3]中的函数也可以作为一个例子,说明如何生成紧邻m的k个素数。 另外,用户可能需要了解如何系统地练习,比如章节练习或按难度级,但根据现有引用,可能需要建议他们参考这些库的结构。同时,需要提醒用户注意代码的正确性和最佳实践,比如在引用[4]的代码中使用scanf_s而不是scanf,这可能涉及编译器差异。 最后,生成相关问时要考虑用户可能进一步的需求,比如如何调试程序、优化代码或应用特定算法,这有助于他们深入学习。</think>根据您提供的引用内容,我将整理部C语言编程练习及参考答案,并提供学习建议: ### 一、典型编程及解答 1. **按位或运算**(引用[1]) ```c #include <stdio.h> int main() { int a = 110; int b = 100; int c = a | b; // 二进制按位或运算 printf("a | b = %d \n", c); return 0; } ``` 运算原理:$110_{10}=01101110_2$,$100_{10}=01100100_2$,或运算后得$01101110_2=110_{10}$[^1] 2. **三位数组合问**(引用[2]) 生成1-5不重复的三位数组合: ```c #include <stdio.h> void main() { int a,b,c,n=0,num[1000]; for(a=1;a<=5;a++) for(b=1;b<=5;b++) for(c=1;c<=5;c++) if(a!=b && a!=c && b!=c) num[n++]=a*100+b*10+c; printf("Total combinations: %d",n); } ``` 3. **素数查找函数**(引用[3][4]) ```c void fun(int m, int k, int xx[]) { int count=0, num=m+1; while(count < k) { int flag=0; for(int i=2; i<=num/2; i++){ if(num%i ==0) { flag=1; break; } } if(!flag) xx[count++]=num; num++; } } ``` ### 二、学习建议 1. **练习路径**: - 基础语法:变量、运算符、流程控制 - 进阶应用:指针、结构体、文件操作 - 算法实现:排序、查找、递归 2. **调试技巧**: - 使用`printf()`进行变量追踪 - 模块测试函数功能 - 利用gdb调试工具 ### 三、资源推荐 1. **在线评测平台**: - 浙江大学PAT平台(https://pat.zju.edu.cn) - LeetCode(https://leetcode.cn) 2. **教材参考**: - 《C Primer Plus》第6版 - 《C和指针》
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小火汁猛猛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值