what's number anagram which are palindromes in string [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What is the number of anagrams which are palindromes in a string?
Example : string = "aaabbbb";
Possible anagram's which are palindromes "abbabba" , "bbaaabb" and "bababab".
The problem here is the time, i have string of size 10^9.
here's my final code can anybody tell me what's the wrong with it ?

Every letter in your input string has to appear in an even amount, execpt one letter can appear in an odd amount. This letter has a fixed position in the palindron. It has to be exactly in the middle. Lets say the amounts of the letter a,b,c,... are #a, #b, #c, ...
You only care about half of those letters, because in an palindron, the second half depands of the first half. So we only use half of the letters:
I used the floor function, so I calculate the letter, which appears in an odd amount, correct.
So how many permutations are in the first half? This is a case of distinct permutation, so we get
possibilities.
For your example:
string = "aaabbbb";
We get: #a=3, #b=4. Therefore
We get 3 palindroms, these are "abbabba" , "bbaaabb" and "bababab", like you posted.
So, if you have a very large string:
Count the amounts of each letter
Check, if there is only 1 letter that appears in an odd amount. It there are more, you can't create palindroms.
Use the formular to calculate the number of different palindroms.

Since each side of the anagram must be a mirror image of the other, the number of anagrams we care about is basically just the number of anagrams we can form on one side, so:
group the characters in the string so identical characters are together (e.g., by sorting).
Check for an odd number of more than one character (of so, # anagrams = 0).
Take half the characters of each group of identical (truncating in the case of odd number).
Compute the number of unique permutations of those characters.

Related

Regex to match string with prime length? [duplicate]

This question already has answers here:
How to determine if a number is a prime with regex?
(4 answers)
Closed 3 years ago.
Given an input string with an arbitrary number of 'x' characters (x, xx, xxxxx, xxxxxxxxxxxxx and so on), how can one write a regex that matches the input string only if it has a prime number of 'x' characters? A string of length 1 should not be matched.
For example:
Match these:
xx
xxx
xxxxx
xxxxxxx
But not these:
x
xxxx
xxxxxxxxx
This is one solution I found - ^(?!(xx+)\1+$) (here, as an answer to this problem). However, I would like to know why it works. Please share any alternate solutions as well.
I'm using the PCRE engine.
I realize that one would typically not use regexes for this sort of thing. I am simply curious about how it could be done.
^(?!(xx+)\1+$)
works by performing a negative lookahead at the start of the line. It will reject any line that consists of
a number of 2 or more xes
followed by the same number of xes, possibly multiple times
In other words, the regex works by matching any number of xes that can not be divided in smaller, equally sized groups of size >= 2.
To exclude the case of just a single x, you could use ^(?!(xx+)\1+$|x$).
I don't think regex is the right tool for this. Why do you need to do this?
If you can't make any assumptions about the length of the strings you need to check if the number is a prime number somehow (which is computationally expensive).
If you know the max length you could precalculate the prime-numbers, and then check the length against them, but it would still be needlessly complex to do this using regex.
So the only way I know of to do this is to use \b(\d{2}|\d{3}|\d{5})\b which as you can tell will quickly become cumbersome.

How does this code works in c++? [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 6 years ago.
Improve this question
I participated in Codeforces Round #396 (Div. 2) yesterday.
The (A) problem seemed pretty advanced in my opinion when I read it.
I tried solving it all the competition time yesterday and some time today.
I came up with a 200 lines long(half-working) solution. And then I gave up.
I looked what other people wrote there and I saw max 20 lines long code that seems magic to me.
The problem asks you to output the length of the longest uncommon subsequence of letters from two strings.
You can read the full problem here:
http://codeforces.com/contest/766/problem/A
#include<bits/stdc++.h>
using namespace std;
string a,b;
int main(){
cin>>a>>b;
printf("%d",a==b?-1:max(a.size(),b.size()));
return 0;
}
This is all the code used to solve the problem, and I really want to know how that one line of code
printf("%d",a==b?-1:max(a.size(),b.size()));
can solve this "advanced" task?
If the two strings are equal, there is no "uncommon subsequence". If they are not equal, neither one is a subsequence of the other, but each one is a subsequence of itself, so each one is an "uncommon subsequence". The longer of the two is the longest "uncommon subsequence", and its length is the correct answer. If the two are not equal but have the same length, then each one is an "uncommon subsequence" and the length of the longest is just the length of either one.
Don't get tangled up in what you (or any other reasonable person) think a "subsequence" is. The problem defines "uncommon subsequence", and all you have to do is apply its definition. This "problem" is about word play, not coding.
It does not seem to solve the task at hand. It just outputs -1 when the strings are equal and the length of the longer string when they are not different. However, the problem asks for the longest uncommon sequence. It would fail with the inputs abc and abcd to give the answer 1 would rather give 4.
What the line does is the following:
It checks whether a == b, whether the strings are equal.
If they are equal, the condition A is true in the ternary operator A ? B : C, so that expression will evaluate to B which is just -1 here.
If they are not equal, it will evaluate to C which is the maximum of the two string lengths, so it will be length of the longer string.
The value of the ternary expression is then printed, albeit without a newline, which is a bit bad.
So the code is so short because it only solves the problem for a few cases but not the general case.
printf ("%d", a == b ? -1 : max (a.size (), b.size ()));
is equivalent to
if (a == b) {
printf ("%d", -1);
}
else {
if (a.size () > b.size ()) {
printf ("%d", a.size ());
}
else {
printf ("%d", b.size());
}
}
BTW That's only a small step in solving the problem...
[CORRECTION]
IT DOES SOLVE THE WHOLE PROBLEM (See answer of Pete Becker)

C++ User defined sequence integers without using arrays [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 6 years ago.
Improve this question
I'm trying to figure this one out on my own and I can't.
The professor is asking for this.
User will define the length of the sequence.
User will then enter numbers from any range being both neg and pos.
The program:
Find the two largest numbers and display in descending order.
Find the two smallest numbers and display in ascending order.
Can't use sort or arrays.
I have an outline but, my head just wants to use arrays..... I don't see this simple without an array. Confused. I'm a beginner. I just want help. Advice? Tutor?
Start with a solution to an easier problem, and grow it into a solution to the actual problem that you are solving:
Write a program that finds and prints the largest number entered by the user. This is easy to do with a single variable keeping track of "high watermark"
Modify your program to keep track of the smallest number as well. You can do it by adding another variable, and keeping track of the "low watermark".
The challenge in both tasks above is the initial value of the high/low watermark. This is a common source of errors; there are multiple Q&As on SO explaining the fix.
Now for the fun part:
Modify your program to keep track of the second-largest number by "demoting" the number that was previously considered largest to second-largest each time that you find a number that is larger than the largest one, and by replacing the second-largest when you find a value above it that does not exceed the largest value.
This would require you to write a couple of if statements.
Finally, modify your program to keep track of the second-smallest number by applying the "mirror image" of the algorithm above.
Algorithm ("Find the two largest numbers in a sequence without storing the sequence")
(I decided to undelete this answer. It might be helpful to OP to get away from the array frame of mind)
Input: A sequence of values S.
Output: Values Max and SecondMax.
The consumed, current value of S is E
1 : Set Max to E
2 : Set SecondMax to E (Second value in S)
3 : If Max < SecondMax -> Swap Max and SecondMax.
4 : while ( e:= E exists)
4.1 : If ( e > Max ) SecondMax := Max, Max := e
4.1.1 : Else If ( e > SecondMax ) SecondMax := e
This is trivially extended to the minimums.

How to apply mask to string in C/C++? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've been asigned with a homework, but I don't know what to do exactly.
Input file is randomly generated from letter from 'a' to 'z'. User will type a mask only with 0/1, for example 011011, where 1 means vowel and 0 means consonant.
Output will be all matches found in input that match the user-given mask (for example for 011011 output will be abbezz).
Any idea how to make this? I don't ask for code, but only for easiest way how to make this possible in c/c++.
Thanks
My understanding is that the requirements are to find all sequences of letters that match the mask.
Given the mask: 010 (consonant, vowel, consonant)
Here are the matches for a couple of words:
"are" - fail, first character is a vowel.
"mat" - pass, 'm' is a consonant, 'a' is a vowel, 't' is a consonant.
"mate" - fail, too many letters.
You will need to have some functions that test a letter for vowel or consonant.
Also consider using a state machine. If a test fails, you want to go back to the starting state.
Write down your algorithm first, step by step. Come up with a few test cases to verify your algorithm. After algorithm works, code it up. Use your same test cases for verifying the program.
Map the string to 0/1 according to the rules you have and search for substrings in the new string that match the mask.
I still do not know exactly what you are trying to accomplish, but maybe it helps to know that once you have your input in a std::string, you can iterate over the characters using iterators and any algorithm. Here is an example with std::for_each:
#include <algorithm>
#include <iostream>
#include <string>
void applyMask(std::string::reference aCharacter)
{
// apply your mask here, character by character
if ('a' == aCharacter) {
aCharacter = 'A';
}
}
int main()
{
std::string s("stackoverflow");
std::for_each(s.begin(), s.end(), &applyMask);
std::cout << s << std::endl;
}
You might want to have a look at std::transform, e.g. http://www.cplusplus.com/reference/algorithm/transform/

How to find longest palindrome [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Write a function that returns the longest palindrome in a given string
I have a C++ assignment which wants me write a program that finds the longest palindrome in a given text. For example, the text is this: asdqerderdiedasqwertunut, my program should find tunut in the index of 19. However if input is changed into this astunutsaderdiedasqwertunutit should find astunutsa in the index of 0 instead of tunutin index of 22.
So, my problem is this. But I am a beginner at the subject, i know just string class, loops, ifs. It would be great if you could help me on this.
Thanks in advance.
The idea is very simple:
Write a function is_palindrome(string) that takes a string, and returns true if it is a palindrome and false if it is not
With that function in hand, write two nested loops cutting out different substrings from the original string. Pass each substring to is_palindrome(string), and pick the longest one among the strings returning true.
You can further optimize your program by examining longest substrings ahead of shorter ones. If you examine substrings from longest to shortest, you'll be able to return as soon as you find the first palindrome.
Dasblinkenlight's idea is pretty good, but it's faster this way:
A palindrome has either an even number of letters or odd, so you have two situations. Let's start with the even. You need to find two consecutive identical letters, and then check whether the immediately previous letter is identical to the next letter. The same in the other situation, except at first you only need one letter. I don't speak English that well, so I hope you understood. :)