Codeforces Round #527 (Div. 3) C. Prefixes and Suffixes (string+multiset+思維)
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的字串的所有n-1個字首和n-1個字尾,要求判斷每個子串是字首還是字尾
題解:首先按字串長度排序,可知相同長度的一定一個是字首一個是字尾(題目保證),可以列舉最後兩個哪個是字首,然後構造出答案串,再用multiset儲存所有答案串的字首和字尾判斷是否和輸入的一致即可,確定最後兩個哪個是字首串之後便可以確定每一個是否是字首還是字尾。
#include<bits/stdc++.h>
using namespace std;
#define Sheryang main
const int maxn=1e6+7;
typedef long long ll;
const int mod=1e9+7;
ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c))c = getchar();if(c == '-')Nig = -1,c = getchar();while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();return Nig*x;}
#define read read()
pair<string,int>p[maxn];
int nn,n;
int cmp(pair<string,int> a,pair<string,int> b){
return a.first.length()<b.first.length();
}
char ans[maxn];
multiset<string>sst;
bool judge(string pre,string suf){
string s=pre+suf.substr(suf.length()-1);
multiset<string>st;
for(int i=0;i<n-1;i++){
st.insert(s.substr(0,i+1));
st.insert(s.substr(i+1));
}
if(st!=sst) return false;
for(int i=1;i<=nn;i+=2){
string ss=s.substr(0,p[i].first.length());
string sss=s.substr(n-p[i].first.length());
if(p[i].first==ss && p[i+1].first==sss){
ans[p[i].second]='P';ans[p[i+1].second]='S';
}else{
ans[p[i+1].second]='P';ans[p[i].second]='S';
}
}
return true;
}
int Sheryang(){
n=read;
nn=2*n-2;
for(int i=1;i<=nn;i++){
cin>>p[i].first;
p[i].second=i;
sst.insert(p[i].first);
}
sort(p+1,p+1+nn,cmp);
if(judge(p[nn].first,p[nn-1].first)){
for(int i=1;i<=nn;i++){
printf("%c",ans[i]);
}
}else if(judge(p[nn-1].first,p[nn].first)){
for(int i=1;i<=nn;i++){
printf("%c",ans[i]);
}
}
return 0;
}