文章

17

粉丝

177

获赞

2

访问

111.0k

头像
直接使用字符串做递归判断,需要计算大于根的字串和小于根的字串
P1317 浙江大学机试题
发布于2021年9月16日 13:23
阅读数 6.4k

#include<iostream>
using namespace std;

string larger_than_root(string s){  //计算比根大的子串
    int start_pos = 0;
    while(start_pos < s.length()){
        if(s[start_pos] > s[0]) break;
        else start_pos++;
    }
    int end_pos = start_pos + 1;
    while(end_pos < s.length()){
        if(s[end_pos] < s[0]) break;
        else end_pos++;
    }
    return s.substr(start_pos, end_pos - start_pos);
}
string lower_than_root(string s){  //计算比根小的字串
    int start_pos = 0;
    while(start_pos < s.length()){
        if(s[start_pos] < s[0]) break;
        else start_pos++;
    }
    int end_pos = start_pos + 1;
    while(end_pos < s.length()){
        if(s[end_pos] > s[0]) break;
        else end_pos++;
    }
    return s.substr(start_pos, end_pos - start_pos);

}
bool is_same_search_tree(string s1,string s2){
    if(s1.length() != s2.length()) return false; //如果长度都不相等肯定不是
    else if(s1.length() == 0){ //都是长度为零,肯定是
        ...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发