C. Prefixes and Suffixes
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Ivan wants to play a game with you. He picked some string s of length n 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 1 to n-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-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 n (2 \le n \le 100) — the length of the guessed string s.
The next 2n-2 lines are contain prefixes and suffixes, one per line. Each of them is the string of length from 1 to n-1 consisting only of lowercase Latin letters. They can be given in arbitrary order.
It is guaranteed that there are exactly 2 strings of each length from 1 to n-1. It is also guaranteed that these strings are prefixes and suffixes of some existing string of length n.
Output
Print one string of length 2n-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 i-th character of this string should be 'P' if the i-th of the input strings is the prefix and 'S' otherwise.
If there are several possible answers, you can print any.
Examples
input
Copy
5
ba
a
abab
a
aba
baba
ab
aba
output
Copy
SPPSPSPS
input
Copy
3
a
aa
aa
a
output
Copy
PPSS
input
Copy
2
a
c
output
Copy
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.
题意:
给你某个字符串的长度n,再给它的n-1个前缀或者n-1个后缀
让你判断那些是前缀那些是后缀
关键给你的前缀和后缀是1到n-1的都有两个
分析:
只要判断两个长度为n-1的字符串哪一个为前缀哪一个为后缀即可,然后进行枚举。
#include<cstdio>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<numeric>
#include<cctype>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
using namespace std;
#define N 100+5
#define rep(i,n) for(int i=0;i<n;i++)
#define sd(n) scanf("%d",&n)
#define sll(n) scanf("%lld",&n)
#define pd(n) scanf("%d\n",n)
#define pll(n) scanf("%lld\n",n)
#define MAX 26
typedef long long ll;
const ll mod=1e6;
string s[210];
bool vis[110];
int main(){
int n;
while(~scanf("%d",&n))
{
bool flag=0;
string s1,s2,pre;
for(int i=0;i<2*n-2;i++)
{
cin>>s[i];
if(s[i].size()==n-1&&!flag)
{
s1=s[i];
flag=1;
}
else if(s[i].size()==n-1) s2=s[i];
}
int cnt=0;
for(int i=0;i<2*n-2;i++)
if(s1.substr(0,s[i].size())==s[i]&&s[i]!=s2) cnt++;
//若前缀超过n-1,且s1的最长后缀等于s2的最长前缀,说明s1是前缀
if(cnt>=n-1&&s1.substr(1,s1.size()-1)==s2.substr(0,s2.size()-1))
pre=s1;
else pre=s2;//否则s2为前缀
memset(vis,0,sizeof(vis));//标记相同长度的是否已经是前缀
for(int i=0;i<2*n-2;i++)
if(s[i]==pre.substr(0,s[i].size())&&!vis[s[i].size()]) {
printf("P"); vis[s[i].size()]=1;
}
else printf("S");
}
return 0;
}