Codeforces Round #527 (Div. 3) C

                                                                              C. Prefixes and Suffixes

Ivan wants to play a game with you. He picked some string ss of length nn consisting only of lowercase Latin letters.

You don't know this string. Ivan has informed you about all its improper prefixes and suffixes (i.e. prefixes and suffixes of lengths from 11 to n−1n−1), but he didn't tell you which strings are prefixes and which are suffixes.

Ivan wants you to guess which of the given 2n−22n−2 strings are prefixes of the given string and which are suffixes. It may be impossible to guess the string Ivan picked (since multiple strings may give the same set of suffixes and prefixes), but Ivan will accept your answer if there is at least one string that is consistent with it. Let the game begin!

Input

The first line of the input contains one integer number nn (2≤n≤1002≤n≤100) — the length of the guessed string ss.

The next 2n−22n−2 lines are contain prefixes and suffixes, one per line. Each of them is the string of length from 11 to n−1n−1 consisting only of lowercase Latin letters. They can be given in arbitrary order.

It is guaranteed that there are exactly 22 strings of each length from 11 to n−1n−1. It is also guaranteed that these strings are prefixes and suffixes of some existing string of length nn.

Output

Print one string of length 2n−22n−2 — the string consisting only of characters 'P' and 'S'. The number of characters 'P' should be equal to the number of characters 'S'. The ii-th character of this string should be 'P' if the ii-th of the input strings is the prefix and 'S' otherwise.

If there are several possible answers, you can print any.

Examples

input

5
ba
a
abab
a
aba
baba
ab
aba

output

SPPSPSPS

input

3
a
aa
aa
a

output

PPSS

input

2
a
c

output

PS

Note

The only string which Ivan can guess in the first example is "ababa".

The only string which Ivan can guess in the second example is "aaa". Answers "SPSP", "SSPP" and "PSPS" are also acceptable.

In the third example Ivan can guess the string "ac" or the string "ca". The answer "SP" is also acceptable.

 

题意:给出一个字符串的所有前缀和后缀(并未说明是前缀还是后缀),按顺序输出是前缀(P)还是后缀(S)。思路就是找的最长的前缀和后缀,让俩前后组合两次,数P和S的个数是否相同

#include<iostream>
#include<memory.h> 
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
string ss[400];
int List[4000];
char cc[200];
int main()
{
	string start = "", end = "";
	int n, P = 0, S = 0;
	
	memset(List, 0, sizeof(List));
	scanf("%d", &n);
	for(int i = 0;i < 2 * n - 2;i ++)
		cin >> ss[i];
	for(int i = 0;i < 2 * n - 2;i ++)
	{
		if(ss[i].size() == n - 1 && start == "")
			start = ss[i];
		else if(ss[i].size() == n - 1 && start != "")
			end = ss[i];
	}
	string str = start + end[end.size() - 1];
	for(int i = 0;i < 2 * n - 2;i ++)
	{
		if(List[ss[i].size()] != 1 && ss[i] == str.substr(0, ss[i].size()))
		{
			cc[i] = 'P';
			List[ss[i].size()] = 1;
			P ++;
		}
		else if(ss[i] == str.substr(str.size() - ss[i].size(), ss[i].size()))
		{
			cc[i] = 'S';
			S ++;
		}
	}
	if(P == S && S == n - 1)
	{
		for(int i = 0;i < 2 * n - 2;i ++)
			printf("%c", cc[i]);
		cout << endl;
		return 0;
	}
	str = end + start[end.size() - 1];
	memset(List, 0, sizeof(List));
	for(int i = 0;i < 2 * n - 2;i ++)
	{
		if(List[ss[i].size()] != 1 && ss[i] == str.substr(0, ss[i].size()))
		{
			cc[i] = 'P';
			List[ss[i].size()] = 1;
			P ++;
		}
		else if(ss[i] == str.substr(str.size() - ss[i].size(), ss[i].size()))
		{
			cc[i] = 'S';
			S ++;
		}
	}
	for(int i = 0;i < 2 * n - 2;i ++)
		printf("%c", cc[i]);
	cout << endl;
	return 0; 
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值