PAT--1093. Count PAT's (25)

Description

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

题解

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <string>
using namespace std;

const int mod  = 1000000007;
const int maxn = 100000 + 3;
int dp[maxn][3];    //长度为i, P, PA, PAT的个数分别为dp[i][0], dp[i][1], dp[i][2]
int cntA[maxn], cntT[maxn]; //第i个字符前'P'的个数,第i个字符后'T'的个数

void solve(string& s){
    int ans = 0;
    if(s[0] == 'P') dp[0][0] = 1;
    for(int i = 1; i < s.length(); ++i){
        dp[i][0] = dp[i - 1][0];
        dp[i][1] = dp[i - 1][1];
        if(s[i] == 'P') dp[i][0]++;
        else if(s[i] == 'A') dp[i][1] += dp[i][0];
        else{
            dp[i][2] += dp[i][1];
            ans = (ans + dp[i][2]) % mod;
        }
    }
    cout << ans << endl;
}

void solve1(string& s){
    for(int i = 1; i <= s.length(); ++i){
        cntA[i] = cntA[i - 1];
        if(s[i - 1] == 'P') cntA[i]++;
    }
    for(int i = s.length(); i > 0; --i){
        cntT[i] = cntT[i + 1];
        if(s[i] == 'T') cntT[i]++;
    }
    int ans = 0;
    for(int i = 1; i <= s.length(); ++i){
        if(s[i-1] == 'A') ans = (ans + cntA[i] * cntT[i]) % mod;
    }
    cout << ans << endl;
}

int main(){
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif // ONLINE_JUDGE
    string s;
    cin >> s;
    solve1(s);
    // solve(s);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
function copy-data([int]$startrow, [int]$startcol, [int]$endrow, [int]$endcol, [string]$pat, [string]$file, [int]$sheetfrom, [int]$sheetto) { $excel = New-Object -ComObject Excel.Application $excel.Visible = $false $wb = $excel.Workbooks.Open($file) $wsfrom = $wb.Sheets.Item($sheetfrom) $wsto = $wb.Sheets.Item($sheetto) $sr = $wsfrom.Range($wsfrom.Cells.Item($startrow, $startcol), $wsfrom.Cells.Item($endrow, $endcol)) $fc = $sr.Find($pat) if ($fc -ne $null) { $rown = $fc.Row $dsr = $wsto.Range($wsto.Cells.Item($rown, $startcol), $wsto.Cells.Item($rown + ($endrow - $startrow), $endcol)) $dsr = $dsr.Resize($sr.Rows.Count, $sr.Columns.Count) $sr.Copy() $dsr.PasteSpecial(-4104) } $excel.DisplayAlerts = $false $wb.Close($true) [Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null } $path = $PWD.Path $file = "$path\a.xlsx" # 将sheet1中第1行到第4行、第2列到第9列的数据复制到sheet2中 copy-data -startrow 1 -startcol 2 -endrow 4 -endcol 9 -pat 1009 -file $file -sheetfrom 1 -sheetto 2 # 将sheet1中第6行到第8行、第2列到第9列的数据复制到sheet2中 copy-data -startrow 6 -startcol 2 -endrow 8 -endcol 9 -pat 1018 -file $file -sheetfrom 1 -sheetto 2 # 将sheet1中第10行到第11行、第2列到第9列的数据复制到sheet2中 copy-data -startrow 10 -startcol 2 -endrow 11 -endcol 9 -pat 1023 -file $file -sheetfrom 1 -sheetto 2 这个是搜索sheet1中关键词吗,我现在要搜索sheet2中关键词 得到行信息后再把sheet1指定内容复制到sheet2指定区域,怎么修改呢
05-19
可以按照以下步骤修改代码: 1. 将函数名 `copy-data` 修改为 `copy-data-to-sheet2`,以表明该函数是将数据从 sheet1 复制到 sheet2。 2. 在函数中添加一个新的参数 `$sheetfromname`,用于指定要搜索的 sheet 名称。 3. 修改 `$wsfrom` 的赋值语句,使用 `$wb.Sheets.Item($sheetfromname)` 获取指定的 sheet 对象。 4. 修改 `$wsto` 的赋值语句,使用 `$wb.Sheets.Item(2)` 获取 sheet2 对象。 5. 修改 `$dsr` 的赋值语句,使用 `$wsto.Cells.Item($rown, $startcol)` 获取指定单元格的对象。 6. 将 `$dsr.Resize($sr.Rows.Count, $sr.Columns.Count)` 修改为 `$dsr.Resize($endrow-$startrow+1, $endcol-$startcol+1)`,以根据复制范围计算新区域的大小。 7. 在调用函数时,将参数 `-sheetfrom` 修改为 `-sheetfromname`,并指定要搜索的 sheet 名称。 修改后的代码如下: ``` function copy-data-to-sheet2([int]$startrow, [int]$startcol, [int]$endrow, [int]$endcol, [string]$pat, [string]$file, [string]$sheetfromname, [int]$sheetto) { $excel = New-Object -ComObject Excel.Application $excel.Visible = $false $wb = $excel.Workbooks.Open($file) $wsfrom = $wb.Sheets.Item($sheetfromname) $wsto = $wb.Sheets.Item(2) $sr = $wsfrom.Range($wsfrom.Cells.Item($startrow, $startcol), $wsfrom.Cells.Item($endrow, $endcol)) $fc = $sr.Find($pat) if ($fc -ne $null) { $rown = $fc.Row $dsr = $wsto.Cells.Item($rown, $startcol) $dsr = $dsr.Resize($endrow-$startrow+1, $endcol-$startcol+1) $sr.Copy() $dsr.PasteSpecial(-4104) } $excel.DisplayAlerts = $false $wb.Close($true) [Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null } $path = $PWD.Path $file = "$path\a.xlsx" # 将sheet1中包含关键词 1009 的行复制到sheet2中指定区域 copy-data-to-sheet2 -startrow 1 -startcol 2 -endrow 4 -endcol 9 -pat 1009 -file $file -sheetfromname "sheet1" -sheetto 2 # 将sheet1中包含关键词 1018 的行复制到sheet2中指定区域 copy-data-to-sheet2 -startrow 6 -startcol 2 -endrow 8 -endcol 9 -pat 1018 -file $file -sheetfromname "sheet1" -sheetto 2 # 将sheet1中包含关键词 1023 的行复制到sheet2中指定区域 copy-data-to-sheet2 -startrow 10 -startcol 2 -endrow 11 -endcol 9 -pat 1023 -file $file -sheetfromname "sheet1" -sheetto 2 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值