D1D2的250p都是水题,特别纯净的那种。
D1 500p HexatridecimalSum
高精度模板上吧!
D1 1000p IncreasingLists
花了半个下午搞定。首先搜索最长不下降序列划分逗号,然后贪心。贪心时注意:只要某一位被预置,并且与之前的string在该位不同,那么就可以对后面的数位置0.还有一些细节,比如开头不能为0。
class IncreasingLists
{
int lst[30];
string str[30], m;
void print ( int n )
{
for ( int i = 0; i < n; i ++ )
printf ( "%d ", lst[i] );
printf ( "/n" );
}
void check ( int num )
{
//print ( num );
string temp = "";
string last, cur;
int len = 0;
for ( int i = 0; i < num; i ++ )
{
if ( i )
temp += ',';
cur.resize ( lst[i], ' ' );
if ( lst[i] > len )
{
len = lst[i];
cur[0] = str[i][0] == '?' ? '1' : str[i][0];
for ( int j = 1; j < len; j ++ )
{
cur[j] = str[i][j] == '?' ? '0' : str[i][j];
}
}
else if ( lst[i] == len )
{
int pos, j;
for ( pos = 0; pos < len; pos ++ )
{
if ( str[i][pos] != '?' && str[i][pos] != last[pos] )
break;
}
for ( j = pos; j < len; j ++ )
cur[j] = str[i][j] == '?' ? '0' : str[i][j];
int less = 1;
if ( pos < len && cur[pos] > last[pos] )
less = 0;
for ( j = pos - 1; j >= 0; j -- )
{
if ( str[i][j] != '?' )
{
cur[j] = str[i][j];
}
else
{
if ( less )
{
cur[j] = last[j] + 1;
if ( cur[j] > '9' )
cur[j] = '0', less = 1;
else
less = 0;
}
else
cur[j] = last[j];
}
}
if ( less )
return;
}
if ( cur[0] == '0' )
return;
last = cur;
temp += cur;
}
if ( ans.size () == 0 || temp < ans )
ans = temp;
}
void dfs ( int last, int cur, int len, int num, string sub )
{
if ( m.size () - last - 1 < len )
return;
if ( cur == m.size () )
{
int tlen = cur - last - 1;
if ( tlen >= len )
{
lst[num] = tlen;
str[num ++] = sub;
//print ( num );
check ( num );
}
return;
}
int tlen = cur - last - 1;
if ( m[cur] == ',' )
{
if ( tlen >= len )
{
lst[num] = tlen;
str[num] = sub;
dfs ( cur, cur + 1, tlen, num + 1, "" );
}
return;
}
if ( m[cur] >= '0' && m[cur] <= '9' )
{
dfs ( last, cur + 1, len, num, sub + m[cur] );
return;
}
if ( tlen >= len )
{
lst[num] = tlen;
str[num] = sub;
dfs ( cur, cur + 1, tlen, num + 1, "" );
}
dfs ( last, cur + 1, len, num, sub + m[cur] );
}
public:
string makeIncreasingList(string mask)
{
m = mask;
ans = "";
dfs ( -1, 0, 1, 0, "" );
if ( ans.size () == 0 )
return "impossible";
else
return ans;
}
}