概述
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;
}
最后
以上就是含糊小天鹅为你收集整理的Codeforces Round #527 (Div. 3) C的全部内容,希望文章能够帮你解决Codeforces Round #527 (Div. 3) C所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复