Codeforces Round #527 (Div. 3) C. Prefixes and Suffixes (思维,字符串)

时间:2024-01-16 11:06:56

Codeforces Round #527 (Div. 3)  C. Prefixes and Suffixes   (思维,字符串)

  • 题意:给你某个字符串的\(n-1\)个前缀和\(n-1\)个后缀,保证每个所给的前缀后缀长度从\([1,n-1]\)都有,问你所给的子串是前缀还是后缀.

  • 题解:这题最关键的是那两个长度为\(n-1\)的子串,我们只要判断哪个是前缀就行了,然后再遍历一遍所给的子串,用长度为\(n-1\)的前缀子串来判断是子串是前缀还是后缀.

  • 代码:

    int n;
    string s[N];
    bool vis[N];
    int cnt; int main() {
    ios::sync_with_stdio(false);cin.tie(0);
    cin>>n; string s1="";
    string s2="";
    string pre;
    for(int i=1;i<=2*n-2;++i){
    cin>>s[i];
    if(s1.size()<s[i].size()) s1=s[i];
    else if(s1.size()==s[i].size()) s2=s[i];
    } for(int i=1;i<=2*n-2;++i){
    if(s1.substr(0,s[i].size())==s[i]) cnt++;
    } if(cnt>=n-1 && s1.substr(1,s1.size())==s2.substr(0,s2.size()-1)) pre=s1;
    else pre=s2; for(int i=1;i<=2*n-2;++i){
    if(s[i]==pre.substr(0,s[i].size()) && !vis[s[i].size()]){
    printf("P");
    vis[s[i].size()]=true;
    }
    else printf("S");
    } return 0;
    }