CodeForces -802HH. FakeNews (medium)

CodeForces -802HH. FakeNews (medium)

Thanks to yourhelp, Heidi is confident that no one can fool her. She has now decided to postsome fake news on the HC2 Facebook page. However, she wants to beable to communicate to the HC2 committee that the post is fake,
using some secret phrase hidden in the post as a subsequence. To make thismethod foolproof, she wants the phrase to appear n times in the post.
She is asking you to design a post (string) s and a hidden phrase p
such that p appears in s as a subsequence exactly n times.

Input

The first and only
line of input contains a single integer n (1 ≤ n ≤ 1 000 000).

Output

The output should
contain two nonempty strings s and p separated by a single space.
Each string should be composed of letters (a-z and A-Z: both lowercase and
uppercase are allowed) and have length at most 200. The number of occurrences
of p in s as a subsequence should be exactly n. If there
are many possible solutions, output any of them. It is guaranteed that at least
one solution exists.

Examples

Input

2

Output

hHheidi Hei

Input

4

Output

bbbba ba

Input

6

Output

aaabb ab

这个题目我自己做的时候没有想出来,看了网上其他人写的博客,自己就也想总结一下。

先说我看的别的写的思路:

在这里插入图片描述

假设中间状态s=pu,这种情况下,p在s中作为子序列的出现次数恰好等于k,然后如果想使p在s中作为子序列的出现次数恰好等于2k+1或者2k+2,就可以按照上面的方法构造。

所以这个算法的时间复杂度为ln(n)。

#include<bits/stdc++.h>
using namespace std;
string p,s;
int n;
void dfs(int n,char c){
 if (n==1)
 {
  s+=c,p+=c;
  return;//n=1的时候初始情况就是 s=a c=a 
 }
 else if (n==2)
 {
  s+=c,s+=c,p+=c;//n=2的时候的初始情况是 s=aa c=a 
  return;
 }
 if (n%2)
 {
  dfs(n/2,c+1);
  s=p+c+s.substr(p.size(),s.size()-p.size()+1)+c+c,p+=c;
 }
 else
 {
  dfs(n/2-1,c+1);
  s=p+c+c+s.substr(p.size(),s.size()-p.size()+1)+c+c,p+=c;
 }
}
int main(){
 scanf("%d",&n);
 dfs(n,'a');
 cout<<s<<' '<<p;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值