文章
2
粉丝
6
获赞
15
访问
1.5k
 
对于每一层,有两个下标,left和right,初始分别为当前层最左边树的下标,当前层最右边树的下标。
while(left<right) 比较 str[left], str[right],判断是否对称
#include <bits/stdc++.h>
using namespace std;
int main() {
    string str;
    while(cin>>str) {
        bool issym=true;
        int n=str.size();
        int level=log(n+1)/log(2);  // 总层数
        for(int l=1; l<=level; l++) { // 一层一层判断
            int left=pow(2,l-1)-1; // 一层的最左边节点下标
            int right=pow(2,l)-2; // 一层的最右边节点下标
            while(left<right) { // 判断左右是否对称
                if(str[left]>='A'&&str[left<='Z'] && str[right]=='#') issym=false; // 若左树为字母,右树为#,不对称
                if(str[right]>='A'&&str[right<='Z'] && str[left]=='#') issym=false; // 若右树为字母,左树为#,不对称
                left++;    //缩减范围
                right--;
            }
        }
        if(issym) {
            cout<<"YES"<<endl;
        } else {
            cout<<"NO"<<endl;
   ...
    
登录后发布评论
暂无评论,来抢沙发