PAT1093: Count PAT's
1093. Count PAT's (25)
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 思路 试了下直接暴搜,果然最后几个测试点超时,看来还是得用dp。
这道题DP的思路就是用两个int数组P[n],T[n]保存第i个字符的左边字母'P'的个数P[i]和右边字母'T'的个数T[i],当遍历到的字符是'A'时,那么以这个A为基础的"PAT"字符串个数就是它左侧P字母个数乘以右侧T字母个数。即P[i]*T[i]
代码
#include<iostream>
#include<vector>
using namespace std;
int main()
{
string s;
while(cin >> s)
{
vector<int> P(,);
vector<int> T(,);
for(int i = ,j = s.size() - ;i < s.size() && j >= ;i++,j--)
{
if(s[i - ] == 'P')
{
P[i]++;
}
if(s[j + ] == 'T')
{
T[j]++;
}
P[i] += P[i - ];
T[j] += T[j + ];
} int sum = ;
for(int i = ;i < s.size();i++)
{
if(s[i] == 'A')
{
sum += P[i] * T[i];
sum %= ;
} }
cout << sum << endl;
}
}
PAT1093: Count PAT's的更多相关文章
- pat1093. Count PAT's (25)
1093. Count PAT's (25) 时间限制 120 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Peng The strin ...
- PAT 1093 Count PAT's[比较]
1093 Count PAT's (25 分) The string APPAPT contains two PAT's as substrings. The first one is formed ...
- PAT甲级——1093 Count PAT's (逻辑类型的题目)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/93389073 1093 Count PAT's (25 分) ...
- PAT_A1093#Count PAT's
Source: PAT A1093 Count PAT's (25 分) Description: The string APPAPT contains two PAT's as substrings ...
- 1093. Count PAT's (25)
The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and ...
- 1093. Count PAT's
The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and ...
- 1093. Count PAT’s (25)-统计字符串中PAT出现的个数
如题,统计PAT出现的个数,注意PAT不一定要相邻,看题目给的例子就知道了. num1代表目前为止P出现的个数,num12代表目前为止PA出现的个数,num123代表目前为止PAT出现的个数. 遇到P ...
- 1093 Count PAT's(25 分)
The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and ...
- PAT (Advanced Level) Practise - 1093. Count PAT's (25)
http://www.patest.cn/contests/pat-a-practise/1093 The string APPAPT contains two PAT's as substrings ...
随机推荐
- android ndk编译项目(android-ndk-16r1)
由于采用android-ndk-16r1版本的ndk来编译 编译的环境之类在这里省略,注意是最后编译的命令如下 Administrator@WIN-AF6P80LVIJ0 ~ $ cd $ANDROI ...
- 理解WebKit和Chromium: Chromium插件和扩展基础
转载请注明原文地址:http://blog.csdn.net/milado_nju ##概述 插件和扩展是一种扩充浏览器功能的技术,在之前我们介绍过NPAPI插件技术,在Chromium中,远远不只是 ...
- 断言(Assertion)需要注意的一个地方
因为断言只在debug构建中有效,所以它是中关重要的去避免运行任何代码或调用任何方法在断言条件中,而这些代码或方法会影响程序的状态. 否则程序的行为将在debug和release构建中变得不一致,这显 ...
- dex分包方案
当一个app的功能越来越复杂,代码量越来越多,也许有一天便会突然遇到下列现象: 1. 生成的apk在2.3以前的机器无法安装,提示INSTALL_FAILED_DEXOPT 2. 方法数量过多,编译时 ...
- C语言实现字符串中(10进制和16进制)转成十进制数
如何将字符串中的10进制数和16进制数提取出来,看以下代码: #include <stdio.h> typedef char TUINT8 ; typedef int TUINT32; T ...
- redis持久化AOF与RDB
RDB 持久化可以在指定的时间间隔内生成数据集的时间点快照(point-in-time snapshot). AOF 持久化记录服务器执行的所有写操作命令,并在服务器启动时,通过重新执行这些命令来还原 ...
- SharePoint2007 开发部署Application Pages
介绍:SharePoint应用程序页,也就是_layouts路径下的aspx页面,服务器C:\Program Files\Common Files\Microsoft Shared\web serve ...
- mac os x下的程序快速切换快捷键
按下command + tab键正向切换程序 按下command + shift + tab键反向切换程序 按下command + tab键选择程序后,仍然按下command键不放,然后按下optio ...
- rails中migration数据库后测试不通过的问题
rails项目中由于后期需求变化,需要在products数据库中增加一个字段来满足多国家商品的分类: rails g migration add_locale_to_products locale:s ...
- 如何通过jQuery获取一个没有定高度的元素---------的自适应高度(offsetHeight的正确使用方法)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...