Prefixes and Suffixes

题目链接:http://codeforces.com/contest/1092/problem/C

题目大意:有一堆字符串,这些字符串是最初始字符串的一些前缀和后缀,现在让你判断输入的字符串是前缀(P)还是后缀(S),输入的字符串中是从1到n-1的长度,每种长度有两个,并按顺序打印出来。

思路:这题其实你只要想到初始的字符串应该有由什么组成,由什么能够确定下来。字符串由前缀和后缀就可以确定下来,并且两个的长度是n-1,由此可以确定有两种组成方式。然后遍历一遍就可以确定这两种字符串谁才是真正的初始字符串。注意:有些字符串前缀也行后缀也行,需要set判断前面是否出现过,默认为前缀,如果前面出现过,那再一次出现肯定就是后缀了,后缀不满足,说明这种字符串不是初始的字符串。还是太菜了,wa了这么多次才对。下面是我写的代码,不喜勿喷。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <queue>
 5 #include <map>
 6 #include <cstring>
 7 #include <string>
 8 #include <set>
 9 #include <vector>
10 #include <list>
11 #include <deque>
12 #include <algorithm>
13 #include <stack>
14 #include <numeric>
15 #include <time.h>
16 #include<iomanip>
17 #include<sstream>
18 #pragma disable:4996)
19 using namespace std;
20 const long long inf = 0x7f7f7f7f;
21 long long GCD(long long a, long long b) { return 0 == b ? a : GCD(b, a%b); }
22 const long long mod = 1e9 + 7;
23 const double pi = acos(-1);
24 string str[200], a[200];
25 int n, h;
26 void check(string s)
27 {
28     set<string>q;
29     set<string>::iterator iter;
30     vector<char>p;
31     int ans1 = 0, ans2 = 0;
32     int len = s.length();
33     for (int i = 0; i < 2 * n - 2; i++)
34     {
35         bool flag = false;
36         for (int j = 0, k = 0; j < str[i].length() && k < len; j++, k++)
37             if (str[i][j] != s[k])
38             {
39                 flag = true;
40                 break;
41             }
42         iter = q.find(str[i]);
43         q.insert(str[i]);
44         if (!flag&&iter == q.end())
45         {
46             p.push_back('P');
47             continue;
48         }
49         flag = false;
50         for (int j = str[i].length() - 1, k = len - 1; j >= 0 && k >= 0; j--, k--)
51             if (str[i][j] != s[k])
52             {
53                 flag = true;
54                 break;
55             }
56         if (!flag)
57             p.push_back('S');
58         else
59             return;
60     }
61     for (int i = 0; i < p.size(); i++)
62         cout << p[i];
63     cout << endl;
64     exit(0);
65 }
66 int main()
67 {
68     ios_base::sync_with_stdio(false);
69     cin >> n;
70     for (int i = 0; i < 2 * n - 2; i++)
71     {
72         cin >> str[i];
73         if (str[i].length() == n - 1)
74             a[h++] = str[i];
75     }
76     check(a[0] + a[1][a[1].length() - 1]);
77     check(a[1][0] + a[0]);
78 }

 

posted @ 2019-04-29 16:47  flymoon  阅读(394)  评论(0编辑  收藏  举报