codeforce-----Treasure Hunt

After a grand birthday party, Katie (hereinafter referred to as Ka) still wanted to find more fun with Shiro (hereinafter referred to as S). Soon, she thought of a game called treasure hunt. Of course, she invited her best friend Kuro (hereinafter referred to as Ku) and S to play with her.
The three friends are very smart so they quickly passed all the challenges and finally reached the finish line. But the treasure can only belong to one cat (the protagonists are all cats), so they began to think of a way to determine the treasure's ownership. Soon, Ku came with some ribbons.
Each cat was given a random color and bright ribbon. Each color of the ribbon can be expressed in uppercase or lowercase letters. We define: a continuous sub-sequence within a color letter expression is a sub-ribbon in a ribbon. The degree of beauty of a ribbon is determined by the maximum number of occurrences of its sub-ribbons. The more sub-ribbons, the more beautiful the ribbon. For example, aaaaaaa colored ribbon has a beauty value of 7 because its subsequence appears 7 times, and abcdabc has a beauty value of 2 because its subsequence abc appears twice .
The rules are simple. The game will be carried out for n rounds, each round each cat must change its own ribbon strictly "one" color (in its own round) to make it different from the previous random color. For example, aaab can become acab in a round. After the n-round game, the owner of the ribbon with the most beautiful value wins the game.
Input: The
first line contains an integer n, representing the round number.
The next three rows contain three ribbons of Ku, S, and Ka, one ribbon in each row. Each ribbon is a string containing no more than 1e5 of uppercase and lowercase letters and is not an empty string. To be fair, ensure that the three ribbons are the same length. Note that uppercase and lowercase letters are considered to represent different colors.
Output:
Output the winner's name ("Kuro", "Shiro" or "Katie"), and if at least two cats have the same beauty value, then output "Draw".

Idea: If n is less than or equal to the number of remaining letters, just add n to the number of selected letters to get the score.
If after replacing all letters with the selected letter, the remaining number of replacements n is even, we can choose an arbitrary letter, replace it with another letter, replace it, and repeat the action until n reaches 0.
Otherwise, we will not replace all other letters. Instead, we will replace these letters until there is 1 letter left (now n is even) and then replace that letter with another letter that is different from the letter we selected. After that, replace the letter with the letter of our choice.
Now that n is even again, we repeat the actions discussed above.

If and only if a string is all a certain letter, and n = 1, you need to consider the "can't change back" case, the answer at this time becomes the string length-1


```#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
int n;
using namespace std;
const int N = 100;
typedef pair<int, char> PIC;
int get_ans(string s){
 map<char, int> m;
 for (int i = 0; i < (int)s.size(); i ++)
    m[s[i]] ++;
       PIC S(0, '0');
    for (int i = 50; i < 150; i ++)
      if (m[char(i)] > S.first)
       S.first = m[char(i)], S.second = char(i);
          if (n <= (int)s.length() - S.first)   return S.first + n;
    else{
     if ((S.first == (int)s.length())&& n == 1)   return s.length() - 1;
        else    return s.length(); 
 }
}
int main(){
 cin >> n;
  string s1, s2, s3;
 cin >> s1 >> s2 >> s3;
  int ans1 = get_ans(s1);
 int ans2 = get_ans(s2);
 int ans3 = get_ans(s3);
  if(( ans1 > ans2) && (ans1 > ans3)) puts("Kuro");
    else if((ans2 > ans1) && (ans2 > ans3)) puts("Shiro");
    else if((ans3 > ans1) && (ans3 > ans2)) puts("Katie");
    else puts("Draw");
    return 0;
} 
164 original articles published · Like 112 · Visits 6768

Guess you like

Origin blog.csdn.net/qq_45772483/article/details/105477513