HDU 5559 Frog and String (构造+找规律)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5559


题面:

Frog and String

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 306    Accepted Submission(s): 97
Special Judge


Problem Description
Frog studies algorithms on strings. He finds it so interesting that he can't stop playing with his strings. These days he has just learnt about palindrome, and comes up with a problem about it.

Given two integers, N and M , he wants to construct a string of length N , whose substrings contain exactly M distinct non-empty palindromes. A palindrome is a string which is exactly the same as the reverse of itself. For example, “ABBA”, “ADA”, “A”, and “UUSSUU” are palindromes, but “USTC”, “AB”, and “ABC” are not. A substring is a consecutive part of the original string. For example, “US”, “USTC”, “STC”, and “TC” are substrings of “USTC”, but “UC” and “CT” are not.

Frog finds it too hard for him to solve this problem. So he asks you for help. BTW, he won't make it too easy for you, so he decided to ask you solve this problem under his restrictions. You can only use the first K capital letters in the English alphabet (AZ) . Please write a program to solve this problem.
 

Input
There is an integer T in the first line indicating the number of total test cases. ( T20000 ). Each test case contains three integers N,M, and K , ( 1N,M100000,1K26 ), separated by single spaces. We guarantee the sum of N will not exceed 2000000.
 

Output
For each test case, output a single line consisting of “ Case #X:” first, where X is the test case number starting from 1. Output the string that you find in the next line. The string should contain only the first K capital letters. If there are multiple solutions, you can output any of them. If there is no such string satisfying Frog’s requirements, output “ Impossible” instead. Please follow the output format exactly, and do not output any additional character or new line.
 

Sample Input
  
  
4 3 3 3 4 4 4 2 2 1 2 1 1
 

Sample Output
  
  
Case #1: ABA Case #2: ABCD Case #3: AA Case #4: Impossible
Hint
For the first test case, “A”, “ABA”, “B” are the all distinct palindrome substrings of “ABA”. There are other possible answers, such as “BAB” and “AAA”. For the second test case, “USTC” is not a valid answer, because it contains letters other than the first 4 capital letters.
 

Source
2015ACM/ICPC亚洲区合肥站-重现赛(感谢中科大)

题意:
    给定N,M,K。问是否能够利用前K种字母(可以不全用)构造一个长度为N,含有M个不同的回文字串的串。

解题:
    分类讨论。
    k==1
       只有N=M才可以,全A。
    k==2
       N>M:
          存在特殊情况(N=8,M=7)的时候,输出“ AABABBAA“,据说是打表出来的。我是找不到的....
          其余情况按照 (M-8个A)+ABAABB”的方式构造即可。(这么奇特的序列,我也是想不到的233333333)
      N=M: AAA...
      N<M: Impossible
    k>=3
       N>M:
            以(M-3个A)+“ABCABC...”的方式构造,这种情况还是比较好想到的,因为除了第一组ABC可以提供三个有效回文字串外,后续的ABC..都是无效的,因此可以算出还需有效的为M-3(即为ABC之前的A的数量),再用N-(M-3)即可得ABC序列的数量。
       N=M:同上
       N<M:同上

代码:

#include <iostream>
#include <cstdio>
using namespace std;
char mod[10]={"ABAABB"};
int main()
{
    int t,n,m,k,cnt=1;
	scanf("%d",&t);
    while(t--)
	{
		printf("Case #%d:\n",cnt++);
		scanf("%d%d%d",&n,&m,&k);
		if(k==1)
		{
			if(n==m)
		    {
				for(int i=0;i<n;i++)
					printf("A");
				puts("");
			}
			else
            {
                printf("Impossible\n");
			}
		}
		else if(k==2)
		{
			if(n==m)
			{
				for(int i=0;i<n;i++)
					printf("A");
				printf("\n");
			}
			else if(n<m)
				printf("Impossible\n");
			else
			{
				if(n==8&&m==7)
					puts("AABABBAA");
				else if(m<8)
					puts("Impossible");
				else
				{
                   for(int i=0;i<m-8;i++)
					   printf("A");
				   for(int i=0;i<n-m+8;i++)
					   printf("%c",mod[i%6]);
				   printf("\n");
				}
			}
		}
		else
		{
			
			if(n==m)
			{
				for(int i=0;i<n;i++)
					printf("A");
				printf("\n");
			}
			else if(n<m)
				printf("Impossible\n");
			else
			{
				if(m<3)
			    {
				  printf("Impossible\n");
				  continue;
			    }
				for(int i=0;i<m-3;i++)
				  printf("A");
				for(int i=0;i<n-m+3;i++)
				{
					if(i%3==0)
						printf("A");
					else if(i%3==1)
						printf("B");
					else
						printf("C");
				}
				printf("\n");
			}
		}
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值