[2018-4-20]BNUZ套题比赛div2 CodeForces 548A Mike and Fax【补】

本文介绍了一道算法题目,需要判断给定的字符串是否能由相同长度的多个回文子串组成。通过等长分割字符串,并验证每个子串是否为回文串,最终确定是否符合题目要求。
A. Mike and Fax
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

While Mike was walking in the subway, all the stuff in his back-bag dropped on the ground. There were several fax messages among them. He concatenated these strings in some order and now he has string s.

He is not sure if this is his own back-bag or someone else's. He remembered that there were exactly k messages in his own bag, each was a palindrome string and all those strings had the same length.

He asked you to help him and tell him if he has worn his own back-bag. Check if the given string s is a concatenation of k palindromes of the same length.

Input

The first line of input contains string s containing lowercase English letters (1 ≤ |s| ≤ 1000).

The second line contains integer k (1 ≤ k ≤ 1000).

Output

Print "YES"(without quotes) if he has worn his own back-bag or "NO"(without quotes) otherwise.

Examples
input
Copy
saba
2
output
Copy
NO
input
Copy
saddastavvat
2
output
Copy
YES
Note

Palindrome is a string reading the same forward and backward.

In the second sample, the faxes in his back-bag can be "saddas" and "tavvat".

题意及题解给个字符串 和 回文串数量, 长度为回文长度(字符串长度除以所给回文串数量可得)来分割这个字符串(等长分割), 判断分割出来的回文串是不是都是回文串 回文长度 都要 相等。ps: 中间不存在多余 字符的存在, 判 字符串长度 不能整除 回文串数量 时,直接输出no 

AC代码:

#include <bits/stdc++.h>

using namespace std;

string a;
int b;

int pd(int sta, int endd) {//计算回文串 回文长度,不是回文串返回0
	int i = sta, j = endd, cnt = 0;
	while (i <= j) {
		if (a[i] == a[j]) {
			cnt++;
		} else {
			return 0;
		}
		i++;
		j--;
	}
	return cnt;
}

int main() {
	set<int> s;
	int cnt = 0;
	cin >> a >> b;
	if (a.size() % b != 0) //不能整除说明一定不是他的包
		printf("NO\n");
	else if (a.size() >= b) {
		int len = a.size() / b;//分割后每节字符串的长度
		for (int i = 0; i < b; i++) {
			int p = pd(i * len, (i + 1) * len - 1);
			if (p != 0) {//是回文的情况
				cnt++;
				s.insert(p);
				if (s.size() > 1) {//集合中不只存在一个元素,证明有不同长度的回文串,根据题意不是他的包
					printf("NO\n");
					return 0;
				}
			} else {//不是回文的情况 输出no
				printf("NO\n");
				return 0;
			}
		}
		if (cnt == b)
			printf("YES\n");
		else
			printf("NO\n");
	} else //字符串长度比b小证明一定不匹配
		printf("NO\n");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值