## [URI Online Judge] – 2496 – The Only Chance

Link: https://www.urionlinejudge.com.br/judge/en/problems/view/2496

Everyone knows that the decisions that are made make all the difference in the result obtained. A well-known example is the Monty Hall paradox, which consists of three stages, and in the third, the competitor gives the final card and, depending on his choice, may or may not win a car.

You want to get an “Accepted” and for this you will have to write a program that checks if a single position change between two letters will result in an ordered sequence. Consider the following strings:

ABCDFGHIEJ

ABCDEFGHJIKLMNO

For the first sequence to be ordered, more than one change is required between the position of the letters. In the second, on the other hand, it is enough that I and J change positions. Its mission is to verify if for each sequence received there is a single exchange between two letters that makes it ordered.

## Input

The input is composed of an integer **N**, which represents the number of test cases (1 <= **N** <= 100). Each test case consists of an integer **M**, which represents the number of letters of a sequence (2 <= **M** <= 26) and an alphabetical sequence composed of **M** letters. Letters are always uppercase and are not in an index greater than **M**. If **M** is 4, the only possible letters for the sequence are: A, B, C, or D in any order.

## Output

For each reported sequence, the program must return a single line that will display “There are the chance.” If the sequence obeys the ordering rule mentioned, or “There are not the chance.” Otherwise.

Input Samples | Output Samples |

2 4 ABDC 4 ACDB | There are the chance. There aren’t the chance. |

2 10 26 | There aren’t the chance. There are the chance. |

solution:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include <bits/stdc++.h> using namespace std; int main() { //freopen("in.txt","r",stdin); int n,t,l; string s; string orginal_string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; cin >> n; while(n--){ bool b = true; int p = 0; cin >> t; cin >> s; for(int i = 0; i < t; i++){ if(s[i] != orginal_string[i]){ if(orginal_string[i] != s[((int)(s[i]) - 65)] or p > 0){ b = false; break; }else{ swap(s[i],s[((int)(s[i]) - 65)]); p++; } } } cout << ((b == true and p < 2) ? "There are the chance.\n" : "There aren't the chance.\n"); } return 0; } |