UVa 257 Palinword

方法:Hash

一定要好好读题,原题的要求即让你判断一个string时候有两个长度大于3的回文substring,这两个substring可以相交但是不能包含另一方。

一个很重要的observation是我们只需要找长度为3 或者长度为4的回文substring即可(贪心),可以证明, 找长度更长的回文substring效果不会比找长度为3或4的效果更好。(因为更长的回文中心总包含了一个长度为3或4的短回文substring,用这些短回文代替长回文,并不会增加被另一方包涵的回文,证明完毕)。而且当遇到aaaa 和 aaa的情况,我们按照上面的证明选择长度为3的。所以我们先判断长度为3的回文,如果成功就不去判断当前位置长度为4的回文了;如果不成功再判断该位置是否有长度为4的回文。

该如何判重呢?HASH。因为我们只考虑长度为3或4的回文,而且alphabet的size只有26,那么这些短回文可以被uniquely 表示成以26为base,而且不超过4位的整数(<26^4)。当然一个长度为3或4的回文substring的信息可以完全被其前两个字符包涵,所以hash出的hash value 可以不超过26^2,相当于完美hash,为了区分长度为3和4的回文,我们可以选择用两个bool array来判重。(下面的code是将长度为3的回文用其所有的三位字符表示,以区分其与长度为4的序列,判重array的size不超过26^3, 可以接受)。

code:

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <iostream>
  5 #include <string>
  6 #include <vector>
  7 #include <stack>
  8 #include <bitset>
  9 #include <cstdlib>
 10 #include <cmath>
 11 #include <set>
 12 #include <list>
 13 #include <deque>
 14 #include <map>
 15 #include <queue>
 16 #include <fstream>
 17 #include <cassert>
 18 #include <unordered_map>
 19 #include <unordered_set>
 20 #include <cmath>
 21 #include <sstream>
 22 #include <time.h>
 23 #include <complex>
 24 #include <iomanip>
 25 #define Max(a,b) ((a)>(b)?(a):(b))
 26 #define Min(a,b) ((a)<(b)?(a):(b))
 27 #define FOR(a,b,c) for (ll (a)=(b);(a)<(c);++(a))
 28 #define FORN(a,b,c) for (ll (a)=(b);(a)<=(c);++(a))
 29 #define DFOR(a,b,c) for (ll (a)=(b);(a)>=(c);--(a))
 30 #define FORSQ(a,b,c) for (ll (a)=(b);(a)*(a)<=(c);++(a))
 31 #define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
 32 #define FOREACH(a,b) for (auto &(a) : (b))
 33 #define rep(i,n) FOR(i,0,n)
 34 #define repn(i,n) FORN(i,1,n)
 35 #define drep(i,n) DFOR(i,n-1,0)
 36 #define drepn(i,n) DFOR(i,n,1)
 37 #define MAX(a,b) a = Max(a,b)
 38 #define MIN(a,b) a = Min(a,b)
 39 #define SQR(x) ((LL)(x) * (x))
 40 #define Reset(a,b) memset(a,b,sizeof(a))
 41 #define fi first
 42 #define se second
 43 #define mp make_pair
 44 #define pb push_back
 45 #define all(v) v.begin(),v.end()
 46 #define ALLA(arr,sz) arr,arr+sz
 47 #define SIZE(v) (int)v.size()
 48 #define SORT(v) sort(all(v))
 49 #define REVERSE(v) reverse(ALL(v))
 50 #define SORTA(arr,sz) sort(ALLA(arr,sz))
 51 #define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
 52 #define PERMUTE next_permutation
 53 #define TC(t) while(t--)
 54 #define forever for(;;)
 55 #define PINF 1000000000000
 56 #define newline '
'
 57 
 58 #define test if(1)if(0)cerr
 59 using namespace std;
 60 using namespace std;
 61 typedef vector<int> vi;
 62 typedef vector<vi> vvi;
 63 typedef pair<int,int> ii;
 64 typedef pair<double,double> dd;
 65 typedef pair<char,char> cc;
 66 typedef vector<ii> vii;
 67 typedef long long ll;
 68 typedef unsigned long long ull;
 69 typedef pair<ll, ll> l4;
 70 const double pi = acos(-1.0);
 71 
 72 
 73 string str;
 74 const int maxn = 26*26*26;
 75 bitset<maxn> vis;
 76 bool solve()
 77 {
 78     vis.reset();
 79     int len = str.length();
 80     for (int i = 1; i < len-1; ++i)
 81     {
 82         int h = ((str[i-1]-'A')*26+str[i]-'A')*26+str[i+1]-'A';
 83         if (str[i-1] == str[i+1])
 84         {
 85             vis[h] = true;
 86             if (vis.count() > 1) return true;
 87             continue;
 88         }
 89         if (i != len-2 && str[i-1]==str[i+2] && str[i] == str[i+1])
 90         {
 91             h = h/26;
 92             vis[h] = true;
 93             if (vis.count() > 1) return true;
 94         }
 95     }
 96     return false;
 97 }
 98 
 99 int main()
100 {
101     ios::sync_with_stdio(false);
102     cin.tie(0);
103     while (cin >> str)
104     {
105         if (solve()) cout << str << newline;
106     }
107 }
108 
109 
110 
111 
112 /*
113  2
114  3 2 cat dog mouse rat bat 1 1 abc cab
115  */
View Code
原文地址:https://www.cnblogs.com/skyette/p/6358656.html