Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor’s Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?
The book contains a single string of characters “a”, “b” and “c”. It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.
Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don’t have to maximise the length of it.
A string aa is a subsequence of a string bb if aa can be obtained from bb by deletion of several (possibly, zero or all) characters.
Input
The input consists of a single string ss (2≤|s|≤1062≤|s|≤106). The string ss consists only of characters “a”, “b”, “c”. It is guaranteed that no two consecutive characters are equal.
Output
Output a palindrome tt that is a subsequence of ss and |t|≥⌊|s|2⌋|t|≥⌊|s|2⌋.
If there are multiple solutions, you may print any of them. You don’t have to maximise the length of tt.
If there are no solutions, output a string “IMPOSSIBLE” (quotes for clarity).
Examples
Input
cacbac
Output
aba
Input
abc
Output
a
Input
cbacacacbcbababacbcb
Output
cbaaacbcaaabc
Note
In the first example, other valid answers include “cacac”, “caac”, “aca” and “ccc”.
思路:由于相同的字母不能靠在一起,而且只有3个字母,所以每四个字母肯定有两个字母相等(鸽巢原理)。这样的话我们用两个指针前后一起找四个子母中相同的字母,这样的话,出来的字符串绝对大于等于整个字符串的一半。
代码如下:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxx=1e6+100;
string s;
int vis[maxx];
int main()
{
cin>>s;
int len=s.length();
int l=0,r=len-1;
while(l<=r)
{
if(l==r)
{
vis[l]=1;
break;
}
if(s[l]==s[r]) vis[l]=vis[r]=1;
else if(s[l]==s[r-1]) vis[l]=vis[r-1]=1;
else if(s[l+1]==s[r]) vis[l+1]=vis[r]=1;
else if(s[l+1]==s[r-1]) vis[l+1]=vis[r-1]=1;
l+=2;r-=2;
}
for(int i=0;i<len;i++) if(vis[i]) cout<<s[i];
cout<<endl;
}
努力加油a啊,(o)/~