Was given to me a task to do encrypt by 2 string, with allowed only latters and spacebar.
One string is text which should I encrypt and second string is key to encrypt text. Also I should add that text must be longer than key.
Replace letters with numbers. So I decided to use ASCII, maybe if I made numbers on my own it would be easier.
Successive characters to be encrypted are summed up with an following character from key the effect of summing modulo n. Im not so sure what it means with this modulo n.
There is few more points but I can handle them alone.
Here is my 1st try with this but original text is resetting also with key.
int pom=0;
int tabenc[a.length()];
int l=0;
while(pom<=a.length()-1){
for(int k=0;k<a.length();k++){
cout<<"Nr: "<<l<<" Sum char: "<<a[k]+b[k]<<" number text:"<<a[k]<<" number key: "<<b[k]<<endl;
tabenc[pom]=a[pom]+b[k];
pom++;
}
}
And my 2nd try was with 2 fors but here probably i already overthinked. I have no clue how I can manage to do this.
int pom=0;
for(int i=0;i<a.length();i++)
{
pom=a[i];
}
for(int j=0;j<b.length();j++){
pom=a[i]+b[j];
cout<<"Nr: "<<i<<" | Sum: "<<a[i]<<" + "<<b[j]<<" = "<<pom<<endl;
}
cout<<"Nr: "<<i<<" | Sum: "<<a[i]<<" + "<<b[i]<<" = "<<pom<<endl;
}
Related
#include<iostream>
using namespace std;
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
int space=n-i;
for(int j=1;j<=space;j++){
cout<<" ";
}
for(int k=i;k>=1;k--){
cout<<k<<" ";
}
for(int l=2;l<=i;l++){
cout<<l<<" ";
}
cout<<endl;
}
return 0;
}
I wrote this code for printing a palindrome pattern but the output I received has irregular spacing and no matter what I couldn't find the error in the code.
I assume the "correct" spacing is where all the 1's line up. Here are some steps you could try to solve the problem:
1. How is the position of 1 determined?
The position should be determined by the last line, starting with n — since we don't require any spaces on the front, according to your program.
2. At which position will 1 appear on the last line?
Before the character 1, there are spaces and other numbers — n-1 numbers from n to 2, and also the same number of spaces, since 1 space is presented after every character, except the last, which doesn't matter here. In total, 2(n-1) characters before 1.
3. Finally, How many spaces are required before line k?
For line k, we can use the above step to know that before the character 1 there are 2(k-1) characters, but we need 2(n-1) to fill the length. Thus, we can obtain the required space in front of the line as 2(n-1)-2(k-1) = 2(n-k). That's why you need the "times 2" over there on line 7.
Try to find steps toward problems.
The task is to make a letter pyramid. I have done it but its behaviour goes very strange after I pass a certain number of characters that were inputed. Looking forward to an answer.
string input{};
string reverseString{};
getline(cin,input);
for(int j = input.length()-1;j>=0;j--){
reverseString += input.at(j);
}
for(int i = 0;i<input.length();i++){
int numberOfSpaces {};
numberOfSpaces = input.length()-i;
string spaces(" ",numberOfSpaces);
cout<< spaces << input.substr(0,i) << input.at(i)<<reverseString.substr(numberOfSpaces,i) <<spaces<<endl;
}
This is an example of the input/output:
string spaces(" ",numberOfSpaces);
This doesn't do what you think it does.
This constructor takes an array and a count.
It copies count (in this case numberOfSpaces) items from the passed in array.
The passed in array has length 1 (technically 2 with the null terminator), and so anything > 2 will cause undefined behaviour as it reads off the end of the array.
The question asks for total time that will be required to type a string on a keyboard, which is represented as two dimensional matrix of characters, with one finger.
input:
2 31
YLrJpXOygVUl6MqBIRFWuAKsH7Gw4Z8
kE0tTQdP1CcxSjamizon9e5NfvDbh32
YE0
The first line contains n and m as input denoting dimensions of the keyboard matrix.
Next n lines contain m characters each denoting the character in the keyboard.
Next line will contain a string S
output:
3
Explanation:
The finger is initially at the first symbol of the keyboard so the time taken to press that key is 0. After that the new key is located at 1,1 so total time taken will be |1-0|+|1-0| i.e. 2 . Now the third key is located at position 1,2 so total time to move to that key will be |2-1|+|1-1| = 1 . So our answer is 3.
The string that's asked to print is in the last input line, YE0, consisting of letters from the above 2-D matrix.
The time calculation logic is:
If you are at cell (x1,y1) of the keyboard and now you want to press the key at (x2,y2) then the time taken will be |x1-x2| + |y1-y2|. You need to calculate the total time taken to type the complete string.
In case it is impossible to type the string you have to print -1.
#include<bits/stdc++.h>
using namespace std;
int main() {
int n,m;
cin>>n>>m;
unordered_map<char, pair<int, int>> map1;
for(int i=0;i<n;i++){
string s;
cin>>s;
for(int j=0;j<m;j++) {
map1.insert({s[j], make_pair(i,j)});
}
}
string key;
cin>>key;
long long total=0;
pair<int,int> sp;
for(int i=0;i<key.length();i++) {
if(map1.find(key[i])==map1.end()) {
total=-1;
break;
} else {
auto it = map1.find(key[i]);
if(i==0) sp=it->second;
pair<int,int> p = it->second;
total+=(abs(p.first-sp.first) + abs(p.second-sp.second));
sp=p;
}
}
cout<<total;
}
This solution is partially accepted and I am not able to figure out the edge cases for which its failing. Can somebody help me?
Here is one failing test case for free.
2 31
YLrJpXOygVUl6MqBIRFWuAKsH7Gw4Z8
kE0tTQdP1CcxSjamizon9e5NfvDbh32
YE 0
Should be -1 because the blank is not in the key matrix.
It fails because you only read in the "word" until first whitespace.
Here is another one:
2 31
YLrJpXOygVUl6MqBIRFW AKsH7Gw4Z8
kE0tTQdP1CcxSjamizon9e5NfvDbh32
Y E0
Shoudl not be -1, but is. Same problem, but with the matrix.
So what you need to do is change your input reading to include white space.
It's mentioned in the question that, "The finger is initially at the first symbol of the keyboard" so when you are parsing the key
In your code if(i==0) sp=it->second; should start from {0,0} to consider the movement from the first symbol of the keyboard to the first symbol of the key.
hello i am a beginner in programming and am in the array lessons ,i just know very basics like if conditions and loops and data types , and when i try to solve this problem.
Problem Description
When Serezha was three years old, he was given a set of cards with letters for his birthday. They were arranged into words in the way which formed the boy's mother favorite number in binary notation. Serezha started playing with them immediately and shuffled them because he wasn't yet able to read. His father decided to rearrange them. Help him restore the original number, on condition that it was the maximum possible one.
Input Specification
The first line contains a single integer n (1⩽n⩽105) — the length of the string. The second line contains a string consisting of English lowercase letters: 'z', 'e', 'r', 'o' and 'n'.
It is guaranteed that it is possible to rearrange the letters in such a way that they form a sequence of words, each being either "zero" which corresponds to the digit 00 or "one" which corresponds to the digit 11.
Output Specification
Print the maximum possible number in binary notation. Print binary digits separated by a space. The leading zeroes are allowed.
Sample input:
4
ezor
Output:
0
Sample Input:
10
nznooeeoer
Output:
1 1 0
i got Time limit exceeded on test 10 code forces and that is my code
#include <iostream>
using namespace std;
int main()
{
int n;
char arr[10000];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
for (int i = 0; i < n; i++) {
if (arr[i] == 'n') {
cout << "1"
<< " ";
}
}
for (int i = 0; i < n; i++) {
if (arr[i] == 'z') {
cout << "0"
<< " ";
}
}
}
Your problem is a buffer overrun. You put an awful 10K array on the stack, but the problem description says you can have up to 100K characters.
After your array fills up, you start overwriting the stack, including the variable n. This makes you try to read too many characters. When your program gets to the end of the input, it waits forever for more.
Instead of putting an even more awful 100K array on the stack, just count the number of z's and n's as you're reading the input, and don't bother storing the string at all.
According to the compromise (applicable to homework and challenge questions) described here
How do I ask and answer homework questions?
I will hint, without giving a code solution.
In order to fix TLEs you need to be more efficient.
In this case I'd start by getting rid of one of the three loops and of all of the array accesses.
You only need to count two things during input and one output loop.
I need to write a function string add(string a, string b)
where a and b are strings representing integers and the function add(a,b) returns a string
representing their sum.
Strings a and b can have a maximum of 100 characters.
I have tried different ways but failed, here is where I'm standing right now.
So I took the 2 strings and I tried adding each digit starting from last.
If in the array at [i] it's more than 10, then add 1 to [i-1], and mod it by 10 to get the last digit.
The return is empty:
string add(string a, string b){
int arrA[a.length()];
int arrB[b.length()];
string Res=" ";
//99999999 2222222
if(a.length()>=b.length()){
//i=7
for (int i=b.length();i>=0;i--){
arrA[i] = (int) (a[i]-'0') + (int) (b[i]-'0');
}
for(int i=b.length()-1;i>=1;i--)
Res[i]=arrA[i];
for(int i=a.length()-1;i>=1;i--){
if (arrA[i]>=10){
arrA[i]=arrA[i]%10;
arrA[i-1]=arrA[i-1]+1;}
}
}
else{
for (int i=a.length();i>=0;i--){
arrB[i] = (int) (a[i]-'0') + (int) (b[i]-'0');
}
for(int i=b.length()-1;i>=1;i--)
Res[i]=arrB[i];
for(int i=b.length()-1;i>=1;i--){
if (arrB[i]>=10){
arrB[i]=arrB[i]%10;
arrB[i-1]=arrB[i-1]+1;}
}
}
return Res;
}
Thank you in advance!
Think about how you would do this with pencil and paper, then write code to do the same thing.
You have two strings of digits. Start at the right, add the two digits, and if the result overflows, subtract 10 and note that you have a carry. Store the resulting digit. Move one place to the left. Repeat until done. If you run out of digits in one string, just pretend that you've got zeros for the rest of the digits.
Note that each digit in the input is the character representation of the digit. To get the numeric value, subtract '0' from each digit. Once you have the result, convert it to a character by adding '0'.
string add(string a, string b) {
int c = stoi(a) + stoi(b);
return to_string(c);
}