zoukankan      html  css  js  c++  java
  • PAT Advanced 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

    题目分析

    求字符串中所有PAT,不需要P/A/T都紧挨着,比如APPAPT中有两个PAT,*P*A*T和**PA*T
    观察可知:PAT个数为所有 A字符左边的P个数*A字符右边的T个数 之和

    解题思路

    正序遍历,记录所有位置之前的P个数,保存在lp数组中,若当前位置字符为P,lp[i]=lp[i-1]+1,若不是P,lp[i]=lp[i-1]
    逆序遍历,记录所有位置右边的T个数,保存在;遇到A进行统计 左边P个数*右边T个数

    Code

    #include <iostream>
    using namespace std;
    const int maxn=100010;
    const int mod=1000000007;
    int lp[maxn];
    char s[maxn];
    int rt,ans;
    int main(int argc,char * argv[]) {
    	string s;
    	cin>>s;
    	for(int i=0; i<s.length(); i++) {
    		if(i>0)lp[i]=lp[i-1];
    		if(s[i]=='P')lp[i]++;
    	}
    	for(int i=s.length()-1; i>=0; i--) {
    		if(s[i]=='A')ans=(ans+lp[i]*rt)%mod;
    		if(s[i]=='T')rt++;
    	}
    	printf("%d",ans);
    	return 0;
    }
    

  • 相关阅读:
    mysqli使用记录
    D3力布图绘制--基本方法
    使用SVG绘制流程图
    关于echarts绘制树图形的注意事项(文字倾斜、数据更新、缓存重绘问题等)
    如何在iview组件中使用jsx
    素描学习记录2
    关于react-router-dom的一些记录
    素描学习记录1
    Typescript中一些不理解的概念解释(泛型、断言、解构、枚举)
    关于this的全面解析(call,apply,new)
  • 原文地址:https://www.cnblogs.com/houzm/p/12527598.html
Copyright © 2011-2022 走看看