Solution failed on pretest 2 - c++11

I'm new to Codeforces and have attempted for the first time today.
Following is the solution that I have found while solving the first question.
Here is my solution to the above question:
#include<bits/stdc++.h>
using namespace std;
int main(){
int no_of_times;
cin>>no_of_times;
for(int l = 0;l < no_of_times;l++){
int n,k;
cin>>n>>k;
if(n == 1 && k == 1){
cout<<1<<"\n";
}
else if(n == 1 && n > k){
cout<<1<<"\n";
}
else if(n > k){
if(n%2 == 0 && k%2 == 0){
cout<<0<<"\n";
}
else if(n%2 == 0 || k%2 == 0){
cout<<1<<"\n";
}
else{
cout<<0<<"\n";
}
}
else{
cout<<(k-n)<<"\n";
}
}
}
I've passed pre-test 1, but failed on pretest 2.
May I know where was I wrong?

Related

Why my dfs code is not working in word search problem?

**this solution is always return 0 My idea is I traverse dfs in four sides and check the condition **```
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
bool dfs(int i,int j,int idx,vector<vector> & board, string & word,vector<vector> & vis
,int del_row[],int del_col[]){
if(idx == word.size()){
return true;
}
vis[i][j]=1;
int n = board.size();
int m = board[0].size();
for(int k=0;k< 4 ;k++){
int row = i + del_row[k];
int col = j + del_col[k];
if( row >= 0 && col >= 0 && row < n && col < m && vis[i][j]== 0 &&
board[row][col] == word[idx]){
if(dfs(row,col,idx+1,board,word,vis,del_row,del_col)){
return true;
}
}
}
vis[i][j] =0;
return false;
}
bool isWordExist(vector<vector<char>>& board, string word) {
// Code here
int n = board.size();
int m = board[0].size();
vector<vector<int>> vis(n,vector<int>(m,0));
int del_row [] = {-1,+1,0,0};
int del_col [] = {0,0,-1,+1};
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(board[i][j] == word[0] && !vis[i][j]){
if(dfs(i,j,1,board,word,vis,del_row,del_col)){
return true;
}
}
}
}
return false;
}
};
this code is always return 0 .

Hand-trace the following function for n=8020 (Data Structure)

can you help me about this question.
Hand trace the following function for n=8020.
int zeros(int n){
if(n==0) // 1 digit (zero/non-zero):
return 1; // bottom out.
else if(n < 10 && n > -10)
return 0;
else // > 1 digits: recursion
return zeros(n/10) + zeros(n%10); }

Find text path through character matrix with recursive algorithm

I'm trying to solve this question: http://www.spoj.com/problems/ALLIZWEL/
Find whether there is a path in the given matrix which makes the
sentence “ALL IZZ WELL”.
There is a path from any cell to all its neighbouring cells.
A neighbour may share an edge or a corner.
Input Specification:
The first line consists of an integer t representing the number of test cases.
The first line of each test
case consists of two integers R and C representing the number of rows and number of columns in the matrix.
Output Specification:
For each test case print “YES” if there is a path which makes the sentence “ALLIZZWELL”.
Else print “NO”.
For sample test cases, open the link.
My code:
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <utility>
#include <algorithm>
#include <stack>
#include <queue>
#include <climits>
#include <set>
using namespace std;
char matrix[101][101];
bool var;
int r,c;
bool check (string str,int pos, bool visited[101][101],int i, int j);
int main (void)
{
int t,i,j;
cin>>t;
bool ans;
while (t != 0)
{
int r,c,flag=0;
cin>>r>>c;
for ( i = 0; i < r; i++ )
{
for ( j = 0; j < c; j++ )
{
cin>>matrix[i][j];
}
}
string str = "ALLIZZWELL";
int pos = 1;
for ( i = 0; i < r; i++ )
{
for ( j = 0; j < c; j++ )
{
bool visited[101][101];
for ( i = 0; i < 101; i++ )
for ( j = 0; j < 101; j++ )
visited[i][j] = false;
visited[i][j] = true;
if (matrix[i][j] == 'A') // for all possible starting positions
ans = check(str,pos,visited,i,j);
if (ans == true)
{
cout<<"YES\n";
flag = 1;
break;
}
if (flag == 1)
break;
}
}
if (flag == 0)
cout<<"NO\n";
t--;
}
return 0;
}
bool check (string str,int pos, bool visited[101][101],int i, int j) // checking for all possible test cases
{
bool result = false;
if (pos == str.length() + 1)
return true;
if (i+1 < r && visited[i+1][j] != true && matrix[i+1][j] == str[pos])
{
visited[i+1][j] = true;
result = result || check(str,pos+1,visited,i+1,j);
if (result == false)
visited[i+1][j] = false;
}
else if (i-1 >= 0 && visited[i-1][j] != true && matrix[i-1][j] == str[pos])
{
visited[i-1][j] = true;
result = result || check(str,pos+1,visited,i-1,j);
if (result == false)
visited[i-1][j] = true;
}
else if (j+1 < c && visited[i][j+1] != true && matrix[i][j+1] == str[pos])
{
visited[i][j+1] = true;
result = result || check(str,pos+1,visited,i,j+1);
if (result == false)
visited[i][j+1] = true;
}
else if (j-1 >= 0 && visited[i][j-1] != true && matrix[i][j-1] == str[pos])
{
visited[i][j-1] = true;
result = result || check(str,pos+1,visited,i,j-1);
if (result == false)
visited[i][j-1] = true;
}
else if (i+1 < r && j+1 < c && visited[i+1][j+1] != true && matrix[i+1][j+1] == str[pos])
{
visited[i+1][j+1] = true;
result = result || check(str,pos+1,visited,i+1,j+1);
if (result == false)
visited[i+1][j+1] = true;
}
else if (i+1 < r && j-1 >= 0 && visited[i+1][j-1] != true && matrix[i+1][j-1] == str[pos])
{
visited[i+1][j-1] = true;
result = result || check(str,pos+1,visited,i+1,j-1);
if (result == false)
visited[i+1][j-1] = true;
}
else if (i-1 >= 0 && j+1 < c && visited[i-1][j+1] != true && matrix[i-1][j+1] == str[pos])
{
visited[i-1][j+1] = true;
result = result || check(str,pos+1,visited,i-1,j+1);
if (result == false)
visited[i-1][j+1] = true;
}
else if (i-1 >= 0 && j-1 >= 0 && visited[i-1][j-1]!= true && matrix[i-1][j-1] == str[pos])
{
visited[i-1][j-1] = true;
result = result || check(str,pos+1,visited,i-1,j-1);
if (result == false)
visited[i-1][j-1] = true;
}
return false;
}
The code is quite self-explanatory: I am trying all possible cases.
I am getting a WA in the third test case, i.e.
2 9
A.L.Z.E..
.L.I.W.L.
I tried debugging but I couldn't narrow down my problem.
The main problems are:
Using i and j in the loop clearing visited (as well as the outer loops)
Using r and c as global variables in check, but writing them as local variables in main
Always returning false from check (instead of result)
Only trying the first choice in check (turn "else if" into "if")
Not clearing the value in ans
Only breaking out of the inner loop, not both the i and j loop
Terminating the search when pos gets to str.length()+1 instead of str.length()
It often helps to put some print statements in recursive functions like these, try them out on a simple example, and see whether the sequence of calls matches your expectations.

Codechef PERMUT2 solution

Following website has the problem.
http://www.codechef.com/problems/PERMUT2
I have been trying to code solution for PERMUT2. My below solution is failing on some test cases. Kindly help me uncover flaws in the below code.
#include <stdio.h>
int a[100000];
int main()
{
int i, j, n, ret;
while(1)
{
scanf("%d", &n);
if(n == 0)
break;
ret = 0;
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n; i++)
if(a[i] != i + 1)
ret++;
if(ret % 2 == 0)
printf("ambiguous\n");
else
printf("not ambiguous\n");
}
return 0;
}
You are not checking the right property. if(a[i] != i + 1) ret++; is not the right checking.
You want to check a[a[i] - 1] == i + 1 for all elements on the array:
bool ambiguous = true;
for(i = 0; i < n; i++) {
if (a[a[i] - 1] != i + 1) {
ambiguous = false;
break;
}
}
if(ambiguous)
printf("ambiguous\n");
else
printf("not ambiguous\n");

Dynamic Programming Altogorithm

I'm trying to construct an algorithm that runs at O(nb) time with the following input/question:
input: an array A[1..n] of n different integers and an integer b (i am assuming that the numbers in A are sequential, starting at 1 ending at n, i.e. for n=4 A[1,2,3,4].
question: in how many ways can b be written as the sum of elements of the array when elements in A[] can only be used once?
I've kind of hit a wall on this one. I'm looking for some kind of recursive solution, but I don't see how to avoid using repeat numbers. Like, for instance, if we started at 1 and stored all the ways to make one (just 1) then 2 (just 2) then three (3 or 2+1) etc, it shouldn't be hard to see how many ways we can make larger numbers. But if, for instance, we take 5, we will see that it can be broken into 4+1, and 4 can be further broken down into 3+1, so then we would see 2 solutions (4+1, and 3+1+1), but one of those has a repeat of a number. Am I missing something obvious? Thanks so much!
Recursive and dynamic solutions in C:
#include <stddef.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
typedef unsigned char uchar;
typedef unsigned int uint;
typedef struct tAddend
{
struct tAddend* pPrev;
uint Value;
} tAddend;
void findRecursiveSolution(uint n, uint maxAddend, tAddend* pPrevAddend)
{
uint i;
for (i = maxAddend; ; i--)
{
if (n == 0)
{
while (pPrevAddend != NULL)
{
printf("+%u", pPrevAddend->Value);
pPrevAddend = pPrevAddend->pPrev;
}
printf("\n");
return;
}
if (n >= i && i > 0)
{
tAddend a;
a.pPrev = pPrevAddend;
a.Value = i;
findRecursiveSolution(n - i, i - 1, &a);
}
if (i <= 1)
{
break;
}
}
}
void printDynamicSolution(uchar** pTable, uint n, uint idx, uint sum, tAddend* pPrevAddend)
{
uchar el = pTable[idx][sum];
assert((el != 0) && (el != 5) && (el != 7));
if (el & 2) // 2,3,6 - other(s)
{
printDynamicSolution(pTable,
n,
idx - 1,
sum,
pPrevAddend);
}
if (el & 4) // self + other(s)
{
tAddend a;
a.pPrev = pPrevAddend;
a.Value = idx + 1;
printDynamicSolution(pTable,
n,
idx - 1,
sum - (idx + 1),
&a);
}
if (el & 1) // self, found a solution
{
tAddend a;
a.pPrev = pPrevAddend;
a.Value = idx + 1;
pPrevAddend = &a;
while (pPrevAddend != NULL)
{
printf("+%u", pPrevAddend->Value);
pPrevAddend = pPrevAddend->pPrev;
}
printf("\n");
}
}
void findDynamicSolution(uint n)
{
uchar** table;
uint i, j;
if (n == 0)
{
return;
}
// Allocate the DP table
table = malloc(sizeof(uchar*) * n);
if (table == NULL)
{
printf("not enough memory\n");
return;
}
for (i = 0; i < n; i++)
{
table[i] = malloc(n + 1);
if (table[i] == NULL)
{
while (i > 0)
{
free(table[--i]);
}
free(table);
printf("not enough memory\n");
return;
}
}
// Fill in the DP table
for (i = 0; i < n; i++)
{
for (j = 0; j <= n; j++)
{
if (i == 0)
{
table[i][j] = (i + 1 == j); // self
}
else
{
table[i][j] = (i + 1 == j) + // self
2 * (table[i - 1][j] != 0) + // other(s)
4 * ((j >= i + 1) && (table[i - 1][j - (i + 1)] != 0)); // self + other(s)
}
}
}
printDynamicSolution(table, n, n - 1, n, NULL);
for (i = 0; i < n; i++)
{
free(table[i]);
}
free(table);
}
int main(int argc, char** argv)
{
uint n;
if (argc != 2 || sscanf(argv[1], "%u", &n) != 1)
{
n = 10;
}
printf("Recursive Solution:\n");
findRecursiveSolution(n, n, NULL);
printf("\nDynamic Solution:\n");
findDynamicSolution(n);
return 0;
}
Output:
for 10:
Recursive Solution:
+10
+1+9
+2+8
+3+7
+1+2+7
+4+6
+1+3+6
+1+4+5
+2+3+5
+1+2+3+4
Dynamic Solution:
+1+2+3+4
+2+3+5
+1+4+5
+1+3+6
+4+6
+1+2+7
+3+7
+2+8
+1+9
+10
See also on ideone.
Let F(x,i) be the number of ways elements of A[1:i] can be summed to get x.
F(x,i+1) = F(x-A[i+1],i) + F(x,i)
That is it!
This is not a dynamic programming solution though. Non-recursive.
Assumption that arr is sorted in your case like [i....j] where a[i] <= a[j]
That's easy enough
void summer(int[] arr, int n , int b)
{
int lowerbound = 0;
int upperbound = n-1;
while (lowerbound < upperbound)
{
if(arr[lowerbound]+arr[upperbound] == b)
{
// print arr[lowerbound] and arr[upperbound]
lowerbound++; upperbound--;
}
else if(arr[lowerbound]+arr[upperbound] < b)
lowerbound++;
else
upperbound--;
}
}
The above program is easily modifiable to a recursive you need to only change the function definition by passing lowerbound and upperbound.
Case for termination is still lowerbound < upperbound
Base case is if arr[lowerbound] +arr[upperbound] == b
Edited based on comments
You will need to use a modified version of integer knapsack problem. The values of [i,j] both need to be modified accordingly. You are having the problem because you are not most probably modifying your i carefully, Increase your i accordingly then their will not be repetition like the one you are having.

Resources