C. Prefixes and Suffixes
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 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
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-1的兩個字串,例第一個:abab和baba,那麼原始串的可能性只有兩個:abab+baba的最後一個=ababa,或者是baba+abab的最後一個=babab,隨便拿一個出來,然後將題目給的那些串一個個在a上是否是字首或字尾,是continue,若都不是,a就是另一種情況了::AC程式碼:
#include<bits/stdc++.h>
using namespace std;
string s1,s2,s[200],a;
bool vis[200];
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=2*n-2;i++)
{
cin>>s[i];
if(s[i].size()==n-1)
{
if(s1.size()==0) s1=s[i];
else s2=s[i];
}
}
bool flag=0;
a=s1+s2[s2.size()-1];
for(int i=1;i<=2*n-2;i++)
{
string te=a.substr(0,s[i].size());//a中長度與S[i]相同的字首
string tw=a.substr(a.size()-s[i].size(),a.size());//a中長度與S[i]相同的字尾
if(te==s[i]||tw==s[i]) continue;
else {
flag=1;break;
}
}
if(flag) a=s2+s1[s1.size()-1];
for(int i=1;i<=2*n-2;i++)
{
string te=a.substr(0,s[i].size());
if(te==s[i]&&!vis[s[i].size()]) printf("P"),vis[s[i].size()]=1;
else printf("S");
}
puts("");
}