题解:P13186 [GCJ 2016 Qualification] Revenge of the Pancakes

· · 题解

P13186 Revenge of the Pancakes 题解

题目传送门

先看题目:

题目给我们一个字符串(只包括+或-),让我们做如下改动:

层数\改动次数 0(原串) 1 2
1 + - +
2 + - +
3 - + +
4 - + +
5 + + +

AC Code:

#include<bits/stdc++.h>
using namespace std;
string s;
int t;
void turn(int n){//翻转函数
    for(int j=n;j>=0;j--)
        s[j]=(s[j]=='-')?'+':'-';
}
int main(){
    cin>>t;
    for(int cl=1;cl<=t;cl++){
        int ans=0;
        cin>>s;
        for(int i=s.size();i>=0;i--){//倒序遍历
            if(s[i]=='-'){
                ans++;
                turn(i);
            }
        }
        printf("Case #%d: %d\n",cl,ans);
    }
    return 0;
}