How to process a string? [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
The first line of input will be 2 integers, h and w, which is the height(h) and width(w) of the rectangular area of sky you will be counting stars from. You may assume their values are below 1000.
The following h lines will have w characters per line, either x (Clear Sky) or * (star).
Sample Input
5 5
x***x
xxx*x
x*xxx
xxx*x
*xxxx
Sample output
7
How can I process the string?

Declare a variable to count the stars with an appropriate data type.
Iterate (loop) over the string to check each character for equality with '*'. If this is the case then increment your star counter.
Width and height are not required. If you want to constrain a larger field to the width and height provided, you can use a column (character# in a line) and a row (line#) counter and keep track where in the file you are.
After each character in your string increment (add 1) your column counter. After each line ('\n'-character denotes a new line) increment your row counter and reset the column counter. if your current column or row exceed the provided width or height, then ignore any '*'-characters until you are within bounds again or the string ends.
I leave the actual code for you as an excercise.
Helpful links:
Strings
Operators
Conditionals
If you have trouble with the input part, you should consider looking elsewhere than stackoverflow.

Related

is setw() and "\t" the same thing? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Is setw() and "\t" the same thing tho?
And are they similar to "space" too?.
Can I use setw() in the place of "\t" or would it result in a completely different output?
They have almost nothing in common.
std::setw(int n) set the width of the next element that goes into the stream. So if you have things like:
std::cout << "Hi," << std::setw(12) << "there!";
This would print:
Hi, there!
^^^^^^ <- 6 empty spaces were made here to fill the width
If you set the width to be longer than the actually object streamed in to it, it will automatically fill them with spaces.
On the other hand, '\t' is a predefined escape sequence. And it will behave similar to when you type a tab in many text editors. Also note that it is actually a character, you could put that in any strings:
std::cout << "\tHi,\tthere!";
This would print:
Hi, there!
^^^^ ^ <-- both of them are tabs
Note those tabs were made different sizes, you should be able to observe similar behaviors when using tabs in text documents. It will try to fill the current 4 block text with spaces if it was not filled yet.
No they are not same at all.
"\t" : allocates 4 spaces;
setw() : setWidth() is a function defined in iomanip header file.
It takes a integer as parameter and allocates the width of value of the integer.
Take for an example : setw(7) It will allocate 7 spaces to you
cout<<setw(7)<<"Hi"<<"**";
Output will be : Hi + 5 Spaces + **
5 Spaces because in total you requested 7 spaces and 2 are occupied by 2 character of word "Hi".
This function is highly used where you want to display something in a very proper format.

Choose k not-adjacent elements out of N [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Suppose we have a line of dots, there are totally N of them, we are to choose k dots such that each pairs of dots are not neighbors. How many possibilities there are in total?
For example, if we have N = 3 dots, we want to choose 2 dots that aren't adjacent, then there's only 1 possibility.
First and last dots have N-2 options to choose from, as you can't select the same dot or its neighbor. All the other 'inside' dots have N-3 options, as you can't select the same and one adjacent dot on each side.
If you would sum them all up, you would get twice as many options (because of duplicates). So N-2 dots have N-3 options, but you have to divide it by 2.
The result would be K = (N-2) + (N-2)*(N-3)/2

Regex (Bigquery) get specific values from STRING [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have the STRING - TX1234XT batch 44, 1111ABCDEF
TX1234XT (Can be different length)
batch 44 (number can be different length)
ABCDEF (can be a different length, but always have 1111 at the start)
What I need is to generate two columns:
BatchNumber Name
44 1111ABCDEF
1 1111SAMPLE
999 1111Example
Starting point:
First is done:
REGEXP_EXTRACT(reference, r'1111[a-zA-Z0-9_.+-]+') AS Name
Second
- REGEXP_REPLACE(REGEXP_EXTRACT(reference, r'batch [0-9_.+-]+'),r'batch ','') AS BatchNumber
SORTED ^_^
I don't really know Google Big Query, but if you want to extract the batch number and the value at the end, you could go with this regular expression:
/^.*?batch\s*(\d+),\s*(1111.+)$/
(\d+) will capture your batch id.
(1111.+) will capture the value starting with 1111.
Example here: https://regex101.com/r/SJXmIV/2

Place Commas Between Numbers [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I'm having trouble getting this to work in AS3, I want to place a comma between numbers only when their is a whitespace.
For example, if the string is "1.23 5.34" I want it to become "1.23, 5.34". The trouble is the white space varies and the number may or may not contain a decimal. So, I'd want "1 1.4" to become "1, 1.4" or "2.3 4.5" to become "2.3, 4.5". This also includes negative numbers, so "1.4 -15.3" should become "1.4, -15.3". If there is anything but a number on either side of the white space, I'd want to skip that space and not effect it. So "Car 35.2" would be skipped and so would (13.5 ).
I've tried several Regexs found around the net and did my best with the limited regex knowledge I have, any help would be greatly appreciated.
Thanks.
UPDADE
(?<=\d)(\s)(?=-?\d) (thanks for your comment Tim)
Try the folowing pattern:
"(?<=\d)(\s)(?=[\d-])" (edited to include negative ones)
replace for ",$1"
youre essentially replacing " " with ", "
var value:String = "1 2 -3 4 -5";
var csvValue:String = value.split(" ").join(", "); // will print out "1, 2, -3, 4, -5"

SPOJ ALPHA CODE [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I was practicing the dynamic programming problem on SPOJ. But I have no idea how to solve this one.
Can anyone please help ME in solving http://www.spoj.pl/problems/ACODE/ problem on SPOJ
Thanks!
Alice and Bob need to send secret messages to each other and are
discussing ways to encode their messages:
Alice: “Let’s just use a very simple code: We’ll assign ‘A’ the code
word 1, ‘B’ will be 2, and so on down to ‘Z’ being assigned 26.”
Bob: “That’s a stupid code, Alice. Suppose I send you the word ‘BEAN’
encoded as 25114. You could decode that in many different ways!”
Alice: “Sure you could, but what words would you get? Other than
‘BEAN’, you’d get ‘BEAAD’, ‘YAAD’, ‘YAN’, ‘YKD’ and ‘BEKD’. I think
you would be able to figure out the correct decoding. And why would
you send me the word ‘BEAN’ anyway?” Bob: “OK, maybe that’s a bad
example, but I bet you that if you got a string of length 5000 there
would be tons of different decodings and with that many you would find
at least two different ones that would make sense.” Alice: “How many
different decodings?” Bob: “Jillions!”
For some reason, Alice is still unconvinced by Bob’s argument, so she
requires a program that will determine how many decodings there can be
for a given string using her code.
Input
Input will consist of multiple input sets. Each set will consist of a
single line of at most 5000 digits representing a valid encryption
(for example, no line will begin with a 0). There will be no spaces
between the digits. An input line of ‘0’ will terminate the input and
should not be processed.
Output
For each input set, output the number of possible decodings for the
input string. All answers will be within the range of a 64 bit signed
integer.
Example
Input:
25114 1111111111 3333333333 0
Output:
6 89 1
Starting from the left, do the following:
Find how many words the sequence can be interpreted as (call that say x[k]) up to this point using a finite number of the values for previous calculated for points along the sequence.
Move to the next point.
If you still can't get it, you can take a look at the Welcome to Code Jam problem. It somewhat similar and has readily available explanations for it.
If you have a string of numbers as S, then, there are two cases possible :
1) only the first digit corresponds to an alphabet
2) the first two digits correspond to an alphabet. BUT, only if the first two digits don't form a number greater than 26.
Let S be of size n. Let f(Si) be the number of strings formed by last i digits. Note that you have to find f(Sn).
Using the above two rules, you can write a relation as :
If first two digits form a number <= 26 :
f ( Sk ) = f (Sk-1) + f (Sk-2)
If first two digits form a number > 26 :