I have got some impossible queries for you! (or are they? ;) )
You have n binary numbers of length m. The ith binary number is Bi. Also, you have to perform q queries on them. The indexing is zero-based and the indexing of bits starts from left.
The queries are of type : a, i, j.
If a is:
0 : perform Logical AND operation between Bi and Bj and output the number of 1s in the result.
1 : perform Logical OR operation between Bi and Bj and output the number of 1s in the result.
2 : perform Logical XOR operation between Bi and Bj and output the number of 1s in the result.
3 : flip the value of the jth bit of Bi (i.e. set the bit to 0 if it equals 1 and vice-versa).
Note: For queries of type 0, 1, and 2, the binary numbers remain unchanged.
It is also recommended to use Fast I/O for C++ and JAVA programmers.
Input Format:
First line contains Integers n and m.
The next n lines contain binary numbers of length m.
The ith line contains binary number Bi.
The next line contains an integer q
The next q lines contain queries of type : a, i, j.
Output Format:
Output number of 1s in the result of type 0, 1 and 2 queries.
Constraints:
1<=n, m<=2500
1<=q<=10^6
I have tried changing the array size, but still the error remains the same!
#include <iostream>
#include <math.h>
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m;
cin>>n>>m;
char arr[3000][3000];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
cin>>arr[i][j];
}
long int q;
cin>>q;
char query[3000][3000];
for(long int k=0;k<q;k++)
for(long int l=0;l<3;l++)
{
cin>>query[k][l];
}
for(long int i=0;i<q;i++)
{
if(int(query[i][0]-48)==3)
{
if(arr[int(query[i][1])-48][int(query[i][2])-48]=='1')
{
arr[int(query[i][1])-48][int(query[i][2])-48]='0';
}
else
{
arr[int(query[i][1])-48][int(query[i][2])-48]='1';
}
}
else if(int(query[i][0]-48)==2)
{
int cntr=0;
int bi=int(query[i][1])-48;
int bj=int(query[i][2])-48;
for(int i=0;i<m;i++)
{
int xorres=arr[bi][i]^arr[bj][i];
if(xorres==1)
cntr++;
}
cout<<cntr<<endl;
}
else if(int(query[i][0]-48)==1)
{
int cntr=0;
int bi=int(query[i][1])-48;
int bj=int(query[i][2])-48;
for(int i=0;i<m;i++)
{
int andres=arr[bi][i]|arr[bj][i];
if(andres-48==1)
cntr++;
}
cout<<cntr<<endl;
}
else if(int(query[i][0]-48)==0)
{
int cntr=0;
int bi=int(query[i][1])-48;
int bj=int(query[i][2])-48;
for(int i=0;i<m;i++)
{
int andres=arr[bi][i]&arr[bj][i];
if(andres-48==1)
cntr++;
}
cout<<cntr<<endl;
}
}
return 0;
}
The two char[3000][3000]'s that you allocate on the stack is the reason for the crash.
Since there's no upper constraint on n you'd better to try to allocate it on the heap and catch the exception if it fails. This can be done by using std::vector<std::vector<char>> instead.
Replace:
int n,m;
cin >> n >> m;
char arr[3000][3000];
With something like this:
#include <vector>
size_t n, m;
std::vector<std::vector<char>> arr;
while(std::cin >> n >> m) {
try {
arr.resize(n, std::vector<char>(m));
break; // success, break out of the while-loop
} catch(const std::exception& ex) {
// exception caught, most probably a bad_alloc
std::cerr << ex.what() << " ... try again\n";
}
}
As proposed in the comments, you probably don't need to store all the queries. Just deal with one query at a time.
Also, never #include <bits/stdc++.h> yourself. It's a non-standard/non-portable header file that includes a lot more than you need, and often not all you need. Instead, only include the headers you actually need.
Similarly, using namespace std; is considered bad practice.
OK, so I think you have complicated some things here.
The size of the queries is 10^6 and you are declaring the array as query[3000][3000].
Now, I don't think you need to store the queries. Consider this-
cin>>q;
while(q--)
{
cin>>a>>i>>j;
/*Your code here*/
}
The question states that the queries are in the form : a i j
So for example if you want to perform operation 0 on first 2 strings, the query will be:
0 1 2
But you are storing the binary numbers from index 0!
So, your code will perform the operation on second and third query. So, what you need to do is just take subtract 1 from values of i and j.
Related
I have defined an empty vector variable inside the loop for the test case, after execution of each test case I expect the vector will go empty, but it is storing the previous result of the earlier test cases as well please help thanks a lot.
My problem statement is this. which takes input as:
The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer N.
N lines follow. For each valid i, the i-th of these lines contains a string S3,i followed by a space and an integer
C3,i — the problem code and the number of correct solutions
on the i-th problem in the third division.
N more lines follow. For each valid i, the i-th of these lines contains a string S2,I followed by a space and an integer
C2,i — the problem code and the number of correct solutions
on the i-th problem in the second division.
Finally, N more lines follow. For each valid i, the i-th of these lines contains a string S1,i followed by a space and an
integer C1,i — the problem code and the number of correct
solutions on the i-th problem in the first division.
GOAL:
Codechef challenges have three divisions. In one challenge, there are N problems in each division, but some problems may be shared among multiple divisions. Each problem is uniquely identified by a code — a string containing only uppercase English letters. Each participant can only submit in one of the divisions.
Chef wants to find the number of correct solutions, in total among all 3 divisions, for each problem. Given a list of N problem codes with the numbers of correct solutions for each problem in each division, find the total number of correct solutions for each problem and sort them in non-decreasing order.
My code takes input as:
3
1
A 1
B 2
C 3
2
AA 1
AB 1
AB 1
AC 1
AC 1
AD 1
1
Z 100
Z 100
Z 100
The expected output is:
1 2 3
1 1 2 2
300
But my output has errors:
1 2 3
1 1 1 2 2 2 3
1 1 1 2 2 2 3 300
My code for above problem is:
#include <iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
int main() {
map<string,int>m;
int t,n,c_1;
string s_1;
cin>>t;
for(int i=0;i<t;i++){
cin>>n;
for(int k=0;k<3;k++){
for(int j=0;j<n;j++){
cin>>s_1;
cin>>c_1;
m[s_1]+=c_1;
}
}
vector<int>v;
for(auto x :m){
v.push_back(x.second);
}
sort(v.begin(),v.end());
for(auto y: v){
cout<<y<<" ";
}
cout<<"\n" ;
}
return 0;
}
Your vector (v) is being reset (to empty) on each run of the outer for loop but your map (m) isn't. That is keeping its content each time, and your new inputs are being appended to it. Thus, your for (auto x : m) { appends the data from each previous loop to your (initially empty) vector.
Move the declaration of map<string, int>m; to inside that outer loop to fix your problem:
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <string>
using std::cin, std::cout;
using std::string;
using std::map;
using std::vector;
using std::sort;
int main()
{
int t, n, c_1;
string s_1;
cin >> t;
for (int i = 0; i < t; i++) {
map<string, int>m; // Move the map to here, so it's created afresh for each loop.
cin >> n;
for (int k = 0; k < 3; k++) {
for (int j = 0; j < n; j++) {
cin >> s_1;
cin >> c_1;
m[s_1] += c_1;
}
}
vector<int>v;
for (auto x : m) {
v.push_back(x.second);
}
sort(v.begin(), v.end());
for (auto y : v) {
cout << y << " ";
}
cout << "\n";
}
return 0;
}
Alternatively, you can use the same map each time and just reset (clear) it on each run of the outer loop, by adding a m.clear(); as the first line inside that loop.
I need write a program which checks if there are 3 or more matching numbers in an array. My code works fine until there is an array like "2 2 3 3 5 5 4 4 1 1", then it approves that there are 3 or more duplicates in the array which is not true. Maybe someone knows a simple solution which would help me? Or do I need to overwrite my code?
Here is my code:
#include <iostream>
using namespace std;
void funk(int n, int a[], int &kiek);
int main()
{
int n, a[101],kiek=0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
funk(n, a, kiek);
if (kiek > 2) {
cout << "TAIP";
}
else
cout << "NE";
}
void funk(int n, int a[], int &kiek)//funkcijos kūnas
{
int j;
for (int i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (a[i] == a[j])
kiek++;
cout << kiek;
}
}
}
This is the input:
10
2 2 3 3 5 5 4 4 1 1
This is the output I need to get:
NE
The problem that your code has is:
You are comparing any 2 numbers and never reset the counter. So if there is an 1 1, you are increasing the counter. If there is then an 2 2 you are also increasing the counter. And for the final 3 3 you also increase the same counter. Then it is 3. Although there are only 2 same values. This can never work. What you could do is
Read all values
Count the frequency of EACH different value
Check if the frequency of the values
If any of the counts is greater than 2, then show corresponding message
Show count of each value
I will show you the "more-modern" C++ approach and will use C++ algorithms for the below example solution.
First we will get the number of values to work with from the user. We will store the values in a std::vector. And, we use std::copy_n to copy the values from std::cin to our std::vector. For that we will use the std::istream_iterator that will iterate over the elements given by the user. So, we use a simple one-liner to read all values from the user.
Next is the frequency counting. For this we have a standard solution in C++. You will find it in dozens of places in the net. We will use a std::map. The key is the integer that we read into the vector and the value is the counter. With the std::map's index operator [] we add a value to the map, if it is not yet existing. With the ++ we simply do the counting, whether the value was already in the std::map or has just been added. Also this is a very simple one-liner.
Then, we check, if any of the counts is greater than 2. For this we will use the STL algorithm std::any:of with a very simple lambda. With that, we can create your desired result.
Last, but not least, we show all values and their count, if the count is greater than 2. This we do with an ultra simple range based for loop. We extract the values out of the counter-std::map using structered bindings.
Please see:
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <iterator>
int main() {
// Give instructions
std::cout << "How many values do you want do read? Please specify: ";
// Read the number of values to enter
size_t numberOfValues{ 0U }; std::cin >> numberOfValues;
// Read the given number of values from std::cin
std::vector <int> values(numberOfValues);
std::copy_n(std::istream_iterator<int>(std::cin), numberOfValues, values.begin());
// Count each value
std::map<int, size_t> counter{};
std::for_each(values.begin(), values.end(), [&counter](const int& i) { counter[i]++; });
// Check, if any count is bigger than 2
if (std::any_of(counter.begin(), counter.end(), [](const std::pair<int, size_t> & c) { return c.second > 2; }))
std::cout << "TAIP\n";
else
std::cout << "NE\n";
// Look, if there is any number with a count bigger than 2 and show the result
for (const auto& [value, count] : counter)
if (count > 2) std::cout << value << " --> " << count << "\n";
return 0;
}
I hope this gives you an idea on how this could be done . . .
#include <bits/stdc++.h>
using namespace std;
string bin(int n){
string x="";
while(n!=0)
{
int z=n%2;
x+=to_string(z);
n%=2;
}
return x;
}
int main(){
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
string x=bin(a[i]);
int u=x.size();
int cnt=0;
for(int g=0;g<u;g++)
{
if(x[g]=='1')
++cnt;
}
cout<<cnt<<' ';
}
cout<<'\n';
}
}
This code is given several test cases and each test case will have an array of n integers, for each element in the array I should count the number of ones in the binary representation of it. I wrote a function that expects an integer and returns a string containing the binary representation of it. But I wonder why my code does not end, and not allowing me to receive other numbers in array.
For instance, there's one test case and and only array of 2 integers if I inputted 1 and wait for ever to enter the second number, what's happening?
This is your bin function reduced to the bare minimum:
string bin(int n){
while(n!=0)
{
n%=2;
}
return {};
}
If n is even you will set it to 0 on the first iteration, otherwise you set it to 1 and never change it afterwards (1%2==1). Hence you have a endless loop. I won't spoil you the "fun" of completing the exercise, so I will just point you to using a debugger. If you step trough your code line by line you could have observed how n never changes and why the loop wont stop.
PS: (spoiler-alert) you might want to take a look at std::bitset (end of spoiler)
#include <iostream>
using namespace std;
/*
*
*/
int main() {
int k, in[k],reversea[k],i,m,n;
cin>>k;
for (i=0;i<k;i++){
cin>>in[i];
}
for (m=k-1;m>=0;m--){
for (n=0;n<k;n++){
in[m]=reversea[n];
}
}
for(i=0;i<k;i++){
cout<<reversea[i];
}
return 0;
}
I have no idea why it says segmentation fault even before i start debugging it. I compile another one on calculating the frequency of 1, 5, and 10 in an array of k numbers, and it says the same thing...
Here is the other one:
#include <iostream>
using namespace std;
int main() {
int k,i,m,n,count5,count1,count10;
int input[k];
cin>>k;
for (i=0;i<k;i++){
cin>>input[i];
}//input all the numbers
for(i=0;i<k;i++){
if (input[i]=1){
count1++;
}
if (input[i]=5){
count5++;
}
if (input[i]=10){
count10++;
}
}
cout<<count1<<"\n"<<count5<<"\n"<<count10<<"\n";
return 0;
}
Please help me. Thanks.
On this line
int k, in[k],reversea[k]
How are you supposed to initialize an array with k elements if k isn't initialized? The size of an array must be known at compile time not run time. If k isn't know until run time, use a std::vector
int k;
std::cin >> k;
std::vector<int> in(k);
std::vector<int> reversea(k);
Both your programs have two major faults.
You need to know the size of an array while creating it. In your code, k is still uninitialized and you are using this value as the size of your array. Instead, change it to
int k,i,m,n;
cin >> k;
int in[k];
int reversea[k];
While reversing the array, you should be filling reversea using values from in, and not the other way round. Also, you don't need 2 for loops, just use 1 for loop.
for (m=k-1; m>=0; m--){
reversea[m] = in[k-1-m];
}
In the second program, you again need to get the value of k before creating the array input[k].
You are testing for equality with a = instead of == . Change your code from
if (input[i]=1){
to
if (input[i] == 1) {
So here is task.
I tried use this code:
#include <iostream>
#include <istream>
#include <conio.h>
#include <math.h>
using namespace std;
int main(){
int i, n;
cout<<"enter n ";
cin>>n;
if(n==1||n==2){
i=1;
cout<<"\n your number "<<i;
}
else {
for(i=3;i<=n;i++){
i=(i-1)-4*(i-2);
}
cout<<"\n your number"<<i;
}
getch();
}
When enter 1 or 2, I get: "your number 1". But when I enter 3, 5, or 9, I get: "your number 11"
Where is mistake?
And I can't use massive for this task,
because size of massive must be constant number. Not variable
n. Am I right?
Sorry for my english.
I thing that you've got -3 votes because you want us to find the mistake in your code.
As for your Question, you can use recursion:
int f(int x)
{
if(x==1||x==2)
return 1;
else
return =f(x-1)-4*(x-2);
}
int main(){
int x;
cin>>x;
cout<<f(x);
}
I recommend you always to use recursion in sequence when every term but some of the first terms depends on the previous terms.
Recursion will call the function for all terms and add the results to a stack on the memory. it's slower than your way; specially if you want to find the answer for more than one term. but here I thing it's more proper.
If you thought that int would be too small for the result, you could use long long instead.
The way you tried to solve your task is incorrect. You shold keep only 2 last calculated elements in memory, let x(i-1) is a and x(i-2) is b. then you calculate your next value, which well be x(i - 1) on next iteration + move previous x(i-1) to x(i-2) - so your algorithms uses only constant amount of memory. Here is the code:
int calc(int n) {
int a = 1, b = 1;
for(n -= 2; n > 0; --n) {
int tmp = a - 4 * b;
b = a;
a = tmp;
}
return a;
}