We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Mathematics
- Probability
- Palindromes
- Discussions
Palindromes
Palindromes
Sort by
recency
|
18 Discussions
|
Please Login in order to post a comment
Where is the issue???
w
include
include
include
include
include
include
include
include
include
include
char* readline(); char* ltrim(char*); char* rtrim(char*);
int parse_int(char*);
/* * Complete the 'solve' function below. * * The function is expected to return a DOUBLE. * The function accepts STRING s as parameter. / bool is_palindrome(const char s){ int n=strlen(s); for(int i=0;i
int min_swaps_to_palindrome(char* s){ int n=strlen(s); int swaps=0;
}
double solve(char* s) { if(is_palindrome(s)) { return 0.0; } int min_swaps=min_swaps_to_palindrome(s); if(min_swaps == -1) { return (double)INT_MAX; } return(double)min_swaps; }
int main() { FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
}
char* readline() { size_t alloc_length = 1024; size_t data_length = 0;
}
char* ltrim(char* str) { if (!str) { return '\0'; }
}
char* rtrim(char* str) { if (!str) { return '\0'; }
}
int parse_int(char* str) { char* endptr; int value = strtol(str, &endptr, 10);
} where is the issue
Given a string, you keep swapping any two characters in the string randomly till the string becomes a palindrome. What is the expected number of swaps you will make? There will always be at least one palindrome which can be formed with the letters of the given string.
Input: The first line contains the number of test cases T. Each of the next T lines contains a string each.
Output: Output T lines containing the answer for the corresponding test case. Print the answer correct to 4 decimal places.
Constraints: T <= 10000 The length of the string will be at most 8 characters. The string will consist of only lower-case letters 'a'-'z'.
Sample Input:
4
b
bb
abb
cbaabbb Sample Output:
0.0000
0.0000
3.0000
59.3380 Explanation:
For the first two cases, the string is already a palindrome so no swaps are needed.
For the third case, there are 3 possible swaps. The string will become "bab","bba" or remain "abb" with 1/3r
d
probability each. It's easy to see that the expected number of swaps needed is 3.0000************For the last case, the answer is 59.337962..., which should be printed as 59.3380
getting a timeout, how do i speed it up
can anyone keep answer in c