【2019牛客暑期多校训练营(第八场)- G】Gemstones(栈,模拟)
題干:
鏈接:https://ac.nowcoder.com/acm/contest/888/G
來源:牛客網(wǎng)
?
Gromah and LZR have entered the seventh level. There are a sequence of gemstones on the wall.
?
After some tries, Gromah discovers that one can take exactly three successive gemstones with the same types away from the gemstone sequence each time, after taking away three gemstones, the left two parts of origin sequence will be merged to one sequence in origin order automatically.
?
For example, as for "ATCCCTTG", we can take three 'C's away with two parts "AT", "TTG" left, then the two parts will be merged to "ATTTG", and we can take three 'T's next time.
?
The password of this level is the maximum possible times to take gemstones from origin sequence.
?
Please help them to determine the maximum times.
輸入描述:
?Only one line containing a string ss_{}s?, denoting the gemstone sequence, where the same letters are regarded as the same types.
?
?
1≤∣s∣≤1051 \le |s| \le 10^51≤∣s∣≤105
?
ss_{}s? only contains uppercase letters.
輸出描述:
Print a non-negative integer in a single line, denoting the maximum times.示例1
輸入
復(fù)制
ATCCCTTG輸出
復(fù)制
2說明
One possible way is that ‘‘ATCCCTTG?"??→??‘‘ATTTG?"??→‘‘AG?"``ATCCCTTG\," \; \rightarrow \; ``ATTTG\," \; \rightarrow ``AG\,"‘‘ATCCCTTG"→‘‘ATTTG"→‘‘AG".題目大意:
給一個字符串 3個連續(xù)且相同的能夠消去 問這個字符串最多消去幾次
解題報告:
棧模擬。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; char s[MAX]; char stk[MAX]; int top = 100,ans; int ok() {if(stk[top] == stk[top-1] && stk[top-1] == stk[top-2]) {top -= 3;return 1;}else return 0; } int main() {cin>>s+1;int len = strlen(s+1);for(int i = 1; i<=len; i++) {stk[++top] = s[i];ans += ok();}printf("%d\n",ans);return 0 ; }?
總結(jié)
以上是生活随笔為你收集整理的【2019牛客暑期多校训练营(第八场)- G】Gemstones(栈,模拟)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: winex.exe - winex是什么
- 下一篇: winfs.exe - winfs是什么