create string r using string p and q, C++, Data Structures, Strings
In this question we have given 3 strings and we nee dto count possible number of ways to create string r using string p and q
1 answer
-
answered 2022-05-07 04:56
greenedge2022
Welcome to stackoverflow, we like to help, but might not make your homework for you :)
do you know?
how many words do you know
See also questions close to this topic
-
C++ increment with macro
I have the following code and I want to use increment with macro:
#include <iostream> #define ABS(x) ((x) < 0 ? -(x) : (x)) int main(int argc, char** argv) { int x = 5; const int result = ABS(x++); std::cout << "R: " << result << std::endl; std::cout << "X: " << x << std::endl; return EXIT_SUCCESS; }
But output will be incorrect:
R: 6 X: 7
Is it possible to somehow use macros with an increment, or should this be abandoned altogether?
-
Can anyone pls tell me whats wrong with my code ? im stuck for the last 3 hours ,this question is bipartite graph in c++
idk why im getting error ,can someone help ? im trying to prove if a graph is bipartite or not in c++
bool isBipartite(vector<int> graph[],int V) { vector<int> vis(V,0); vector<int> color(V,-1); color[0]=1; queue <int> q; q.push(0); while (!q.empty()) { int temp = q.front(); q.pop(); for (int i=0;i<V;i++) { if (!vis[i] && color[i] == -1) "if there is an edge, and colour is not assigned" { color[i] = 1 - color[temp]; q.push(i); vis[i]=1; } else if (!vis[i] && color[i] == color[temp] "if there is an edge and both vertices have same colours" { vis[i]=1; return 0; // graph is not bipartite } } } return 1; }
it gives output "no" for whatever i enter
-
How to assign two or more values to a QMap Variable in Qt
I am getting confused of how to store the values assigned from 3 different functions and storing them in a single map variable
QMap<QString,QString> TrainMap = nullptr; if(......) ( TrainMap = PrevDayTrainMap(); TrainMap = NextDayTrainMap(); TrainMap = CurrentDayTrainMap(); }
The PrevDayTrainMap,NextDayTrainMap & CurrentDayTrainMap returns a set of values with Date and the TrainIdName.I need to store all the values from prevday,currentday and nextday in the TrainMap but it stores only the currentday values to the TrainMap as it is assigned at the last.I am not sure what to do so that it doesn't overwrite.If I should merge what is the way to do it?
-
Reverse words in a string in C++
Can anyone please tell that why the below written code isnt working , theres no error but its simply printing the string which was passed and is not reversing it.
Logic: First reverse individual words and after reversing all the words in a string simply reverse the entire string.
I/P : hello world O/P : world hello
Below is the code:
#include <bits/stdc++.h> using namespace std; void reverse(string str,int low, int high){ while(low<=high){ swap(str[low],str[high]); low++; high--; } } void reverseWords(string str){ int start=0; int n = str.length(); for(int end=0;end<n;end++){ if(str[end]==' '){ reverse(str,start,end-1); start=end+1; } } reverse(str,start,n-1); reverse(str,0,n-1); cout<<str; } int main() { string s = "Welcome to Gfg"; cout<<"After reversing words in the string:"<<endl; reverseWords(s); return 0; }
-
Modify user input to use in exec call
I am trying to modify a user input string to call execvp in a future function. In my code I have a function which tokenises the user input such that if the user entered in: command a b c. It outputs
char** output = { "command", "a", "b", "c", NULL }
The length of the user input is unknown. My issue is that I want to modify the variable char** output to remove the user entered command, such that I getchar** output = {"a", "b", "c", NULL }
My current attempt at this is in the code below:char** exec_arg(char** output, int numTokens){ 143 char* argExec[numTokens]; 147 for(int k = 0; k < (numTokens-1); k++){ 148 argExec[k] = output[k+1]; 149 printf("%s", argExec[k]); 150 } 152 return argExec; 155 }
However I am finding that when I run this I am getting the output: {"a", "b", "c"}
-
suggestions on fulltext search or already existing search algorithms
Can someone suggest how to solve the below seach problem easily, I mean is there any algorithm, or full text search will be suffice for this?
There is below classification of items data,
ItemCategory ItemCluster ItemSubCluster SubCluster Items Vegetable Root vegetables Root WithOutSkin potato, sweet potato, yam Vegetable Root vegetables Root WithSkin onion, garlic, shallot Vegetable Greens Leafy green Leaf lettuce, spinach, silverbeet Vegetable Greens Cruciferous Flower cabbage, cauliflower, Brussels sprouts, broccoli Vegetable Greens Edible plant stem Stem celery, asparagus
The inputs will be some thing like,
sweet potato, yam Yam, Potato garlik, onion lettuce, spinach, silverbeet lettuce, silverbeet lettuce, silverbeet, spinach
From the input, I want to get the mapping of the input items those belongs to which ItemCategory, ItemCluster, ItemSubCluster, SubCluster.
Any help will be much appreciated.
-
Writing multithreaded code to generate incremental index
I want to write a code in following scenario : we want to design an invoice generator.this invoice generator also generates an incremental invoice no .Also note that for some reasons we dont use something like sql Auto Increment here and we want to use C# (I wrote the increment logic). since several users can send requests at a time it means that we dont have to give the same invoice no to them (I used lock for it and I wrote it).
Now i have written increment logic. and i take care of multiple users requests with lock . and I wrote CRUD for mongodb database.
My problem is that my increent logic is naive. each time i find maximum and i increment by one which is not scalable. and another problem is that when we delete a record we don't want the invoice no to be repeated and since we find max if the deleted record had the max value then the invoice no would be duplicate and this is something we don't want to have.
I really don't know how to handle the problem. specially in case of deletion should i store max values in a file or should i change increment logic completely?
i appreciate any help
-
Which is the best way to represent my data
I have a following data structure:
Page0 Key0 Text Color Name Key1 Text Color Name Page1 Key0 Text Color Name Key1 Text Color Name
Which is the best data structure to use ?
-
looping through an array of objects that contains three key, value pairs
I have these objects, which could be in an array, like in the example or a database:
[ { "payer": "DANNON", "points": 1000, "timestamp": "2020-11-02T14:00:00Z" } { "payer": "UNILEVER", "points": 200, "timestamp": "2020-10-31T11:00:00Z" } { "payer": "DANNON", "points": -200, "timestamp": "2020-10-31T15:00:00Z" } { "payer": "MILLER COORS", "points": 10000, "timestamp": "2020-11-01T14:00:00Z" } { "payer": "DANNON", "points": 300, "timestamp": "2020-10-31T10:00:00Z" } ]
I want to subtract a total of
{ "points": 5000 }
as an example from the points values of these objects starting with the oldest points based on timestamp, keep in mind no payer's points to go negative. then I want to return how much each payer's points were used. The expected result looks like this:[ { "payer": "DANNON", "points": -100 }, { "payer": "UNILEVER", "points": -200 }, { "payer": "MILLER COORS", "points": -4,700 } ]
My initial solution was:
- To sort the array based on the oldest timestamp
- Create a new object to store the number of points used from each payer
- Looping through the sorted array and subtracting the points from each payer and storing the number of points used in that new object.
- push all new objects to an array and return it. This solution made sense to me but I couldn't implement it, I kept on having unexpected results until my brain froze and I can't think anymore.
This is an apprenticeship interview question. I thought it was easy at the beginning, but then after spending hours without solving it, I'm thinking am I an imposter? or is this question a tricky one?
Thank you for sharing your knowledge and experience and helping me out. I appreciate it!