hey guys I am trying to solve a problem that requires making a program that searches for number k in n strings in an array and all its previous numbers including zero and finally calculates how many strings in array have these numbers . for example if the input is 2 strings ("0123","012") and search for number 1 the output should be 2 in this case .
so I made an array of strings and 2 loops to search in every char in every string (every element in array) but my program gives me wrong answer I don't know why , am I using a wrong function to search (find function) or what ?
#include <bits/stdc++.h>
using namespace std;
main() {
int n, k, sum = 0, good = 0;
cin >> n >> k;
string x[n];
for (int i = 0; i < n; i++) cin >> x[i];
for (int i = 0; i < n; i++) // string loop
{
for (int m = 0; m <= k; m++) // char loop
{
char c = '0' + m;
size_t search = x[i].find(c);
if (search != string::npos) {
sum++;
}
}
if (sum == (k + 1)) good++;
}
cout << good;
}
Related
I have an exercise which looks like that:
Problem statement is simple and straight forward . You will be given a non-negative integer P of length N and you need to check whether
it's divisible by Q ?
Integer P will be given in its decimal representation with P0 as leftmost digit and P1 as second digit from left !
Rest of the digit can be generated from the formula :
Pi = ( 4*Pi-1 + Pi-2 ) modulo Q for 2 <= i <= N-1
Input
The first line contains one integer T - denoting the number of test cases.
T lines follow each containing four integers P0 , P1 , Q and N !
Output
For each testcase output YES if the corresponding integer is divisible by Q and NO otherwise.
Constraints
T <= 100000
0 < P0 , P1 , Q < 10
0 < N <= 1018
Example
Input:
4
1 4 2 2
1 4 2 1
4 2 3 2
3 4 7 3
Output:
YES
NO
YES
NO
Explanation
Value of P is 14, 1, 42, 345 in respective cases !
and that's what I came up with
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
int t, q, n, p_0, p_1, p_temp, p;
vector<int> digits;
vector<string> answers;
string number = "";
cin >> t;
for (int i = 0; i < t; i++)
{
cin >> p_0 >> p_1 >> q >> n;
if (n == 1)
{
digits.push_back(p_0);
}
else
{
digits.push_back(p_0);
digits.push_back(p_1);
for (int i = 2; i <= (n - 1); i++)
{
p_temp = (4 * digits[i - 1] + digits[i - 2]) % q;
digits.push_back(p_temp);
}
}
for (int i = 0; i < digits.size(); i++)
{
number += to_string(digits[i]);
}
p = stoi(number);
cout << number << endl;
if (p % q == 0)
{
answers.push_back("YES");
}
else
{
answers.push_back("NO");
}
number = "";
}
for (int i = 0; i < answers.size(); i++)
{
cout << answers[i] << endl;
}
}
Everything I have done works fine, except for one thing, this part does not clear my number variable
number = "";
And honestly I don't know why, could someone correct my mistakes and explain me what did I do wrong. Thanks.
Your problem is with the digits vector.
Each loop the number string just gets repopulated with the digits vector which is never cleared.
Use digits.clear() to empty the vector like so:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
int t, q, n, p_0, p_1, p_temp, p;
vector<int> digits;
vector<string> answers;
string number = "";
cin >> t;
for (int i = 0; i < t; i++)
{
cin >> p_0 >> p_1 >> q >> n;
if (n == 1)
{
digits.push_back(p_0);
}
else
{
digits.push_back(p_0);
digits.push_back(p_1);
for (int i = 2; i <= (n - 1); i++)
{
p_temp = (4 * digits[i - 1] + digits[i - 2]) % q;
digits.push_back(p_temp);
}
}
for (int i = 0; i < digits.size(); i++)
{
number += to_string(digits[i]);
}
p = stoi(number);
cout << number << endl;
if (p % q == 0)
{
answers.push_back("YES");
}
else
{
answers.push_back("NO");
}
digits.clear();
number = "";
}
for (int i = 0; i < answers.size(); i++)
{
cout << answers[i] << endl;
}
}
To clear a string you can/should use std::string::clear() as:
number.clear();
There may be other logical errors in your program which may be the reason for not getting the output you expect.
Also instead of creating/initializing the string number using string number = "";, you should use
string number;//no need to write = ""
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a problem and I have no idea how to start, here it is:
You are given a permutation of 1,2,...,n.
You will be given q queries, each query being one of 2 types:
Type 1: swap elements at positions i and j
Type 2: given a position i, print the length of the longest subarray containing the ith element such that the sum of its elements doesn't exceed n
Input
The first line contains 2 integers n and q (1≤n,q≤105).
The next line contains the n integers of the permutation p1,…,pn (1≤pi≤n).
The next q lines contain queries in the format:
Type 1: 1 i j (1≤i,j≤n)
Type 2: 2 i (1≤i≤n)
Output
For each query of type 2, print the required answer.
Here is my code so far, but it gives me the wrong answer on the last few cases:
#include <iostream>
#include <algorithm>
long long atMostSum(int arr[], int n, int k, int a)
{
int sum = arr[a];
int cnt = 1, maxcnt = 1;
for (int i = 0; i < n; i++) {
if ((sum + arr[i]) <= k) {
sum += arr[i];
cnt++;
}
else if(sum!=0)
{
sum = sum - arr[i - cnt] + arr[i];
}
maxcnt = std::max(cnt, maxcnt);
}
return maxcnt - 1;
}
int main(void) {
int n, q;
std::cin >> n >> q;
int p[n];
for(int i = 0; i < n ; i++) {
std::cin >> p[i];
}
for (int i = 0; i < n; i ++) {
int a;
std::cin >> a;
if (a == 2) {
int m;
std::cin >> m;
std::cout << atMostSum(p, n, n, m) << "\n";
} else {
int l, f;
std::cin >> l >> f;
int temp;
temp = p[l];
p[l] = p[f];
p[f] = temp;
}
}
}
Sample Input
3 3
1 2 3
2 1
2 2
2 3
Sample Output
2
2
1
long long atMostSum(int arr[], int n, int k, int a){
long long ans = arr[a];
for (int i = 0; i <= a; i++) {
long long sum = 0;
for (int j = i; j < n; j++) {
sum += arr[j];
if(sum > n) break;
if(i <= a && a <= j) {
ans = max(ans, sum);
}
}
}
return ans;
}
this isn't best for complexity but should be right...
Cannot provide you with the complete solution, but something to get you started with. You are basically looking for a subset in arr[1...n] which consists of element arr[i] and a sum not exceeding n. So simply look for a sub-array with sum (n - arr[i]) and then it becomes a classical problem of finding a subarray with given sum
You can refer the later part of solution here - https://www.geeksforgeeks.org/find-subarray-with-given-sum/
In the following code, I am having trouble comparing specific letters two given strings.
#include <bits/stdc++.h>
using namespace std;
int main() {
int m, n;
cin >> m >> n;
cin.ignore();
string phrases[m];
string records[n];
for (int i = 0; i < m; i++) {
getline(cin, phrases[i]);
}
for (int i = 0; i < n; i++) {
getline(cin, records[i]);
}
int lowBound;
sort(phrases, phrases + m);
int ans = 0;
bool stillIs;
for (int i = 0; i < n; i++) {
lowBound = lower_bound(phrases, phrases + m, records[i]) - phrases;
if (lowBound == m) {
continue;
}
stillIs = true;
for (int j = 0; j < records[i].length(); i++) {
if (records[i][j] == phrases[lowBound][j]) {
stillIs = false;
}
}
if (stillIs) {
ans++;
}
}
cout << ans;
return 0;
}
On line 33, if (records[i][j] == phrases[lowBound][j]), it is not giving me an error, but if I run it with this line, nothing happens, but when I comment the if statement out, it works properly, but obviously not giving me the correct answer. Is there any way I can compare these two strings (the second one is larger in size than the first one) to find whether the first one is the start of the second one?
Thanks!
You are incrementing i instead of j in the loop at line 32
You have your test backwards in line 33 - you want to set stillIs = false if the characters don't match, (i.e., !=)
I haven't fully read your code, but those two problems jumped out at me, so see if that sorts it out
The output for the variable sum_e is negative which isn't what I expect it to be.
I have simply added the values at even and odd places and stored them in two variables. I checked for solutions and found ones with digit extraction from a number. None of them had a string input.
#include<iostream>
using namespace std;
int main(){
string s;
cin>>s;
int sum_e=0,sum_o=0;
int l=s.length();
for(int i=0;i<=l;i=i+2){
sum_o+=(s[i]-'0');
}
for(int j=1;j<=l;j=j+2){
sum_e+=(s[j]-'0');
}
cout<<sum_o<<endl<<sum_e;
return 0;
}
I subtracted '0' from the string index to convert it into int. One of the variables shows the right output and the other shows a negative one.
Your for loops run one time longer than the length of the array, so when i = l, s[i] will get an undefined/garbage value from memory. Use i < l and j < l rather than i <= l and j <= l, since the index in C++ begins at zero.
#include <iostream>
using namespace std;
int main(){
string s;
cin >> s;
int sum_e = 0, sum_o = 0;
int l = s.length();
for(int i = 0; i < l; i = i + 2){
sum_o += (s[i] - '0');
}
for(int j = 1; j < l; j = j + 2){
sum_e += (s[j] - '0');
}
cout << sum_o << endl << sum_e;
return 0;
}
To improve your code, use one for loop instead of two.
for(int i = 0; i < l; i++){
// Check if even (i%2 returns the remainder of i/2, so here i%2==1 means even)
if(i%2 == 1){
sum_e += (s[i] - '0');
}else{
sum_o += (s[i] - '0');
}
}
Array indexing in C++ starts from 0. You store the length of string as l, so elements of your string lies from s[0] to s[l-1]. At s[l] some garbage value is present which gets added to one of your variables, hence producing undesired results.
#include<iostream>
using namespace std;
int main(){
string s;
cin>>s;
int sum_e=0,sum_o=0;
int l=s.length();
for(int i=0;i<l;i=i+2){ // use <
sum_o+=(s[i]-'0');
}
for(int j=1;j<l;j=j+2){ // use <
sum_e+=(s[j]-'0');
}
cout<<sum_o<<endl<<sum_e;
return 0;
}
You can also do your odd and even position sum using a single loop. Your code size will reduce and look better
for(int i=0;i<l;i=i+2){
if(i%2==0)// even index means odd position numbers
sum_o+=(s[i]-'0');
else
sum_e+=(s[j]-'0');
}
I was just wondering if any other logic is possible for this problem:
Question : Find the number of pairs in a given string and output the sum of all the pairs and the unpaired elements. PS: The input is case sensitive.
Example O/P:
eeqe 3
aaaa
2
rwertr
5
I figured out the solution to this problem by first sorting the input string and then comparing adjacent elements as shown in the code below:
int main()
{
int t,count=0,pos=0;
char swap;
char a[201];
cin>>a;
int len=strlen(a);
//cout<<a<<endl;
for (int c = 0 ; c < ( len - 1 ); c++)
{
for (int d = 0 ; d < len - c - 1; d++)
{
if (a[d] > a[d+1]) /* For decreasing order use < */
{
swap = a[d];
a[d] = a[d+1];
a[d+1] = swap;
}
}
}
//cout<<a<<endl;
count=0;
for(int i=0;i<len;){
if(a[i]==a[i+1]){
count++;
i+=2;
//if(i== len-2)i++;
}
else{ count++; i++;}
}
//if(a[len-1]!=a[len-2])count++;
cout<<count<<endl;
return 0;
}
This code works fine. But, I was just wondering if there is any other efficient solution to this problem that doesn't involve sorting the entire input array.
It basically avoids sorting based on the idea that there are only 256 possible chars, so it's sufficent to count them.This is my solution:
int main()
{
std::string s; std::cin >> s;
int cnt[256] = {};
for (std::size_t i = 0; i < s.size(); ++i)
++cnt[static_cast<unsigned char>(s[i])];
int sum = 0;
for (std::size_t i = 0; i < 256; ++i)
sum += cnt[i]/2 + cnt[i]%2;
std::cout << sum << std::endl;
}
If, for example, the string contains 5 times an 'a', this allows 5/2 pairs (integer division) and 1 remains unpaired (because 5 is odd => 5%2 is 1)
Edit: Because we are here in SO:
int main()
{
std::array<int, 256> cnt{-1}; // last char will be '\0', ignore this.
std::for_each(std::istreambuf_iterator<char>(std::cin.rdbuf()),
std::istreambuf_iterator<char>{},
[&](unsigned char c){++cnt[c];});
std::cout << std::accumulate(cnt.begin(), cnt.end(), 0,
[](int i, int c)->int{return i+(c/2)+(c%2);}) << '\n';
}