After the big birthday party, Katie still wanted Shiro to have some more fun. Later, she came up with a game called treasure hunt. Of course, she invited her best friends Kuro and Shiro to play with her.
The three friends are very smart so they passed all the challenges very quickly and finally reached the destination. But the treasure can only belong to one cat so they started to think of something which can determine who is worthy of the treasure. Instantly, Kuro came up with some ribbons.
A random colorful ribbon is given to each of the cats. Each color of the ribbon can be represented as an uppercase or lowercase Latin letter. Let's call a consecutive subsequence of colors that appears in the ribbon a subribbon. The beauty of a ribbon is defined as the maximum number of times one of its subribbon appears in the ribbon. The more the subribbon appears, the more beautiful is the ribbon. For example, the ribbon aaaaaaa has the beauty of 7
because its subribbon a appears 7 times, and the ribbon abcdabc has the beauty of 2because its subribbon abc appears twice.
The rules are simple. The game will have n
turns. Every turn, each of the cats must change strictly one color (at one position) in his/her ribbon to an arbitrary color which is different from the unchanged one. For example, a ribbon aaab can be changed into acab in one turn. The one having the most beautiful ribbon after nturns wins the treasure.
Could you find out who is going to be the winner if they all play optimally?
The first line contains an integer n
) — the number of turns.
Next 3 lines contain 3 ribbons of Kuro, Shiro and Katie one per line, respectively. Each ribbon is a string which contains no more than 105
uppercase and lowercase Latin letters and is not empty. It is guaranteed that the length of all ribbons are equal for the purpose of fairness. Note that uppercase and lowercase letters are considered different colors.
Print the name of the winner ("Kuro", "Shiro" or "Katie"). If there are at least two cats that share the maximum beauty, print "Draw".
三个人有三个等长字符串,没人每次可以更改自己字符串中的一个字母,给出可修改次数n,求问最后哪个人的字符串里,重复最多的子串的重复数最多,如果有并列第一第二(第三)的就平局。
一个串重复次数=这个串里每个字母的重复次数,所以最优是选单个字母重复最多。
首先统计串中出现次数最多的字母计数val:
~~若val+n大于字符串长度len,则beauty=len;
~~若val+n小于等于len,则beauty=val+n;
~~特殊情况,若val==len,“aaaaaaaa”,且n==1,最后必然会有一个a变成别的字母,“baaaaaaa”。
即若val==len && n==1 beauty=val--;
代码如下
#include
#include
#include
#include
#include
#include
#include
using namespace std;
long long n;
char str[3][100005];
int len;
int book[3][300];
struct mmax{
int val;
char *name;
}m[3]={{0,"Kuro"},{0,"Shiro"}, {0,"Katie"}};
bool cmp(mmax a, mmax b){
return a.val>b.val;
}
int main(){
cin>>n;
for(int i=0;i<3;i++){
cin>>str[i];
}
len=strlen(str[0]);
memset(book,sizeof(book),0);
for(int i=0;i<3;i++){
for(int j=0;jlen) m[i].val=len;
}
sort(m,m+3,cmp);
if(m[0].val==m[1].val){
cout<<"Draw"<