本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/93389073

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 1 characters containing only PA, 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

题目大意:在一串字符中寻找能够拼凑出PAT的组合数量,字符配对要遵循原始字符串的先后顺序。

思路:开三个数组PPos、APos、TPos分别记录'P'、'A'、'T'三个字符所在的位置。遍历APos,对于当前位置的'A',在它之前的'P'的个数和在它之后的'T'的个数之积就是当前的'A'的组合数量,所有的'A'的组合数量之和就是答案。

 #include <iostream>
#include <string>
#include <vector>
using namespace std; vector<int> PPos, APos, TPos;
string s; int main()
{
int num = ;
cin >> s;
for (int i = ; i < s.length(); i++) {
if (s[i] == 'P')
PPos.push_back(i);
if (s[i] == 'A')
APos.push_back(i);
if (s[i] == 'T')
TPos.push_back(i);
}
int PSize = PPos.size(),
ASize = APos.size(),
TSize = TPos.size(),
PIndex = ,
TIndex = ;
for (int i = ; i < ASize; i++) {
while (PIndex < PSize && PPos[PIndex] < APos[i]) {
PIndex++;
}
while (TIndex < TSize && TPos[TIndex] < APos[i]) {
TIndex++;
}
if (TIndex >= TSize)
break;
num += PIndex * (TSize - TIndex);
num = num % ;
}
cout << num << endl;
return ;
}

最新文章

  1. 设计模式--外观模式Facade(结构型)
  2. Android 常用代码
  3. hadoop 2.6配置记录
  4. nginx 配置多个二级域名
  5. Lecture Notes: Macros
  6. Java知识点:instanceof关键字
  7. docker 指定容器名字
  8. 查看htmlView
  9. Struts2基础学习(二)&mdash;Action
  10. 使用Identity Server 4建立Authorization Server (1)
  11. android获取string.xml的值
  12. 持续代码质量管理-SonarQube Scanner部署
  13. MFC编程之数值调节按钮
  14. Mysql占用CPU过高如何优化?(转)
  15. pandas isin
  16. js中级小知识3
  17. PICE(2):JDBCStreaming - gRPC-JDBC Service
  18. vue再次入手(数据传递①)
  19. 关于 Flutter的Button按钮没有高度设置
  20. git rm -r --cache命令 及 git .gitignore 文件

热门文章

  1. Maven 将jar导入本地maven仓库
  2. 扩展欧几里得算法(exgcd)
  3. CSS:word-wrap/overflow/transition
  4. HDU1026(延时迷宫:BFS+优先队列)
  5. orcal数据库得连接必须用localhost,url中不要用127.0.0.1,不然无法连接
  6. java web基础知识
  7. linux 时间处理 + 简单写log
  8. VIsual Studio 2010 常用快捷键
  9. JavaScript中的BOM知识框架
  10. Java之网络编程UDP和TCP