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
Related
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
I am a novice to CP and while I was doing this problem of finding the second maximum among three numbers I wrote this code which however works for int but doesn't work for long even though they are the same data types.
Input Format:
The first line contains the number of triples, N.
The next N lines which follow each have three space separated integers.
Input Used:
3
1 2 3
10 15 5
100 999 500
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >>n ;
while(n--){
long a,b,c,d;//if i change long to int output is correct
scanf("%i%i%i",&a,&b,&c);
d=min(max(a,b),max(a,c));
printf("%i\n",d);// outputs 1 \n 10 \n 100 \n(\n means new line)
}
return 0;
}
Perhaps because you're using the wrong specifier. %i is for ints, but %li is for longs.
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
The link to the problem is "https://www.codechef.com/OCT17/problems/PERFCONT"
I have worked out a solution to this problem but I am getting wrong answer.
My solution:
#include<iostream>
using namespace std;
int main(){
long long int hard,cakewalk,t,temp;
long long int n,p;
cin>>t;
while(t--){
hard = cakewalk = 0;
cin>>n;
cin>>p;
while(n--){
cin>>temp;
if(temp<=(p/10))
hard++;
if(temp>=(p/2))
cakewalk++;
if(hard>2 || cakewalk>1){
break;
}
}
if(hard==2 && cakewalk ==1){
cout<<"yes"<<endl;
}
else{
cout<<"no"<<endl;
}
}
return 0;
}
As I have gathered we have to calculate the number of Hard and Cakewalk type problems and if there exactly 2 and 1 respectively, it's a balanced contest.
Kindly help me in solving this.
You are getting WA because you are breaking when hard > 2 || cakewalk > 1 without reading the whole input line. So, after it breaks and gives the correct input for the given test case, but it will fail because the number that is next inputted is technically not the input for the next test case. It is the remaining part of first test case.
For input:
2
4 100
1 1 1 1
2 100
1 50
For the first test case, you skip taking input after reading 1 1 1 from line 3. So, the next input you take is 1 (considering it to be n) whereas n should be 2 for next test case.
Removing this should work:
if(hard>2 || cakewalk>1){
break;
}
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