-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
451b697
commit 9d118ab
Showing
5 changed files
with
60 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# A1093 Count PAT's | ||
|
||
## 1.题意理解 | ||
统计字符串里有几个```PAT```子串 | ||
|
||
## 2.思路分析 | ||
关键在于抓住中心字母“A”,统计每一个A的左边有几个P,右边有几个T。 | ||
|
||
## 3.参考代码 | ||
```cpp | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
const int N = 100100; | ||
const int MOD = 1000000007; | ||
|
||
int leftP[N] = {0}; | ||
|
||
int main() | ||
{ | ||
string s; | ||
cin >> s; | ||
for(int i = 0; i < s.length(); i++) | ||
{ | ||
if(i > 0) | ||
leftP[i] = leftP[i - 1]; | ||
if(s[i] == 'P') | ||
leftP[i]++; | ||
} | ||
int ans = 0, rightT = 0; | ||
for(int i = s.length() - 1; i >= 0; i--) | ||
{ | ||
if(s[i] == 'T') | ||
rightT++; | ||
else if(s[i] == 'A') | ||
ans = (ans + leftP[i] * rightT) % MOD; | ||
} | ||
cout << ans; | ||
return 0; | ||
} | ||
``` | ||
> 注意:在求解leftP数组过程中,两个if不能写反。其实只要是不构成else if关系的,都会进入判断,这时一定要注意顺序。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.