I am unable to understand the output to my c++ code - c++

I was doing a problem the part of the problem is to shift the a given string of 0 and 1 of a given number n to a given amount (here sft variable taken). T queries. I was getting error in right shift while left shift had no problem. The whole code is below -
#include<iostream>
#include<bitset>
using namespace std;
int main()
{
const int m=16;
int n,t;
cin>>t;
int sft;
char ch;
int arr[m];
while(t--)
{
cin>>n;
cin>>sft;
cin>>ch;
bitset<m>bt(n);
cout<<bt<<endl;
if(ch=='R')
{
for(int i=0;i<m;i++)
{
arr[i]=bt[((i+sft)%m)]; // problem is here
// cout<<((i+sft)%m)<<"-"<<bt[((i+sft)%m)]<<" "; // to check what is happening
}
}}}
PROBLEM - The problem is that for a given position in bt string , I am not getting what I am supposed to get it is giving wrong bit I do not know why?
input :
1(queries)
16(number) 3 (sft) R(right)
Output
bt string = 0000000000010000
Position-Bit in bt = 3-0 4-1 5-0 6-0 7-0 8-0 9-0 10-0 11-0 12-0 13-0 14-0 15-0 0-0 1-0 2-0

The least significant bit is 0, so that should be on the right side, so your output should be:
2-0 1-0 0-0 15-0 ... 5-0 4-1 3-0
or 0000000000000010 (2) which is the right shift for 3 positions of 0000000000010000 (16)
So your processing is okay for circular right shifting (rolling), but your output is confusing.
For non-circular (logical) shifting, introduce 0 for invalid positions.
See also: https://en.wikipedia.org/wiki/Bitwise_operation#Bit_shifts

Related

Solution of : SPOJ : DSUBSEQ

My code is giving wrong answer for the following question. I have tried many test cases but can't find the error.
http://www.spoj.com/problems/DSUBSEQ/
# include<bits/stdc++.h>
# define lli long long int
# define pb push_back
# define loop(i,a,b) for(int i=a;i<b;i++)
# define loopl(i,a,b) for(lli i=a;i<b;i++)
# define MAXN 1000
#define INF 1000000000
# define mod 1000000007
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
int n=s.length();
int visited[26],dp[n+1];
memset(visited,-1,26);
loop(i,0,26) visited[i]=-1;
dp[0]=1;
loop(i,1,n+1)
{
dp[i]=2*dp[i-1];
if(visited[s[i-1]-'A']!=-1) dp[i]=(dp[i]%mod-dp[visited[s[i-1]-'A']]%mod + mod)%mod ;
visited[s[i-1]-'A'] = i-1 ;
}
cout<<dp[n]<<endl;
}
}
Sample test Cases:
INPUT:
3
AAA
ABCDEFG
CODECRAFT
OUTPUT:
4
128
496
But i am getting : wrong answer #1
This language is c++ . I am new to dynamic programming.
The input contains lower test cases also.
It will be a good thing to convert every character to upper case
Update: Another thing you are doing wrong is not taking mod in this line dp[i]=2*dp[i-1]
Imagine a test where there are distinct 26 letters in that case ans will 2^26 will certainly overflow
Make it dp[i]=(2*dp[i-1])%mod

Logic error in writing the number twice in a row (C++)

I have been trying to implement something in C++ but apparently, there's a syntax error.
The following code yields "1 3100" when 31 is entered as input :
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
long long n; cin>>n;
long long j = floor((log10(n)));
long long nn = (n*((long long)pow(10,j+1)))+n;
cout<<j<<" "<<nn;
}
The following code yields "1 3130" for the same input, i.e, 31 :
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
long long n; cin>>n;
long long j = floor((log10(n)));
long long nn = (n*(pow(10,j+1)))+n;
cout<<j<<" "<<nn;
}
And I wished to produced "1 3131" for the input 31. Basically, I am trying to write the number twice in a row: the same thing that you get when you parse the number into string and add the same string twice (like, n=11, parse into s = "11" and then yield s+s).
So I want to multiply the input by a suitable power of ten to get enough "trailing zeros" and then add the input again.
Where am I going wrong? Also, why is there a difference between the two codes above? (Please explain why the first code gives that as an output and the second code that as an output and also help me with a newer code to get the desired output).
There is no syntax error, otherwise your code would not end up in an executeable to run.
The explanation for the unexpected output of "3130" is a misuse of a floating point function in an integer context.
long long n; cin>>n; // n becomes 31
long long j = floor((log10(n))); // j becomes 1
long long nn = (n*(pow(10,j+1)))+n; // the result from pow is a floating point just below 100
// integer-multiplied by 31 gives 3099
// adding 31 results in 3130
cout<<j<<" "<<nn; // output 3130

Loop terminating prematurely for multiple queries

For this problem I need to to do multiple queries so I used a for loop for it
#include<bits/stdc++.h>
using namespace std;
int main(){
vector<int>ans; //vector to store the ans
string s,e; //real dna and virus dna
cin>>s;
int q,start,match=0; // no. of queries, starting value of the query, var to store matching occurence
unsigned int step=0; //var to keep count of virus dna iteration
cin>>q;
for(int i=0;i<q;i++){//loop to iterate q times
cin>>start;
int l,r,x,c;
if(start==2){ // for 2nd type query
cin>>l>>r>>e;
for(int i=l-1;i<=r-1;i++){
if(s[i]==e[step]){
match+=1;
}
step+=1;
if(step== e.length()){
step=0; //starting again from start of virus
}
}
}
ans.push_back(match);
match=0; //reintializing value for next query
if(start==1){ //for 1st type query
cin>>x>>c;
s[x-1]=c; //replacing char at x-1 with c
}
}
for(int j=0;j<ans.size();j++){ //loop for ans output
cout<<ans[j]<<endl;
}
return 0;
}
but it terminates before it should for ex: for this input,
ATGCATGC
4
2 1 8 ATGC
2 2 6 TTT
1 4 T
2 2 6 TA
It would stop at 5th line and print 8 ,2 ,0, 0 whereas it should be 8, 2, 4. If I do individual queries without loop things work fine but any kind of loop doesn't work. Pls help. Also any suggestion for solving this kind of problems more efficiently will be very helpful to me.
The bigger problem in your code is that the variable c is defined as int
int l,r,x,c;
when receive chars and not integers (T, in your example) so should be defined as char
int l,r,x;
char c;
If c is int, when you send 4 T to
cin>>x>>c;
x receive 4, c doesn't receive T (T isn't a valid int) so start the next iteration of the external cycle, is called
cin>>start;
when T remain in the buffer; start is integer so there is the same problem and the program end.
And, as pointed by soon, ans.push_back() should be inside the first if. Now a value (zero) is added also with rows starting with 1.

SPOJ LASTDIG, getting TLE

Below is my code for the problem. I am getting TLE. Can anyone tell me how to fix it.
Here is the problem statement:
Nestor was doing the work of his math class about three days but he is tired of make operations a lot and he should deliver his task tomorrow. His math’s teacher gives two numbers a and b. The problem consist in find the last digit of the potency of base a and index b. Help Nestor with his problem. You are given two integer numbers: the base a (0 <= a <= 20) and the index b (0 <= b <= 2,147,483,000), a and b both are not 0. You have to find the last digit of a^b.
Input
The first line of input contains an integer t, the number of test cases (t <= 30). t test cases follow. For each test case will appear a and b separated by space.
Output
For each test case output an integer per line representing the result.
Example
Input:
2
3 10
6 2
Output:
9
6
Here is my code
#include <iostream>
using namespace std;
int main() {
int t;
scanf("%d",&t);
int num;
unsigned int pow;
while(t--)
{
scanf("%d",&num);
scanf("%d",&pow);
int z=1;
if(num==0&&pow==0)
printf("1");
else
{
while(pow!=0)
{
z=z*num;
z=z%10;
pow--;
}
printf("%d\n",z);
}
}
return 0;
}
The value for b is too high, a simple approach such as yours is bound to give a TLE. You can solve this problem using Modular Exponentiation.
http://en.wikipedia.org/wiki/Modular_exponentiation

Undesired results from calculating powers of 2 [C++]

Okay, I'm done banging my head against my desk. I'm trying to compute huge powers of 2 [beyond what's capable of being held in the uint64_t data type] by holding digits in a vector of 'char's. Here is my program, followed by my actual outputs:
/*
This program doubles a very large number by using a vector of char types
Usage: program.exe [number]
Output will be 2^[number]
*/
#include <iostream>
#include <vector>
#include <stdlib.h>
using namespace std;
int main(int argc, char *argv[])
{
vector<char> BigNum;
BigNum.push_back('2');
int carry=0, digit;
int power=atoi(argv[1]);
power-=1;
for(int x=0;x<power;x++) //Example: going from 16 to 32. x==4
{
for(int y=BigNum.size()-1;y>=0;y--) //Go from BigNum[1] to BigNum[0] ('6' then '1')
{
digit=atoi(&BigNum[y]); //digit = 6, then digit=1
BigNum[y]=(char)(((digit*2+carry)%10)+48); //BigNum[1]=(char)(6*2+0)%10+48 = '2' in char
//BigNum[0]=(char)(1*2+1)%10+48 = '3' in char
carry=digit*2/10; //carry=1, then 0
}
if(carry==1) //does not execute. BigNum=={'3','2'}
{
BigNum.push_back('0');
for(int y=BigNum.size()-1;y>0;y--)
{
BigNum[y]=BigNum[y-1];
}
BigNum[0]='1';
carry=0;
}
}
for(int x=0;x<BigNum.size();x++) cout<<BigNum[x];
}
Compiled with:
g++ program.cpp -o program
So here are my results when I run the program:
C:\MyApps\program 2
4
C:\MyApps\program 3
8
C:\MyApps\program 4
16
Okay, looks good so far... even my "if(carry==1)" section, where I push a number to the FRONT of the vector works, since we "carried the 1" to get into double digits. Let's continue:
C:\MyApps\program 5
52
What?
C:\MyApps\program 6
26
What what?
C:\MyApps\program 654
84
C:\MyApps\program 654444
00
It never gets to triple digits... and what the heck is going on?
You're applying atoi to something that isn't a null-terminated string. In practice, it may well look in memory like a null-terminated string, but not the one you actually want it to look like.
The cleanest way to fix this is probably to store actual digit values 0..9 rather than ASCII '0'..'9' in your vector. You'll find that the code is nicer that way too.