第七章 回溯算法part03
93.复原IP地址 78.子集 90.子集II
93.复原IP地址
题目地址 :
Code ( 复用 了 131.分割回文串 的 代码 , 同时 对 Component 的 处理 方向 、 遍历 ,/ 选取 方式、 添加 条件 进行 了 调整 , 对 Cache 字符串 的 拼接 ,/ 修剪 维护 进行 了 调整 尤其 是 添加 '.' 分隔 符 后 , Component 的 左 边界 下标 可以 快速 提供 字符串 在 修剪 时 需要 的 一部分 长度 信息 与 前段 形成 '.' 的 数量 形成 长度 信息 ) :
class Solution {
public:
struct Struct_Edge_Left_String_Edge_Right
{
int edge_Left ;
string str_SubStr ;
int edge_Right ;
};
vector<string> restoreIpAddresses(string s) {
int length_s = s.length() ;
// 子串 在 这里 可以 重复
// It ' s different
// seed 位 / 发芽 的 位置
// 回文 串 信息 的 建立
vector<vector<Struct_Edge_Left_String_Edge_Right>> vec_Info_SubStr(length_s , vector<Struct_Edge_Left_String_Edge_Right>(0)) ;
// IP 段 的 添加
for(int i = ( length_s - 1 ) ; i >= 0 ; i-- )
{
int edge_Right = i ;
int edge_Left ;
string str_Temp_Cache = "";
//cout<< " i = " << i << endl ;
for(int j = 0 ; j < 3 ; j ++ )
{
edge_Left = i - j ;
//cout<< " j = " << j << endl ;
if( ( i - j ) < 0 )
{
break ;
}
if(s[i - j ] < 0x30 || s[i - j ] >= 0x40 )
{
break ;
}
if(j == 1 && s[i - j ] == '0')
{
str_Temp_Cache = s[ (i - j) ] + str_Temp_Cache ;
continue ;
}
if(j == 2 )
{
if(s[i - j ] == '0')
{
break ;
}
if(s[i - j ] > '2')
{
break ;
}
// cout<< "s[ (i - j ) + 1 ] = " << s[ (i - j ) + 1 ] << endl ;
// cout<< "s[ (i - j ) + 2 ] = " << s[ (i - j ) + 2 ] << endl ;
// if(s[ (i - j ) + 1 ] == '5')
// {
// cout<<"s[ (i - j ) + 1 ] == '5' : "<< "true" <<endl;
// }
// if(s[ (i - j ) + 2 ] > '5')
// {
// cout<<"s[ (i - j ) + 2 ] > '5' : "<< "true" <<endl;
// }
if(s[ (i - j ) ] == '2' && ( (s[ (i - j ) + 1 ] > '5' ) ) )
{
break ;
}
if(s[ (i - j ) ] == '2' && ( ( s[ (i - j ) + 1 ] == '5' && s[ (i - j ) + 2 ] > '5') ) )
{
break ;
}
// if(s[ (i - j ) ] == '2' && ( (s[ (i - j ) + 1 ] > '5' ) || ( s[ (i - j ) + 1 ] == '5' && s[ (i - j ) + 2 ] > '5') ) )
// {
// break ;
// }
}
str_Temp_Cache = s[ i - j ] + str_Temp_Cache ;
Struct_Edge_Left_String_Edge_Right struct_Edge_Left_String_Edge_Right_Temp ;
struct_Edge_Left_String_Edge_Right_Temp.edge_Left = edge_Left ;
struct_Edge_Left_String_Edge_Right_Temp.edge_Right = edge_Right ;
struct_Edge_Left_String_Edge_Right_Temp.str_SubStr = str_Temp_Cache ;
vec_Info_SubStr[edge_Left].push_back(struct_Edge_Left_String_Edge_Right_Temp) ;
//cout<<"struct_Edge_Left_String_Edge_Right_Temp.str_SubStr = " << struct_Edge_Left_String_Edge_Right_Temp.str_SubStr << endl ;
}
}
// 存到 以 左 起始点 为 下标 的 向量 中 , 结构体 包含 : 左 起始 下标 , 字符串 信息 , 右 结束 下标 / 右 边界
//vector< vector<string>> vec_2_Dimention_For_Receive_And_Return ;
//vector<string> vec_Str_For_Temp_Cache ;
vector< string > vec_Str_For_Receive_And_Return ;
string str_For_Temp_Cache = "" ;
//cout<< 11111111111<< endl ;
// 尝试 使用 回文 字串 拼 出 “ 成 串 ” / “ 原本 整个 的 字符串 ”
fuction_Recursion_For_SpliceWholeStr( 0 , length_s , 1 , vec_Info_SubStr , vec_Str_For_Receive_And_Return , str_For_Temp_Cache ) ;
return vec_Str_For_Receive_And_Return ;
}
void fuction_Recursion_For_SpliceWholeStr( int i_Edge_Left , int & length_s , int num_Segment , vector<vector<Struct_Edge_Left_String_Edge_Right>> & vec_Info_SubStr , vector< string > & vec_Str_For_Receive_And_Return , string & str_For_Temp_Cache )
{
//cout<<" str_For_Temp_Cache = " << str_For_Temp_Cache << endl ;
if(num_Segment > 4 )
{
return ;
}
else
{
}
if(i_Edge_Left >= length_s)
{
return ;
}
else
{
}
for(int i = 0 ; i < vec_Info_SubStr[i_Edge_Left].size() ; i++ )
{
if( ( vec_Info_SubStr[i_Edge_Left][i].edge_Right ) == (length_s - 1 ) )
{
str_For_Temp_Cache += vec_Info_SubStr[i_Edge_Left][i].str_SubStr ;
//
if(num_Segment != 4 )
{
}
else
{
vec_Str_For_Receive_And_Return.push_back(str_For_Temp_Cache) ;
//cout<<" str_For_Temp_Cache = " << str_For_Temp_Cache << endl ;
}
str_For_Temp_Cache = str_For_Temp_Cache.substr(0, ( ( ( (vec_Info_SubStr[i_Edge_Left][i].edge_Left) + 1 ) - 1 ) + ( num_Segment - 1 ) ) ) ;
}
else if( ( vec_Info_SubStr[i_Edge_Left][i].edge_Right ) > (length_s - 1 ))
{
continue ;
}
else if( ( vec_Info_SubStr[i_Edge_Left][i].edge_Right ) < (length_s - 1 ))
{
//cout<<"vec_Info_SubStr[i_Edge_Left][i].str_SubStr = " << vec_Info_SubStr[i_Edge_Left][i].str_SubStr <<endl ;
str_For_Temp_Cache += vec_Info_SubStr[i_Edge_Left][i].str_SubStr + "." ;
//
//cout<<" str_For_Temp_Cache = " << str_For_Temp_Cache << endl ;
// “ 下 一 个 ”
fuction_Recursion_For_SpliceWholeStr( ( vec_Info_SubStr[i_Edge_Left][i].edge_Right + 1 ) , length_s , (num_Segment + 1 ) , vec_Info_SubStr , vec_Str_For_Receive_And_Return , str_For_Temp_Cache ) ;
str_For_Temp_Cache = str_For_Temp_Cache.substr(0, (( ( ( vec_Info_SubStr[i_Edge_Left][i].edge_Left) + 1 ) - 1 ) + ( num_Segment - 1 ) ) ) ;
}
}
}
};