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

  • 相关阅读:
    Spring注解驱动开发3:自动装配
    Spring注解驱动开发2:生命周期和属性赋值
    Spring注解驱动开发1:组件注册
    Java线程及其实现方式
    Winform 可取消的单选按钮(RadioButton)
    autoit脚本-从基本的函数用法开始
    python进阶(一)
    dict字典的一些优势和劣势
    读《流畅的python》第一天
    智能化脚本autoit v3的简单了解
  • 原文地址:https://www.cnblogs.com/houzm/p/12527598.html
Copyright © 2011-2022 走看看