Saruman of Many Colours---题解

题意:给士兵涂颜色,士兵们按一个固定的序号坐在一条传送带上,传送带可以向前或向后走任意人员数,这条传送带经过一个涂颜色的屋子,而屋子里的座位数是固定的,每次只能使用一种颜色,而且每次屋内必须坐满人。问:按给出的要求给士兵涂颜色,最短的移动次数是多少?不能按要求给所有的士兵涂颜色的话,就要输出-1。

 

思路:1.首先判断什么情况下不能按要求给所有人涂上相应的颜色。

经判断,当所给出的要求里的最长的相同字串的长度没有屋子里的座位数大时,就不能。

2.首先找到一个长度大于或等于屋子里座位数的字符串,从整个串的头部和尾部分别向该串遍历给士兵涂色,即可。

 

 

代码如下:

#include<cstdio>

char a[22222];

int main(){

    int t;

    scanf("%d",&t);

    while(t--){

        int n,k;

        int count=0;

        scanf("%d%d",&n,&k);

        scanf("%s",&a);

        int sign;

        int flag=0;

        for(int i=0;i<=n-k;i++){  //从头部开始遍历给士兵涂颜色并寻找一定长度的字符串

                for(int j=i;j<i+k;j++){

                    if(a[j]==a[i]){

                        sign=j;

                    }

                    if(a[j]!=a[i])

                        break;

                }

                if(sign-i==k-1){flag=1;break;}

//若字符串的长度大于或等于屋子里座位数时,flag=1,如n=5,k=3,a=rgggg,此时,sign=3,退出循环。

                count++;

                i=sign;

        }

        int sign2;

        if(flag==0){printf("-1\n");continue;}//flag=0时,没有长度大于或等于k的字符串

//寻找该字符串的尾部字符的序号,用sign2记录,如上面sign的列子,此时,sign2=4.

        for(int i=sign;i<=n-1;i++){

            if(a[i]==a[sign])sign2=i;

            if(a[i]!=a[sign])break;

        }

//颠倒整个字符串,以使从从后向前遍历,变成,从前向后遍历。

        for(int i=0;i<n/2;i++){

                char w=a[i];

                a[i]=a[n-i-1];

                a[n-1-i]=w;

        }

//遍历代码类似前边。但要注意结束标志。

        for(int i=0;i<n-1-sign2;i++){

                int sign3;

                    for(int j=i;j<i+k;j++){

                        if(a[j]==a[i]){

                            sign3=j;

                        }

                        if(a[j]!=a[i])

                            break;

                    }

                    count++;

                i=sign3;

        }

//最后处理找到的字符串的遍历需要的次数,注意长度为sign2-sign+k.

        count+=(sign2-sign+k)/k+((sign2-sign+k)%k>0?1:0);

        printf("%d\n",count);

    }

    return 0;

}

 

原题如下:


'"For I am Saruman the Wise, Saruman Ring-maker, Saruman of Many Colours!"

'I looked then and saw that his robes, which had seemed white, were not so, but were woven of all colours. And if he moved they shimmered and changed hue so that the eye was bewildered.' - Gandalf the Grey.

And so it was that Saruman decided to brand his Uruk-hai army with the many colours that he fancied. His method of branding his army was as follows.

He straps his army of N Uruk-hai onto chairs on a conveyor belt. This conveyor belt passes through his colouring-room, and can be moved forward or backward. The Uruk-hai are numbered 0 to N-1 according to the order in which they are seated. Saruman wishes that the i'th Uruk-hai be coloured with the colour c[i].

Further, his colouring-room has space for exactly K chairs. Once the chosen K consecutive Uruk-hai are put into the room, a colour jet sprays all K of them with any fixed colour. The conveyor belt is not circular (which means that the N-1'th and the 0'th Uruk-hai are not consecutive). Note that Uruk-hai can be recoloured in this process.

Saruman wants to find out what is the minimum number of times that the jet needs to be used in order to colour his army in the required fashion. If it is not possible to colour the army in the required fashion, output -1.

Input (STDIN):

The first line contains the number of test-cases T.

Each test case consists of 2 lines. The first line contains two space-separated integers, N and K.

This is followed by a single like containing a string of length N, describing the colours of the army. The i'th character of the string denotes the colour of the i'th Uruk-hai in the army.

Output (STDOUT):

Output T lines, one for each test case containing the answer for the corresponding test case. Remember if it is not possible to colour the army as required, output -1.

Constraints:

1 <= T <= 50

1 <= K <= N <= 20,000

The string c has length exactly N and contains only the characters 'a',...,'z'.

Sample Input:

2

3 2

rgg

3 3

rgg

Sample Output:

2

-1

Notes/Explanation of Sample Input:

In the first test case, soldiers 0 and 1 can first be painted with 'r', and then soldiers 1 and 2 can be painted with 'g'.

In the second test case, since N = K, all the soldiers will only have the same color.Description

'"For I am Saruman the Wise, Saruman Ring-maker, Saruman of Many Colours!"

'I looked then and saw that his robes, which had seemed white, were not so, but were woven of all colours. And if he moved they shimmered and changed hue so that the eye was bewildered.' - Gandalf the Grey.

And so it was that Saruman decided to brand his Uruk-hai army with the many colours that he fancied. His method of branding his army was as follows.

He straps his army of N Uruk-hai onto chairs on a conveyor belt. This conveyor belt passes through his colouring-room, and can be moved forward or backward. The Uruk-hai are numbered 0 to N-1 according to the order in which they are seated. Saruman wishes that the i'th Uruk-hai be coloured with the colour c[i].

Further, his colouring-room has space for exactly K chairs. Once the chosen K consecutive Uruk-hai are put into the room, a colour jet sprays all K of them with any fixed colour. The conveyor belt is not circular (which means that the N-1'th and the 0'th Uruk-hai are not consecutive). Note that Uruk-hai can be recoloured in this process.

 

Saruman wants to find out what is the minimum number of times that the jet needs to be used in order to colour his army in the required fashion. If it is not possible to colour the army in the required fashion, output -1.

 

Input (STDIN):

The first line contains the number of test-cases T.

Each test case consists of 2 lines. The first line contains two space-separated integers, N and K.

This is followed by a single like containing a string of length N, describing the colours of the army. The i'th character of the string denotes the colour of the i'th Uruk-hai in the army.

 

Output (STDOUT):

Output T lines, one for each test case containing the answer for the corresponding test case. Remember if it is not possible to colour the army as required, output -1.

 

Constraints:

1 <= T <= 50

1 <= K <= N <= 20,000

The string c has length exactly N and contains only the characters 'a',...,'z'.

 

Sample Input:

2

3 2

rgg

3 3

rgg

 

Sample Output:

2

-1

 

Notes/Explanation of Sample Input:

In the first test case, soldiers 0 and 1 can first be painted with 'r', and then soldiers 1 and 2 can be painted with 'g'.

In the second test case, since N = K, all the soldiers will only have the same color.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值