ACM-ICPC Asia Beijing Regional Palindromes

Recently, Nvoenewr learnt palindromes in his class.

A palindrome is a nonnegative integer that is the same when read from left to right and when read from right to left. For example, 0, 1, 2, 11, 99, 232, 666, 998244353353442899 are palindromes, while 10, 23, 233, 1314 are not palindromes.

Now, given a number, Nvoenewr can determine whether it’s a palindrome or not by using loops which his teacher has told him on the class. But he is now interested in another question: What’s the K-th palindrome? It seems that this question is too difficult for him, so now he asks you for help.

Nvoenewr counts the number from small to big, like this: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101 and so on. So the first palindrome is 0 and the eleventh palindrome is 11 itself.
Nvoenewr may ask you several questions, and the K may be very big.

Input
The first line contains one integer T(T <= 20) —— the number of questions that Nvoenewr will ask you.

Each of the next T lines contains one integer K. You should find the K-th palindrome for Nvoenewr.

Let’s say K is a n-digit number. It’s guaranteed that K >= 1, 1 <= n <= 100000 and the sum of n in all T questions is not greater than 1000000.

Output
Print T lines. The i-th line contains your answer of Nvoenewr’s i-th question.

Sample Input
4
1
10
11
20
Sample Output
0
9
11
101


题目大意:给你一个k,输出第k大的回文串。

看着比较难,但是如果我们打表之后,就能发现一些规律。

对于个位数和10,11就特判一下然后对于首个数字不是1的k,它对应的回文串就是将首数字减一,在反转顺序贴到后面如k=523,则它对应的回文串就是42324,(注意,这种情况下,一定是将0-len-1位反转贴到后面,即中间要留一个)对于首个数字是1的k,是将它的首数字1直接抛弃,选取第2-len位如果第2位数字不是0,就是将2-len位反转后贴到后面如果第2位数字是0,先将0替换成9,再将2~len-1位反转后贴到后面(即这时中间要留一个)。


AC代码:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
//#define int long long
using namespace std;
int T,k;
string str;
signed main(){
	cin>>T;
	while(T--){
		cin>>str; int len=str.size();
		if(str=="10")	puts("9");
		else if(str=="11")	puts("11");
		else if(len==1)	cout<<char(str[0]-1)<<endl;
		else if(str[0]=='1'){
			if(str[1]=='0'){
				str[1]='9';
				string a=str.substr(1,len-1);//
				string b=str.substr(1,len-2);
				reverse(b.begin(),b.end());
				a+=b;
				cout<<a<<endl;
			}else{
				string a=str.substr(1,len-1);//
				string b=a;
				reverse(b.begin(),b.end());
				a=a+b;
				cout<<a<<endl;
			}
		}else{
			str[0]-=1;
			string a=str.substr(0,len-1);
			reverse(a.begin(),a.end());
			str+=a;
			cout<<str<<endl;
		}
	}
	return 0;
}
全部评论

相关推荐

10-13 22:56
门头沟学院 C++
rt,鼠鼠的浪潮网签明天过期,鼠鼠是山东人,好像自己也能接受。之前的面试大厂基本挂干净了,剩下小米二面后在泡,问了下面试官没有挂,但要泡。还有海信似乎也通过了,不过在深圳,鼠鼠也不是很想去。其它还有一些公司应该陆陆续续还有一些面试,现在有些纠结是直接签了还是再等再面呢?大佬们能不能给鼠鼠提一些意见,万分感谢!!!
牛客78696106...:浪潮可不是开摆,当初我还是开发的时候我组长跟我说他们组有段时间天天1,2点走,早上5点就来,全组肝出来心肌炎,浪潮挣钱省立花可不是说说,当然也看部门,但是浪潮普遍就那dio样,而且你算下时薪就知道不高,没事也是9点半走,不然算你旷工
投递小米集团等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务