博客
关于我
1093 Count PAT‘s (25分) 含DP做法
阅读量:376 次
发布时间:2019-03-05

本文共 1694 字,大约阅读时间需要 5 分钟。

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 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

方法一:题目意思就是找出一个A左边的P个数*右边的T,构造两个数组分别存储在字符串i位置左边P的个数和右边T的个数,然后遍历字符串将合理位置的两个数组的数乘起来相加即可,不要忘了最后模1000000007,设字符串长度N ,整个复杂度就是遍历了三遍字符串所以还是O(N)的,不会超时。这种做法比较直接,也比较容易想到

AC代码

#include
#include
#include
using namespace std;#define N 1000000007typedef long long LL;string m;int main(){ LL res=0; cin >> m; vector
a(m.size(),0); vector
b(m.size(),0); for(int i=1;i
=0;i--) if(m[i+1]=='T') b[i]=b[i+1]+1; else b[i]=b[i+1]; for(int i=0;i

方法二:DP

#include
#include
#include
using namespace std;#define N 1000000007char s[100010],p[]=" PAT";int f[100010][4];int main(){ cin >> s+1; int n=strlen(s+1); f[0][0]=1; for(int i=1;i<=n;i++) for(int j=0;j<=3;j++) { f[i][j]=f[i-1][j]; if(s[i]==p[j]) f[i][j]=(f[i][j]+f[i-1][j-1])%N; } cout << f[n][3]; return 0;}

转载地址:http://azbwz.baihongyu.com/

你可能感兴趣的文章
Netty中实现多客户端连接与通信-以实现聊天室群聊功能为例(附代码下载)
查看>>
Netty中的组件是怎么交互的?
查看>>
Netty中集成Protobuf实现Java对象数据传递
查看>>
netty之 定长数据流处理数据粘包问题
查看>>
Netty事件注册机制深入解析
查看>>
netty代理
查看>>
Netty入门使用
查看>>
netty入门,入门代码执行流程,netty主要组件的理解
查看>>
Netty原理分析及实战(一)-同步阻塞模型(BIO)
查看>>
Netty原理分析及实战(三)-高可用服务端搭建
查看>>
Netty原理分析及实战(二)-同步非阻塞模型(NIO)
查看>>
Netty原理分析及实战(四)-客户端与服务端双向通信
查看>>
Netty发送JSON格式字符串数据
查看>>
Netty和Tomcat的区别已经性能对比
查看>>
Netty在IDEA中搭建HelloWorld服务端并对Netty执行流程与重要组件进行介绍
查看>>
Netty基础—1.网络编程基础一
查看>>
Netty基础—1.网络编程基础二
查看>>
Netty基础—2.网络编程基础三
查看>>
Netty基础—2.网络编程基础四
查看>>
Netty基础—3.基础网络协议一
查看>>