浙大pat | 牛客网甲级1063 Count PAT's (25)字符串

题目描述

The string APPAPT contains two PAT's as substrings. The firstone 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 inthe string.



输入描述:

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

characters containing only P, A, or T.


输出描述:

For each test case, print in one line the number of PAT'scontained in the string. Since the result may be a huge number, you only haveto

output the result moded by 1000000007.



输入例子:

APPAPT



输出例子:

2

使用了一个简单的DP思维,使用一个数组存储从左到右一直到I  P的数量,然后使用一个数组存储从右到左一直到I T的数量,然后遍历一遍原数组,找到A之后计算这个位置的索引,然后这个A左边的P的数量和这个A的右边的T的数量相乘就是以这个A为中心的PAT的数量

遍历所有的A,结果相加就是最终的结果

记得要取模,还有就是数值不要越界

#include <iostream>
#include <string>
#define theMod 1000000007
using namespace std;
int leftToRightP[100003];
int rightToLeftT[100003];
int main()
{
	string startStr;
	cin >> startStr;
	for (int i = 0; i < startStr.size(); i++)
	{
		if (startStr[i] == 'P')
		{
			leftToRightP[i] = leftToRightP[i - 1] + 1;
		}
		else
		{
			leftToRightP[i] = leftToRightP[i - 1];
		}
	}
	for (int i = startStr.size()-1; i >=0; i--)
	{
		if (startStr[i] == 'T')
		{
			rightToLeftT[i] = rightToLeftT[i + 1] + 1;
		}
		else
		{
			rightToLeftT[i] = rightToLeftT[i + 1];
		}
	}
	long long patCount=0;
	for (int i = 0; i < startStr.size(); i++)
	{
		if (startStr[i] == 'A')
		{
			patCount += (((long long)leftToRightP[i])* rightToLeftT[i]);
			patCount %= theMod;
		}
	}
	cout << patCount;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值