HDU 4731 Minimum palindrome

本文解析了一道关于设置最安全回文密码的算法题目,通过寻找规律来确定由特定数量字母组成的最短长度回文串,以确保密码的安全性,并提供了AC代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


Minimum palindrome

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1439    Accepted Submission(s): 600



Problem Description

Setting password is very important, especially when you have so many "interesting" things in "F:\TDDOWNLOAD".
We define the safety of a password by a value. First, we find all the substrings of the password. Then we calculate the maximum length of those substrings which, at the meantime, is a palindrome.
A palindrome is a string that will be the same when writing backwards. For example, aba, abba,abcba are all palindromes, but abcab, abab are not.
A substring of S is a continous string cut from S. bcd, cd are the substrings of abcde, but acd,ce are not. Note that abcde is also the substring of abcde.
The smaller the value is, the safer the password will be.
You want to set your password using the first M letters from the alphabet, and its length should be N. Output a password with the smallest value. If there are multiple solutions, output the lexicographically smallest one.
All the letters are lowercase.
 

Input

The first line has a number T (T <= 15) , indicating the number of test cases.
For each test case, there is a single line with two integers M and N, as described above.(1 <= M <= 26, 1 <= N <= 105)
 

Output

For test case X, output "Case #X: " first, then output the best password.
 

Sample Input

  
2 2 2 2 3
 

Sample Output

  
Case #1: ab Case #2: aab
 

Source

 

Recommend

liuyiding

思路:就是找规律。尼玛,比赛时以为M个字母全要用上,吐血啊!当M=1时,肯定全是a;当M>2时,是abc循环使用;困难的是M=2时,其实写个暴力程序打表出来,然后找规律即可。如下所示。


打表代码如下:

#include <bits/stdc++.h>
using namespace std;
int num[40];

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	for (int i=1; i<=25; ++i){
		int maxid = i;
		int ans = 0;
		for (int j=0; j<(1<<i); ++j){
			int t = j;
			for (int k=0; k<i; ++k){
				num[k] = t&1;
				t >>= 1;
			}
			int minid = 0;
			for (int k=0; k<i; ++k){
				for (int s=0; k-s>=0&&k+s<i; ++s){
					if (num[k-s] != num[k+s])
						break;
					if (s*2+1 > minid)
						minid = s*2+1;
				}
				for (int s=0; k-s>=0&&k+s+1<i; ++s){
					if (num[k-s] != num[k+s+1])
						break;
					if (s*2+2 > minid)
						minid = s*2+2;
				}
			}
			if (minid < maxid){
				maxid = minid;
				ans = j;
			}
		}
		for (int j=i-1; j>=0; --j){
			num[j] = ans&1;
			ans >>= 1;
		}
		cout << i << ": ";
		for (int j=0; j<i; ++j)
			cout << char(num[j]+'a');
		cout << endl;
	}
	return 0;
}

附上AC代码:

#include <bits/stdc++.h>
using namespace std;
int n, m;

int main(){
	int T, cas=0;
	scanf("%d", &T);
	while (T--){
		scanf("%d%d", &m, &n);
		printf("Case #%d: ", ++cas);
		if (m == 1)
			while (n--)
				putchar('a');
		else if (m == 2){
			if (n == 1)
				putchar('a');
			else if (n == 2)
				printf("ab");
			else if (n == 3)
				printf("aab");
			else if (n == 4)
				printf("aabb");
			else if (n == 5)
				printf("aaaba");
			else if (n == 6)
				printf("aaabab");
			else if (n == 7)
				printf("aaababb");
			else if (n == 8)
				printf("aaababbb");
			else{
				printf("aa");
				int t = (n-2)/6;
				while (t--)
					printf("aababb");
				t = (n-2)%6;
				if (t == 1)
					putchar('a');
				else if (t == 2)
					printf("aa");
				else if (t == 3)
					printf("aaa");
				else if (t == 4)
					printf("aaaa");
				else if (t == 5)
					printf("aabab");
			}
		}
		else{
			int t = n/3;
			while (t--)
				printf("abc");
			t = n%3;
			if (t == 1)
				putchar('a');
			else if (t == 2)
				printf("ab");
		}
		puts("");
	}
	return 0;
}


资源下载链接为: https://pan.quark.cn/s/d9ef5828b597 在本文中,我们将探讨如何通过 Vue.js 实现一个带有动画效果的“回到顶部”功能。Vue.js 是一款用于构建用户界面的流行 JavaScript 框架,其组件化和响应式设计让实现这种交互功能变得十分便捷。 首先,我们来分析 HTML 代码。在这个示例中,存在一个 ID 为 back-to-top 的 div 元素,其中包含两个 span 标签,分别显示“回到”和“顶部”文字。该 div 元素绑定了 Vue.js 的 @click 事件处理器 backToTop,用于处理点击事件,同时还绑定了 v-show 指令来控制按钮的显示与隐藏。v-cloak 指令的作用是在 Vue 实例渲染完成之前隐藏该元素,避免出现闪烁现象。 CSS 部分(backTop.css)主要负责样式设计。它首先清除了一些默认的边距和填充,对 html 和 body 进行了全屏布局,并设置了相对定位。.back-to-top 类则定义了“回到顶部”按钮的样式,包括其位置、圆角、阴影、填充以及悬停时背景颜色的变化。此外,与 v-cloak 相关的 CSS 确保在 Vue 实例加载过程中隐藏该元素。每个 .page 类代表一个页面,每个页面的高度设置为 400px,用于模拟多页面的滚动效果。 接下来是 JavaScript 部分(backTop.js)。在这里,我们创建了一个 Vue 实例。实例的 el 属性指定 Vue 将挂载到的 DOM 元素(#back-to-top)。data 对象中包含三个属性:backTopShow 用于控制按钮的显示状态;backTopAllow 用于防止用户快速连续点击;backSeconds 定义了回到顶部所需的时间;showPx 则规定了滚动多少像素后显示“回到顶部”按钮。 在 V
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值