【PAT】1093. Count PAT's (25)

本文介绍了一种算法,用于计算字符串中特定子串“PAT”的出现次数。通过预先计算每个字符左侧的P和右侧的T的数量,高效地找出所有可能的组合。

摘要生成于 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 105 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

分析:以字母A为核心,计算在这个A左边的字母P的个数以及在这个A的右边的字母T的个数,两者相乘即以这个A为核心可以组建的PAT组合的个数。对每个A进行以上操作,结果相加即可。

#include <iostream>
#include <vector>
#include <string>
using namespace std;

struct node{
	node(int p, int t){
		numP = p;
		numT = t;
	}
	int numP;//在这个字母A左边的P的个数 
	int numT;//在这个字母A右边的T的个数 
};

int main(int argc, char** argv) {
	string str;
	cin>>str;
	int i;
	vector<node> vecA;
	int cntP=0, cntA=0, cntT=0;
	for(i=0; i<str.size(); i++){
	 	if(str[i]== 'P'){
	 		cntP++;	
		}else if(str[i]=='A'){
			vecA.push_back( node(cntP,0));
		}
	}	
	int index = vecA.size()-1;
	for(i=str.size()-1; i>=0; i--){
		if(str[i]=='T'){
			cntT++;
		}else if(str[i]=='A'){
			vecA[index--].numT = cntT;
		}		
	}
	long long  total = 0, t=1000000007;
	
	for(i=0; i<vecA.size(); i++){
		total = (total+vecA[i].numP*vecA[i].numT)%t;
	}
  	printf("%lld\n",total);
	return 0;
}


同样的思路,另一种写法:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main(int argc, char** argv) {
	string str;
	cin>>str;
	int len = str.size();
	vector<int> cntP(len);
	vector<int> cntT(len);
	int i;
	if(str[0]=='P'){
		cntP[0] = 1;
	}else{
		cntP[0] = 0;
	}
	int tmp;
	for(i=1; i<len; i++){
		if(str[i] == 'P'){
			tmp = 1;
		}else{
			tmp = 0;
		}
		cntP[i] = cntP[i-1] + tmp;
	}
	if(str[len-1]=='T'){
		cntT[len-1] = 1;
	}else{
		cntT[len-1] = 0;
	}
	for(i=len-2; i>=0; i--){
		if(str[i]=='T'){
			tmp=1;
		}else{
			tmp=0;
		}
		cntT[i] = cntT[i+1] + tmp;
	}
	long total = 0;
	for(i=1; i<str.size()-1; i++){
		if(str[i] == 'A'){
			total += cntP[i-1]*cntT[i+1];
			total %= 1000000007;
		}
	}
	printf("%ld\n",total);
	return 0;
}



### 如何使用 PyInstaller 打包 Python 脚本 #### 安装 PyInstaller 在使用 PyInstaller 之前,需要先安装该工具。可以通过 `pip` 工具来完成安装操作。如果是在 Anaconda 环境下工作,则建议创建一个新的虚拟环境并激活后再进行安装[^2]。 ```bash pip install pyinstaller -i https://mirrors.aliyun.com/pypi/simple ``` 对于特定的 Python 版本需求或者依赖项较多的情况,可以按照如下方式进行处理: ```bash conda create -n myenv python=3.11.7 conda activate myenv pip install numpy pandas openpyxl Jinja2 pyinstaller -i https://mirrors.aliyun.com/pypi/simple ``` 以上命令会确保所有必要的依赖被正确安装到环境中[^3]。 --- #### 基础打包流程 一旦完成了 PyInstaller 的安装,就可以开始打包过程。以下是基本的操作步骤: ##### 单文件模式 (`--onefile`) 将所有的资源和依赖压缩至单一可执行文件中,便于分发。这种方式特别适合于小型应用程序或简单的脚本转换。 ```bash pyinstaller --onefile your_script.py ``` 这会在当前目录下的 `dist` 文件夹生成最终的 `.exe` 可执行文件(Windows 平台),或其他平台对应的二进制文件[^4]。 ##### 隐藏控制台窗口 (`--noconsole`) 如果你的应用是一个图形界面程序 (GUI),可能不希望启动时弹出黑框式的终端窗口。此时可以加入 `--noconsole` 参数。 ```bash pyinstaller --onefile --noconsole gui_application.py ``` 注意,在这种情况下,标准输出流会被禁用,因此调试信息无法通过打印语句查看[^1]。 ##### 自定义图标 (`--icon`) 为了让生成的应用更加美观,还可以指定一个 `.ico` 图标作为应用标志。 ```bash pyinstaller --onefile --icon=path/to/icon.ico your_script.py ``` 这里的路径应指向实际存在的 ICO 文件位置。 ##### 添加外部数据文件 (`--add-data`) 某些时候,Python 脚本可能会读取一些额外的数据文件比如配置文档、图片素材等。这些也需要被打包进去才能正常运作。 Linux/MacOS 用户应当这样写入相对路径: ```bash pyinstaller --onefile --add-data "data_file.txt:." your_script.py ``` 而 Windows 则采用分号代替冒号分割源目两部分: ```bash pyinstaller --onefile --add-data "data_file.txt;." your_script.py ``` --- #### 进阶选项 有时项目较为复杂,存在隐含导入模块等问题,这时就需要借助更高级别的参数解决这些问题。例如当发现缺少某个库的时候可以用 `--hidden-import` 显式声明出来。 ```bash pyinstaller --onefile --hidden-import=some_module your_script.py ``` 另外还有清除旧版缓存的功能也很实用,尤其是在修改过代码之后再次尝试编译新版本前推荐加上这个开关以防残留干扰。 ```bash pyinstaller --clean --onefile your_script.py ``` --- #### 示例代码片段展示 假设我们有一个名为 `hello_world.py` 的简单脚本: ```python print("Hello, world!") ``` 那么完整的打包命令将是这样的: ```bash pyinstaller --onefile hello_world.py ``` 完成后可以在 `dist/hello_world.exe` 中找到结果文件。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值