【Codeforces 1421 C】Palindromifier,构造方案,构造回文串

举报
小哈里 发表于 2022/05/10 23:47:59 2022/05/10
【摘要】 problem C. Palindromifier time limit per test1 second memory limit per test256 megabytes inputstandar...

problem

C. Palindromifier
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Ringo found a string s of length n in his yellow submarine. The string contains only lowercase letters from the English alphabet. As Ringo and his friends love palindromes, he would like to turn the string s into a palindrome by applying two types of operations to the string.

The first operation allows him to choose i (2≤i≤n−1) and to append the substring s2s3…si (i−1 characters) reversed to the front of s.

The second operation allows him to choose i (2≤i≤n−1) and to append the substring sisi+1…sn−1 (n−i characters) reversed to the end of s.

Note that characters in the string in this problem are indexed from 1.

For example suppose s=abcdef. If he performs the first operation with i=3 then he appends cb to the front of s and the result will be cbabcdef. Performing the second operation on the resulted string with i=5 will yield cbabcdefedc.

Your task is to help Ringo make the entire string a palindrome by applying any of the two operations (in total) at most 30 times. The length of the resulting palindrome must not exceed 106

It is guaranteed that under these constraints there always is a solution. Also note you do not have to minimize neither the number of operations applied, nor the length of the resulting string, but they have to fit into the constraints.

Input
The only line contains the string S (3≤|s|≤105) of lowercase letters from the English alphabet.

Output
The first line should contain k (0≤k≤30) — the number of operations performed.

Each of the following k lines should describe an operation in form L i or R i. L represents the first operation, R represents the second operation, i represents the index chosen.

The length of the resulting palindrome must not exceed 106.

Examples
inputCopy
abac
outputCopy
2
R 2
R 5
inputCopy
acccc
outputCopy
2
L 4
L 2
inputCopy
hannah
outputCopy
0
Note
For the first example the following operations are performed:

abac → abacab → abacaba

The second sample performs the following operations: acccc → cccacccc → ccccacccc

The third example is already a palindrome so no operations are required.

solution

/*
题意:
+ 给出一个长为n的字符串,可以进行两种操作,构造小于30次的方案使字符串回文(长度小于106)
+ 操作1,选择i并将si,si-1...s2附加到前面。操作2,选择i并将si,,,sn-1附加到后面
思路:
+ 放宽条件容易想到abc的反向直接加到左边形成cbaabc,但是两边的字符不能复制
+ 可以先R(n-1),最后L(2)一下,就间接的完成了这一操作,所以3次就行。
+ e.g. abcde->abcde+d->edcba+abcde+d->d+edcba+abcde+d
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 110;
int main(){
	ios::sync_with_stdio(false);
	string s;  cin>>s;
	int ok = 1;
	for(int i = 0; i < s.size(); i++)
		if(s[i]!=s[s.size()-1-i])ok = 0;
	if(ok==1)cout<<"0\n";
	else{
		cout<<"3\n";
		cout<<"R "<<s.size()-1<<"\n";
		cout<<"L "<<s.size()<<"\n";
		cout<<"L "<<2<<"\n";
	}
	return 0;
}



  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

文章来源: gwj1314.blog.csdn.net,作者:小哈里,版权归原作者所有,如需转载,请联系作者。

原文链接:gwj1314.blog.csdn.net/article/details/113473276

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。