码迷,mamicode.com
首页 > 其他好文 > 详细

UVa 11888 Abnormal 89's

时间:2017-01-31 18:34:38      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:mem   ;;   set   stream   cout   algorithm   images   ide   sed   

方法:Manacher

Manacher算法在O(length) 时间内求出各个回文子串的长度。O(length) 时间检查时那一种情况。

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 ‘\n‘
 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 const int maxn = 2e5+1;
 74 char ma[maxn*2];
 75 int p[maxn*2];
 76 int l;
 77 void Manacher(const string &str)
 78 {
 79     int len = str.length();
 80     l = 0;
 81     ma[l++] = $;
 82     ma[l++] = #;
 83     rep(i, len)
 84     {
 85         ma[l++] = str[i];
 86         ma[l++] = #;
 87     }
 88     ma[l] = 0;
 89     int pnow = 0, pid = 0;
 90     for (int i = 1; i < l; ++i)
 91     {
 92         if (pnow > i) p[i] = min(p[2*pid-i], pnow-i);
 93         else p[i] = 1;
 94         for (; ma[i-p[i]] == ma[i+p[i]]; ++p[i]);
 95         if (i+p[i] > pnow)
 96         {
 97             pnow = i+p[i];  pid = i;
 98         }
 99     }
100 }
101 /*
102  abaaba
103  .  ,  a  ,  b  ,  a  ,  a  ,  b  ,  a  ,
104  0  1  2  1  4  1  2  7  2  1  4  1  2  1
105  0  1  2  3  4  5  6  7  8  9 10 11 12 13
106  */
107 int main()
108 {
109     int T;  cin >> T;
110     repn(kase, T)
111     {
112         string str;
113         cin >> str;
114         Manacher(str);
115 
116         bool done = false;
117         for (int i = 2; i < (1+l)/2; ++i)
118             if (p[i] == i && p[(i+p[i]-1+l)/2] == l-(i+p[i]-1+l)/2)
119             {
120                 cout << "alindrome\n";
121                 done = true;
122                 break;
123             }
124         if (!done)
125             if (p[(l+1)/2]-1 == str.length())
126                 cout << "palindrome\n";
127             else
128             cout << "simple\n";
129 
130     }
131 }
View Code

 

UVa 11888 Abnormal 89's

标签:mem   ;;   set   stream   cout   algorithm   images   ide   sed   

原文地址:http://www.cnblogs.com/skyette/p/6358973.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!