string removeKdigits(string num, int k) {
    std::string increasing(""); // Use it as a stack

    num.push_back('0'); // Add a sentinel to handle ["112","1"] case

    for (char c : num) {
      while (!increasing.empty()
             && increasing.back() > c
             && k > 0) {
        increasing.pop_back();
        -- k;
      }

      increasing.push_back(c);
    }

    increasing.pop_back(); // pop back the '0'

    std::size_t first_non_zero = increasing.find_first_not_of("0");
    if (first_non_zero == std::string::npos) {
      return "0";
    }
    return increasing.substr(first_non_zero);
  }
示例#2
0
int main( int argc, char *argv[] )
{
    int errs = 0;
    int dims[4], nnodes, ndims;

    MTest_Init( &argc, &argv );

    /* Test multiple dims create values.  For each, make sure that the 
       product of dims is the number of input nodes */
    nnodes = 2*3*5*7*11;
    ndims  = 2;
    dims[0] = dims[1] = 0;
    MPI_Dims_create( nnodes, ndims, dims );
    if (prodof(ndims,dims) != nnodes) {
	errs++;
	printf( "dims create returned the wrong decomposition for %d in %d dims\n",
		nnodes, ndims );
    }
    if (increasing( ndims, dims )) {
	errs++;
	printf( "dims create returned a decomposition with increasing dimensions (see MPI-1 standard section 6.5)\n" );
	printf( "dims create returned the wrong decomposition for %d in %d dims\n",
		nnodes, ndims );
    }

    /* Test multiple dims create values.  For each, make sure that the 
       product of dims is the number of input nodes */
    nnodes = 2*7;
    ndims  = 2;
    dims[0] = dims[1] = 0;
    MPI_Dims_create( nnodes, ndims, dims );
    if (prodof(ndims,dims) != nnodes) {
	errs++;
	printf( "dims create returned the wrong decomposition for %d in %d dims\n",
		nnodes, ndims );
    }
    if (increasing( ndims, dims )) {
	errs++;
	printf( "dims create returned a decomposition with increasing dimensions (see MPI-1 standard section 6.5)\n" );
	printf( "dims create returned the wrong decomposition for %d in %d dims\n",
		nnodes, ndims );
    }

    nnodes = 2*2*3*3*5*7*11;
    ndims  = 2;
    dims[0] = dims[1] = 0;
    MPI_Dims_create( nnodes, ndims, dims );
    if (prodof(ndims,dims) != nnodes) {
	errs++;
	printf( "dims create returned the wrong decomposition for %d in %d dims\n",
		nnodes, ndims );
    }
    if (increasing( ndims, dims )) {
	errs++;
	printf( "dims create returned a decomposition with increasing dimensions (see MPI-1 standard section 6.5)\n" );
	printf( "dims create returned the wrong decomposition for %d in %d dims\n",
		nnodes, ndims );
    }

    nnodes = 11;
    ndims  = 2;
    dims[0] = dims[1] = 0;
    MPI_Dims_create( nnodes, ndims, dims );
    if (prodof(ndims,dims) != nnodes) {
	errs++;
	printf( "dims create returned the wrong decomposition for %d in %d dims\n",
		nnodes, ndims );
    }
    if (increasing( ndims, dims )) {
	errs++;
	printf( "dims create returned a decomposition with increasing dimensions (see MPI-1 standard section 6.5)\n" );
	printf( "dims create returned the wrong decomposition for %d in %d dims\n",
		nnodes, ndims );
    }

    nnodes = 5*7*11;
    ndims  = 4;
    dims[0] = dims[1] = dims[2] = dims[3] = 0;
    MPI_Dims_create( nnodes, ndims, dims );
    if (prodof(ndims,dims) != nnodes) {
	errs++;
	printf( "dims create returned the wrong decomposition for %d in %d dims\n",
		nnodes, ndims );
    }
    if (increasing( ndims, dims )) {
	errs++;
	printf( "dims create returned a decomposition with increasing dimensions (see MPI-1 standard section 6.5)\n" );
	printf( "dims create returned the wrong decomposition for %d in %d dims\n",
		nnodes, ndims );
    }

    nnodes = 64;
    ndims  = 4;
    dims[0] = dims[1] = dims[2] = dims[3] = 0;
    MPI_Dims_create( nnodes, ndims, dims );
    if (prodof(ndims,dims) != nnodes) {
	errs++;
	printf( "dims create returned the wrong decomposition for %d in %d dims\n",
		nnodes, ndims );
    }
    if (increasing( ndims, dims )) {
	errs++;
	printf( "dims create returned a decomposition with increasing dimensions (see MPI-1 standard section 6.5)\n" );
	printf( "dims create returned the wrong decomposition for %d in %d dims\n",
		nnodes, ndims );
    }

    MTest_Finalize( errs );
    MPI_Finalize();
    return 0;
  
}