In my code at line it is showing index -97 outofbound why it is showing that where is problem with my code???
I don't understand why it is showing what is wrong with my code or my logic
class Solution {
public:
vector<string> removeAnagrams(vector<string>& words) {
bool ans = true;
for(int i=1;i<words.size();i++){
int temp[26]={0};
for(int j=0;j<words[i].size();j++){
int x = words[i][j];
int y = words[i-1][j];
int check=x-97;
int check1 = y-97;
temp[check]++; // at this place it is showing error
temp[check1]--; // at this place it is showing error
}
for(int k=0;k<26;k++){
if(temp[k]) ans =false;
}
if(ans==true){
words.erase(words.begin()+i);
i--;
}
}
return words;;
}
};
Related
I'm soo sorry I searched for and read similar questions but couldn't understand/use them to solve my own.
Im writing a bool function within an if statement but the function doesn't seem to return false, what am I doing wrong.
My bool function just checks if there are more than one of the given number in an array:
bool findsame(int a[], int b){
int k=0;
for(int i=0;i<20;i++){
if(a[i]==b){
k++;
}
}
if(k>1){
return true;
}
else{
return false;
}
}
int main()
{
const int size=20;
int a[size]={4,4};
int b=4;
if(findsame(a,b)){
cout<<"true";
}
}
I think you got confused why "false" is not getting printed on console with the function returning the false value.
You need to add to an extra else statement to print false on the console:
if(findsame(a,b)){
std::cout<<"true";
}else{
std::cout<<"false";
}
Also, there are two 4 values in the array, therefore always true will get printed.
Try passing value of b other than 4 and 0.
Have a look at the following implementation where value of variable b is equal to 1:
#include<iostream>
bool findsame(int a[], int b){
int k=0;
for(int i=0;i<20;i++){
if(a[i]==b){
k++;
}
}
if(k>1){
return true;
}
else{
return false;
}
}
int main()
{
const int size=20;
int a[size]={4,4};
int b=1;
if(findsame(a,b)){
std::cout<<"true";
}else{
std::cout<<"false";
}
}
Output:
false
PS: I have also tested code for the value of b = 4 and it prints true. Check and Run the code here: https://onlinegdb.com/S1LR5PtvD
I have and QT application with class Solver that solve some numeric problem (Bairstow method for finding roots of the polynomial by finding its distribution to trinomials) but while for smaller instances (5 parameters, in array tabA) it work fine, but when I tried this for larger instances (7 parameters) application crashed.
After I run the debugger I get the following message:
Heap block at 02989F58 modified at 02989F80 past request 20
I'm not exactly sure what is about (well I suppose I get stack overflow but I'm not sure where and how) but it points to line delete[] Q; delete[] W; here is how debugger pointed it:
And here is code of that class methods (the error occurs in main method int Bairstow(*parameters*), which work on class fields and returns number which indicate is solution was found, is not existing or can't be found in given number of iteration)
Here's the main method:
int solver::Bairstow(int stopien,double const *tabA, double *tabP,double *tabR, double eps, int N, double p_,double r_)
{
int i,q, Iter, indpziel=0;
i=stopien;
while(i>=0 && tabA[i]==0)
{
i--;
}
if (i<2) { return 1; }
double *A=new double[stopien +1]
for (i=0;i<=stopien;i++)
{
A[i]=tabA[i];
}
double *Q=new double[stopien-1 +1];
double *w=new double[stopien-3 +1];
double Reszta[2];
double dqdp[2], dqdr[2];
double p,r, a,b,c,d;
while (stopien>=2)
{
p=p_, r=r_;
Iter=O;
do
{
PodzielWiel(stopien,A,p,r,Q,Reszta);
if (fabs(Reszta[1])<eps && fabs(Reszta[0])<eps)
{
break;
}
PodzielWiel(Stopien-2,Q,p,r,W,dqdr);
for (i=Stopien-2;i>=0;i--)
{
Q[i+1]=Q[i];
}
Q[0]=0;
PodzielWiel(Stopien-1,Q,p,r,W,dqdp);
q=LiczMOdwrotna(dqdp[0],dqdr[0],dqdp[1],dqdr[1],a,b,c,d);
if (q==1)
{
delete[] Q;
delete[] W;
return 2;
}
p = p-(a*Reszta[0]+b*Reszta[1]);
r = r-(c*Reszta[0]+d*Reszta[1]);
} while (++Iter<N);
if (Iter==N) return 3;
tabP[IndDziel]=p;
tabR[IndDziel]=r;
IndDziel++;
Stopien-=2;
for (i=0;i<=Stopien;i++)
{
A[i]=Q[i];
}
}
delete[] Q; delete[] W; //that's the line debugger pointed to
return 0;
}
And two helper methods:
for polynomial division (by trinomial x^2-px-r)
int solver::PodzielWiel(int stopien,double const *tabA, double p,double r, double *Q,double *R)
{
if (Stopien<O) return 1;
int i;
for (i=0;i<=stopien-2;i++)
{
Q[i]=0;
}
while (stopien>=0 && tabA[stopien]==0)
{
stopien--;
}
if (stopien<2)
{
R[0]=tabA[0];
R[1]=tabA[1];
return 0;
}
double *A=new double[Stopien +1];
for (i=0;i<=stopien;i++)
{
A[i]=tabA[i];
}
Q[stopien-2]=A[stopien];
if (stopien>2)
{
for(i=stopien; i>1; i--)
{
Q[i-2]=A[i];
A[i-1]+=Q[i-2]*p;
A[i-2]+=Q[i-2]*r;
}
R[1]=A[1];
R[0]=A[0] ;
}
else
{
R[1]=A[1]+p*Q[0] ;
R[0]=A[0]+r*Q[0] ;
}
delete [] A;
return 0;
}
for inversing the 2x2 matrix(first 4 parameters are inputs and next 4 outputs as it):
int Solver::LiczMOdwrotna(double x,double y,double w,double z, double &a,double &b,double &c,double &d)
{
if (x*z==y*w)
{
return 1;
}
if (x*w!=0)
{
c=1/(y-z*x/w);
a=-z*c/w;
d=1/(z-y*w/x);
b=-y*d/x;
}
else if (y*z!=0)
{
a=1/(x-w*y/z);
c=-w*a/z;
b=1/(w-z*x/y);
d=-x*b/y;
}
else if (x==0 && z==0)
{
c=1/y;
d=0;
a=0;
b=1/w;
}
else if (y==0 && w==0)
{
a=1/x;
b=0;
c=0;
d=1/z;
}
return 0;
}
And sory for maybe poor formatting but I have to use OCR software as copying from QT creator was impossible... even after using show in explorer, save file as txt in new localization (to make it visible outside QT Creator) and then doing it again... I still couldn't copy anything...
I want to build a c++ program that would solve 8-puzzle problem using BFS.
I want to show every generated state.
But the problem is, I don't know how to generate state.
I just want some clean function which will efficiently generate states and there will be a Explored array which will assure that there is no redundant state.
I've explored GitHub but there is too much complex solutions
I've written the following code till now
#include<iostream>
#include<conio.h>
using namespace std;
class puzzle{
private:
int initial[3][3],goal[3][3] = {{1,2,3},{4,5,6},{7,8,0}};
int queue[1000];
string data;
public:
void genratePuzzle();
void showState();
bool check_goal(int initial);
};
void puzzle::genratePuzzle(){
cout<<"\n***Create initial state 0-8***\n";
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
cout<<"Insert at ["<<i<<"]["<<j<<"] : ";
cin>>initial[i][j];
}
}
}
void puzzle::showState(){
cout<<"\n***State***\n";
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
cout<<initial[i][j]<<" ";
}
cout<<endl;
}
}
bool puzzle::check_goal(int initial){
bool check = true;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(initial[i][j] != goal[i][j]){
check = false;
}
}
}
return check;
}
int main(){
puzzle p1;
p1.genratePuzzle();
p1.showState();
getch();
}
Goal state
1 2 3
4 5 6
7 8 0
Put your state into
struct state {
int data[3][3];
bool operator < (const state & other) {
for (int y=0; y<3; ++y) {
for (int x=0; x<3; ++x) {
if (data[y][x] < other.data[y][x]) {
return true;
}
if (data[y][x] > other.data[y][x]) {
return false;
}
}
}
return false; // all were equal
}
}
Now you can use values of type state as keys in a std::map e.g. make a std::map<state, bool> explored if you want. It behaves like an array indexed by states, so:
state a;
// do something to the state...
// and you can do this
explored[a] = true;
How do you generate new states? You start with an existing state and try all valid moves on it. Repeat until done.
#include <iostream>
#include <vector>
#define MAXX 1000
using namespace std;
int number[MAXX], digits=0;
int adjust(int i)
{
if(number[i]<9)
{
cout<<"i = "<<i<<" "; //PROBLEM
(number[i])++;
return i;
}
number[i]=0;
adjust(i-1);
}
void makePalindrome(int head,int tail)
{
int revert;
if(head>tail)
return;
if(number[head]==number[tail])
{
makePalindrome(head+1,tail-1);
}
if(number[tail]<number[head])
{
number[tail]=number[head];
makePalindrome(head+1,tail-1);
}
if (number[tail]>number[head])
{
number[tail]=number[head];
revert=adjust(tail-1);
if(revert<=head)
{
makePalindrome(revert,digits-revert-1);
}
else
{
makePalindrome(head+1,tail-1);
}
}
}
int main(int argc, char const *argv[])
{
long long int num,num_copy;
int head,tail;
int number_reverse[MAXX];
cout<<"Enter the number whose next greater palindrome you want to find" <<endl;
cin>>num;
num_copy=num;
while(num_copy>0)
{
number_reverse[digits]=num_copy%10;
digits++;
num_copy=num_copy/10;
}
//cout<<"Digits = "<<digits<<"\n";
for (int i = digits-1; i >=0; --i)
{
number[digits-i-1]=number_reverse[i];
//cout<<number[digits-i-1]<<" ";
}
head=0; tail=digits-1;
makePalindrome(head,tail);
cout<<"Answer : ";
for (int i = 0; i < digits; ++i)
{
cout<<number[i];
}
cout<<"\n";
return 0;
}
When I am running with an input : 94187978322, it is giving two different answers with and without a "cout" line (line with comment "PROBLEM").
Here's the output:
ishivendra:code shivendraagrawal$ g++ next_palindrome.cpp
ishivendra:code shivendraagrawal$ ./a.out
Enter the number whose next greater palindrome you want to find
94187978322
Answer : 94188078149
ishivendra:code shivendraagrawal$ g++ next_palindrome.cpp
ishivendra:code shivendraagrawal$ ./a.out
Enter the number whose next greater palindrome you want to find
94187978322
i = 7 i = 6 i = 4 Answer : 94188088149
The second output is the desired output. Can you point out the cause of this difference and incorrectness for the first one ?
I figured half of it out, it was a logical error.
I should have written return adjust(i-1).
I still don't know the cause for the peculiarity (There was no compilation or runtime error) but at least my code is running as desired now.
I am trying to sort an array of int and chars (from a class) by descending order. These are student names and grades.
The class is defined as:
class Student {
public:
char name[20];
int grades;
};
numCount is the incremental value of number of records.
void bubble_sort(Student theResults[], int numCount)
{
bool swapped = true;
while(swapped)
{
swapped = false;
for(int i=1;i<numCount;i++)
{
if(theResults[i-1].grades < theResults[i].grades)
{
int tempHold = theResults[i-1].grades;
theResults[i-1].grades = theResults[i].grades;
theResults[i].grades = tempHold;
swapped = true;
}
}
}
The issue I am having is that the int values (grades) are sorted correctly after the loop but having difficulty getting the names to be correctly allocated to match with the grades.
I have used the following code but it doesn't work as it displays the incorrect grades for the students.
char* title_temp = theResults[i-1].name;
theResults[i-1].name[20] = theResults[i].name[20];
theResults[i].name[20] = title_temp[20];
I think your problem is here:
if(theResults[i-1].grades < theResults[i].grades)
{
int tempHold = theResults[i-1].grades;
theResults[i-1].grades = theResults[i].grades;
theResults[i].grades = tempHold;
swapped = true;
}
What you really want to do is
if(theResults[i-1].grades < theResults[i].grades)
{
Student tempHold = theResults[i-1];
theResults[i-1] = theResults[i];
theResults[i] = tempHold;
swapped = true;
}
Before all you were changing was the grade value and not the names, this will switch the entire Student object and should produce the output you are looking for
You'd have to copy the entire char block, each element at a time using a loop, or you could use memcpy.
You could also use a shallow copy of your class
void bubble_sort(Student theResults[], int numCount)
{
bool swapped = true;
while(swapped)
{
swapped = false;
for(int i=1;i<numCount;i++)
{
if(theResults[i-1].grades < theResults[i].grades)
{
Student tempHold = theResults[i-1];
theResults[i-1]= theResults[i];
theResults[i] = tempHold;
swapped = true;
}
}
}
}
The problem is that you need to swap the objects, the grades only have to act as a key to guide the sort, try this :
void bubble_sort(Student theResults[], int numCount)
{
Student tempHold;
bool swapped = true;
while(swapped)
{
swapped = false;
for(int i=1;i<numCount;i++)
{
if(theResults[i-1].grades < theResults[i].grades)
{
tempHold = theResults[i-1]; //swap the objects, not just the grades.
theResults[i-1]= theResults[i];
theResults[i] = tempHold;
swapped = true;
}
}
}}
However, if you must copy members, then in addition to swapping grades :
char temp[20];
strcpy(temp ,theResults[i-1].name);
strcpy(theResults[i-1].name,theResults[i].name);
strcpy(theResults[i].name,temp);
Instead of using
char* title_temp = theResults[i-1].name; // <-wrong
theResults[i-1].name[20] = theResults[i].name[20];//20 is invalid index
theResults[i].name[20] = title_temp[20]; //this is just 1 element out of the whole array
which is wrong due to many reasons.