I'm solving a algorithm problem https://codeforces.com/contest/1671/problem/E. Although my submit can pass the tests provided by the contest, I find it fails on specific test(Hack). When I'm trying to find where's the error, I find that if I choose start debugging, the program would run perfectly. However, when I click "run", it would give a wrong answer. So, I'm curious about what happens.
#include<iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int const NN = 1e6;
int const MOD = 998244353;
char str[NN];
long long dfs_data[NN];
int powans[20];
string myhash[NN];
int n;
long long dfs(int num) {
if (dfs_data[num] != 0) return dfs_data[num];
if (num >= powans[n - 1] - 1) {
dfs_data[num] = 1;
return 1;
}
if (myhash[num * 2 + 1] == myhash[num * 2 + 2]) {
dfs_data[num] = (dfs(num * 2 + 1) % MOD) * (dfs(num * 2 + 2) % MOD) % MOD;
} else dfs_data[num] = 2 * (dfs(num * 2 + 1) % MOD) * (dfs(num * 2 + 2) % MOD) % MOD;
return dfs_data[num];
}
void gethashcode(int t) {
if (t >= powans[n - 1] - 1) {
myhash[t] += str[t];
return;
}
if (myhash[2 * t + 1] == "") gethashcode(2 * t + 1);
if (myhash[2 * t + 2] == "") gethashcode(2 * t + 2);
if (myhash[2 * t + 1] < myhash[2 * t + 2]) myhash[t] = str[t] + myhash[2 * t + 1] + myhash[2 * t + 2];
else myhash[t] = str[t] + myhash[2 * t + 2] + myhash[2 * t + 1];
}
void solve() {
memset(dfs_data, 0, sizeof dfs_data);
cin >> n;
cin >> str;
powans[0] = 1;
for (int i = 1; i < 19; i++) {
powans[i] = 2 * powans[i - 1];
}
for (int i = 0; i < NN; i++) {
myhash[i] = "";
}
gethashcode(0);
cout << dfs(0);
}
int main() {
solve();
}
I have a 2d char array, of which I am trying to retrieve the value of an opposite side while on the edge. Instead of retrieving that value, it keeps returning with null. Can't make heads or tails of what the problem is. Here is the code I traced the problem to.
int xO = x; int yO = y;
//reassigns to opposite side of array
if ((x == 0) && (xM == - 1)) { xM = mapSize - 1; }
if ((x == mapSize - 1) && (xM == 1)) { xM = 0 - (mapSize - 1); }
if ((y == 0) && (yM == - 1)) { yM = mapSize - 1; }
if ((y == mapSize - 1) && (yM == 1)) { yM = 0 - (mapSize - 1);
}
//Checks 9 chars around and assigns any found characters
if (mapPlates[x + xM][y + yM] == 0) {
std::cout << "//" + std::to_string(x + xM) + ", " + std::to_string(yM + y) + "; " + std::to_string(mapPlates[x + xM][y + yM]) + "//";
}
if (mapPlates[x + xM][y + yM] =! "%") {
remaining--;
mapPlates[xO][yO] = mapPlates[x + xM][y + yM];
std::cout << "{assigned " + std::to_string(mapPlates[xO][yO]) + " }";
}
return remaining;
}
Snippet of output(only returns values == null)(should be returning '%'):
```//14, 49; 0////14, 0; 0////14, 1; 0////19, 49; 0////20, 49; 0////19, 0;
The output is 34 but How this expression evaluated?
Could you please show with parenthesis?
Correct operator precedence is:
a += ((((((2 * i++) % 5) * 4) + (--j)) - (3 / k)) + 2);
But I think correct should be:
a += (((((2 * i++) % (5 * 4)) + (--j)) - (3 / k)) + 2);
#include <iostream>
using namespace std;
int main()
{
int a = 3, i = 12, j = 14, k = 16;
a += 2 * i++ % 5 * 4 + --j - 3 / k + 2;
cout << a;
}
The operators *, / and % have same precedence and are grouped left to right. Therefore it is ((2 * i++) % 5) * 4 and not (2 * i++) % (5 * 4)
I'm kinda new to c++ and I have an assignment for my course to read a P6 ppm image and use some filters on it.Im stuck on the blurring one and I cannot find my logical error on my code.
My problem is that the output image is blurred but it is like there are 4 blurred same images there
EDIT- HERE IS THE LINK TO OUTPUT IMAGE(TESTFILTER.ppm) THE INPUT IMAGE (Original) AND A SIMPLE VIEWER TO VIEW THEM MY PROFFESSOR GAVE US (CPIViewer)
http://www.filedropper.com/blurfilterquestion
Here is the blur filter code.
typedef Vec3<float> buffer;
static void blur(Image* pic) { //BLUR FILTER
int h = pic->getHeight();
int w = pic->getWidth();
int count = 0;
buffer* temp = new buffer[h*w];
for (int x = 0; x < w; x++){
for (int y = 0; y < h; y++){
if (x == 0 && y == 0) {
count++;
temp[y*w + x] = (pic->getPixel(x, y) + pic->getPixel(x + 1, y) + pic->getPixel(x, y + 1) + pic->getPixel(x + 1, y + 1)) / 4;
}
else if (x == 0 && y == h - 1) {
count++;
temp[y*w + x] = (pic->getPixel(x, y) + pic->getPixel(x + 1, y) + pic->getPixel(x, y - 1) + pic->getPixel(x + 1, y - 1)) / 4;
}
else if (x == w - 1 && y == 0){
count++;
temp[y*w + x] = (pic->getPixel(x, y) + pic->getPixel(x - 1, y) + pic->getPixel(x, y + 1) + pic->getPixel(x - 1, y + 1)) / 4;
}
else if (x == w - 1 && y == h - 1){
count++;
temp[y*w + x] = (pic->getPixel(x, y) + pic->getPixel(x - 1, y) + pic->getPixel(x, y - 1) + pic->getPixel(x - 1, y - 1)) / 4;
}
else if (x == 0){
count++;
temp[y*w + x] = ((pic->getPixel(x, y)) + (pic->getPixel(x, y - 1)) + (pic->getPixel(x, y + 1)) + (pic->getPixel(x + 1, y - 1)) + (pic->getPixel(x + 1, y)) + (pic->getPixel(x + 1, y + 1))) / 6;
}
else if (x == w - 1){
count++;
temp[y*w + x] = ((pic->getPixel(x, y)) + (pic->getPixel(x, y - 1)) + (pic->getPixel(x, y + 1)) + (pic->getPixel(x - 1, y + 1)) + (pic->getPixel(x - 1, y)) + (pic->getPixel(x - 1, y - 1))) / 6;
}
else if (y == 0){
count++;
temp[y*w + x] = ((pic->getPixel(x, y)) + (pic->getPixel(x, y - 1)) + (pic->getPixel(x, y + 1)) + (pic->getPixel(x - 1, y + 1)) + (pic->getPixel(x - 1, y)) + (pic->getPixel(x + 1, y + 1))) / 6;
}
else if (y == h - 1){
count++;
temp[y*w + x] = ((pic->getPixel(x, y)) + (pic->getPixel(x, y - 1)) + (pic->getPixel(x - 1, y)) + (pic->getPixel(x + 1, y - 1)) + (pic->getPixel(x + 1, y)) + (pic->getPixel(x - 1, y - 1))) / 6;
}
else {
count++;
temp[y*w + x].r = ((pic->getPixel(x, y).r) + (pic->getPixel(x + 1, y).r) + (pic->getPixel(x, y + 1).r) + (pic->getPixel(x, y - 1).r) + (pic->getPixel(x - 1, y).r) + (pic->getPixel(x - 1, y - 1).r) + (pic->getPixel(x + 1, y + 1).r) + (pic->getPixel(x + 1, y - 1).r) + (pic->getPixel(x - 1, y + 1).r)) / 9;
temp[y*w + x].g = ((pic->getPixel(x, y).g) + (pic->getPixel(x + 1, y).g) + (pic->getPixel(x, y + 1).g) + (pic->getPixel(x, y - 1).g) + (pic->getPixel(x - 1, y).g) + (pic->getPixel(x - 1, y - 1).g) + (pic->getPixel(x + 1, y + 1).g) + (pic->getPixel(x + 1, y - 1).g) + (pic->getPixel(x - 1, y + 1).g)) / 9;
temp[y*w + x].b = ((pic->getPixel(x, y).b) + (pic->getPixel(x + 1, y).b) + (pic->getPixel(x, y + 1).b) + (pic->getPixel(x, y - 1).b) + (pic->getPixel(x - 1, y).b) + (pic->getPixel(x - 1, y - 1).b) + (pic->getPixel(x + 1, y + 1).b) + (pic->getPixel(x + 1, y - 1).b) + (pic->getPixel(x - 1, y + 1).b)) / 9;
}
}
}
const buffer* constTemp;
constTemp = temp;
pic->setData(constTemp);
std::cout << "TIMES: " << count << std::endl;
here is the getPixel method
typedef Vec3<float> color;
enter code here
color Image::getPixel(unsigned int x, unsigned int y) const {
if (x > getWidth() || y > getHeight()) { color col(0, 0, 0); return col; }
color col = buffer[y*width + x];
return col;
}
and here is my main() method
#include "Image.h"
#include "ppm_format.h"
#include "Filters.h"
#include <iostream>
using namespace imaging;
using namespace std;
using namespace filtering;
int main(int argc, char* argv[]) {
Image* image = ReadPPM(argv[argc - 1]); // Creating Image
float r, g, b;
bool filterFlag = false; //Flag used to check if -f command is given
string check;
Vec3<float> temp;
for (int i = 1; i < argc - 2; i++) {
string check(argv[i]);
if (!check.compare("-f")) { filterFlag = true; } // check.compare("-f") returns 0 if check = -f
if (filterFlag) {
i++;
check = argv[i];
if (!check.compare("gray")) {
filterFlag = false; //filter flag falsified to show that a filter is read
gray(image);
}
else if (!check.compare("color")) {
filterFlag = false;
if (argc-2 <= i + 3) {
cout << "INVALID COLOR FILTER CALL.EXITING...";
system("pause");
exit(0);
}
r = i + 1;
g = i + 2;
b = i + 3;
i += 4;
color(image, r, g, b);
}
else if (!check.compare("blur")) {
filterFlag = false;
blur(image);
}
else if (!check.compare("median")){
filterFlag = false;
medianORdiff(image,"median");
}
else if (!check.compare("diff")){
filterFlag = false;
medianORdiff(image,"diff");
}
else {
cout << "Invalid filter name or no filter name at all.\nFilter names are gray , color r g b , blur , median , diff";
system("pause");
exit(0);
}
*image >> "C://Users/M1TR0S0UL4S/Desktop/TESTFILTER.ppm";
}
}
When attempted to push back a vector of UINT, the progrma crashes with Critical error detected c0000374. Below is the initial code:
void Terrain::CreateIndexList(UINT Width, UINT Height){
UINT sz_iList = (Width - 1)*(Height - 1) * 6;
UINT *iList = new UINT[sz_iList];
for (int i = 0; i < Width; i++){
for (int j = 0; j < Height; j++){
iList[(i + j * (Width - 1)) * 6] = ((UINT)(2 * i));
iList[(i + j * (Width - 1)) * 6 + 1] = (UINT)(2 * i + 1);
iList[(i + j * (Width - 1)) * 6 + 2] = (UINT)(2 * i + 2);
iList[(i + j * (Width - 1)) * 6 + 3] = (UINT)(2 * i + 2);
iList[(i + j * (Width - 1)) * 6 + 4] = (UINT)(2 * i + 1);
iList[(i + j * (Width - 1)) * 6 + 5] = (UINT)(2 * i + 3);
}
}
for (int i = 0; i < sz_iList; i++){
Geometry.IndexVertexData.push_back(iList[i]);
}
delete[] iList;
}
The goal is to take the generated indices from the iList array and fill the Geometry.IndexVertexData vector array. While debugging this, I've created several other implementations of this:
//After creating the iList array:
Geometry.IndexVertexData.resize(sz_iList); //Fails with "Vector subscript out of range?"
UINT in = 0;
for (int i = 0; i < Width; i++){
for (int j = 0; j < Height; j++){
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6] = iList[in];
in++;
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 1] = iList[in];
in++;
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 2] = iList[in];
in++;
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 3] = iList[in];
in++;
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 4] = iList[in];
in++;
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 5] = iList[in];
in++;
}
}
And a final, direct to vector implementation:
Geometry.IndexVertexData.reserve(sz_iList);
for (int index = 0; index < sz_iList; index+=6) {
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6] = ((UINT)(2 * i));
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 1] = (UINT)(2 * i + 1);
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 2] = (UINT)(2 * i + 2);
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 3] = (UINT)(2 * i + 2);
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 4] = (UINT)(2 * i + 1);
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 5] = (UINT)(2 * i + 3);
}
sz_iList has a final value of 2166, resultant from a grid of 20x20 (400 total points) and is used to initialize sizes. In all cases, the vector array would not fully fill, crashing with Critical error detected c0000374. Am I doing something wrong?
Your sz_iList doesn't appear to be big enough. Let's use a simple example of Width = Height = 2;, then sz_iList = (2 - 1) * (2 - 1) * 6 = 6, right? But in your nested loops, the last iteration occurs when i = j = 1 (i is one less than Width and j is one less than Height), where (in the last line of your loop), you try to access element (i + j * (Width - 1)) * 6 + 5 = (1 + 1 * (2 - 1)) * 6 + 5 = (1 + 1 * 1) * 6 + 5 = 2 * 6 + 5 = 17, which is bigger than the size of your array. This results in undefined behavior.