UVa 11888 Abnormal 89's

方法: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 }

UVa 11888 Abnormal 89's

时间: 2024-10-11 23:16:33

UVa 11888 Abnormal 89's的相关文章

UVA 11888 - Abnormal 89&#39;s(Manachar)

UVA 11888 - Abnormal 89's 题目链接 题意:给定一个字符串,判断类型,一共三种,两个回文拼接成的,一个回文,其它 思路:利用Manachar处理出每个位置的最长回文,然后扫描一遍去判断即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 200005; int t, p[N * 2], n, len;

UVA 11888 - Abnormal 89&amp;#39;s(Manachar)

UVA 11888 - Abnormal 89's option=com_onlinejudge&Itemid=8&page=show_problem&category=524&problem=2988&mosmsg=Submission+received+with+ID+14075396" target="_blank" style="">题目链接 题意:给定一个字符串.推断类型.一共三种.两个回文拼接成的,

千穗谷大家空心砖接着看见数据库恢复

pinterest.com/youzhenqu/%E6%BF%89%E6%BA%AA%E5%8E%BF%E5%93%AA%E9%87%8C%E6%9C%89%E6%89%BE%E5%B0%8F%E5%A7%90%E6%9C%8D%E5%8A%A1%E7%94%B5%E8%AF%9D%E4%BF%A1%E6%81%AF pinterest.com/youzhenqu/%E7%95%8C%E9%A6%96%E6%89%BE%E5%B0%8F%E5%A7%90%E4%B8%8A%E9%97%A8%E5

7. 及时发布。除非真正的用户接触到你的产品并给予反馈

7. 及时发布.除非真正的用户接触到你的产品并给予反馈,你永远都不会知道你的产品是好是坏. 8. 尽快发布,经常发布.不要惦记着再增加一些其它功能.只要能达到可以用来收集用户反馈的最小功能集合,那就发布它.收集反馈信息,反复这个过程,发布下一 个版本.下一个版本,越快越好.如果你3个月才发布出第一版面向用户的产品,你拖延的太久了.如果3个星期才发布一次更新包,你拖延的太久了.如果不能一 周几次,那每周发布一次更新.每3周发布一次重大更新. 9. 唯一有意义的事是你的产品的好坏.其它的都是鸡毛蒜皮

否极泰来接电话高科技电话施

http://www.yhd.com/ctg/s2/c19606-0-60977/%E8%BF%9E%E4%BA%91%E6%B8%AF%E6%B5%B7%E5%B7%9E%E5%8C%BA%E5%93%AA%E9%87%8C%E6%9C%89%E6%89%BE%E5%AD%A6%E7%94%9F%E5%A6%B9%E6%89%93%E7%82%AE%E4%B8%8A%E9%97%A8%E6%9C%8D%E5%8A%A1%E5%8C%85%E5%A4%9C%E7%94%B5%E8%AF%9D%2

[2016-02-03][UVA][514][Rails]

时间:2016-02-03 22:24:52 星期三 题目编号:UVA 514 题目大意:给定若干的火车(编号1-n),按1-n的顺序进入车站, 给出火车出站的顺序,问是否有可能存在 分析:    FIFO,用栈模拟一遍即可, 方法:    根据输入的顺序,从1-n开始,当前操作的为i 如果i是当前对应的编号,那么直接跳过(进入B) 如果不是,根据当前需求的编号,小于i,就从栈顶弹出一个元素, 看这个元素是否是需求的,是则继续.否则NO 1 2 3 4 5 6 7 8 9 10 11 12 13

Uva 11464 偶数矩阵

题目链接:https://uva.onlinejudge.org/external/114/11464.pdf 和开关问题类似,只不过现在是用的位运算操作更简单了,其中要注意的是,只能将0变成1. 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define inf 0x3f3f3f3f 6 7 int a[20][20]; 8 int b[20][20]; 9 int n; 10 11 int dr[3] = {-1,0,0}

UVa 1451 Average -斜率优化

A DNA sequence consists of four letters, A, C, G, and T. The GC-ratio of a DNA sequence is the number of Cs and Gs of the sequence divided by the length of the sequence. GC-ratio is important in gene finding because DNA sequences with relatively high

[题解]UVa 12661 Funny Car Racing - spfa

很简单的一道最短路问题.分情况处理赛道的打开和关闭. Code 1 /** 2 * UVa 3 * Problem#12661 4 * Accepted 5 * Time:50ms 6 */ 7 #include<iostream> 8 #include<fstream> 9 #include<sstream> 10 #include<cstdio> 11 #include<cstdlib> 12 #include<cstring>