zoukankan      html  css  js  c++  java
  • 算法初步——其他高效技巧与算法B1040/A1093.有几个PAT

    暴力:

    #include <bits/stdc++.h>
    #include<math.h>
    #include <string>
    using namespace std;
    const int N = 100005;
    long int temp[N];
    int main(){
        char temp[N];
        cin>>temp;
        int len = strlen(temp);
        int result = 0;
        vector<int> res(2);
        for(int i=0;i<len;++i){
            if(temp[i] == 'P'){
                int j = i;
                for(j++;j<len;++j){
                    if(temp[j] == 'A'){
                        int s = j;
                        for(s++;s<len;++s){
                            if(temp[s] == 'T'){
                                result++;
                            }
                        }
                    }
                }
            }
        }
        cout<<result<<endl;
        system("pause");
        return 0;
    } 

    换个角度思考问题:

    对于一个确定位置的A来说,以它形成的PAT的个数等于它左边P的个数乘以它右边T的个数。

    #include <bits/stdc++.h>
    #include<math.h>
    #include <string>
    using namespace std;
    const int MAXN = 100005;
    const int MOD = 1000000007;
    char str[MAXN];
    int leftNumP[MAXN] = {0};
    int main(){
        cin>>str;
        int len = strlen(str);
        for(int i=0;i<len;++i){
            if(i > 0){//如果不是0号位
                leftNumP[i] = leftNumP[i - 1];//继承上一位的结果
            }
            if(str[i] == 'P'){
                leftNumP[i]++;
            }
        }
        //ans为答案,rightNumT记录右边T的个数
        int ans = 0,rightNumT= 0;
        for(int i=len-1;i>=0;--i){
            if(str[i] == 'T'){
                rightNumT++;
            }else if(str[i] =='A'){//当前位为A
                ans = (ans+leftNumP[i] * rightNumT) % MOD;
            }
        }
        cout<<ans<<endl;
        system("pause");
        return 0;
    } 
  • 相关阅读:
    javascript异步编程学习及实例
    通过调试vue-cli 构建代码学习vue项目构建运行过程
    web技术栈开发原生应用-多端共用一套代码
    vuejs应用开发前后端分离
    web前端跨域解决方案JSONP,CORS,NGINX反向代理
    js script 加载顺序
    js 百度地图
    layui-table 样式
    css 排版 test
    layui table
  • 原文地址:https://www.cnblogs.com/JasonPeng1/p/12194079.html
Copyright © 2011-2022 走看看