hdu3294Finding Palindromes

题目链接:http://poj.org/problem?id=3376

未解决!!!

下面代码明显会TLE-_-||

新的方法还不理解以后再看。

 1 #include<cstdio>
 2 #include<string>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 const int maxn=2001000;
 7 string s[maxn];
 8 string t;
 9 int r[2000010];
10 int ans;
11 void manacher(string s)
12 {
13
14     int id=0,m=0;
15     int len=s.length();
16     for(int i=len;i>=0;i--)
17     {
18         s[i*2+2]=s[i];
19         s[i*2+1]=‘#‘;
20     }
21     s[0]=‘*‘;
22     for(int i=2;i<len*2+1;i++)
23     {
24         if(id+r[id]>i) r[i]=min(r[id*2-i],r[id]+id-i);
25         else r[i]=1;
26         while(s[i-r[i]]==s[i+r[i]]) r[i]++;
27         if(id+r[id]<i+r[i]) id=i;
28         if(m<r[id]) m=r[id];
29         if(m==len+1) {ans++;return ;}
30     }
31     return ;
32 }
33 int main()
34 {
35     int n;
36     //while(scanf("%d",&n)!=EOF)
37     scanf("%d",&n);
38     {
39         ans=0;
40         int x;
41         for(int i=0;i<n;i++){
42                 scanf("%d",&x);
43             cin>>s[i];
44         }
45         //二重循环,tle
46         for(int i=0;i<n;i++)
47             for(int j=0;j<n;j++)
48             {
49                 t="";
50                 t=s[i]+s[j];
51                 manacher(t);
52             }
53         printf("%d\n",ans);
54     }
55 }
时间: 2024-11-05 04:50:27

hdu3294Finding Palindromes的相关文章

[UVa] Palindromes(401)

UVA - 401 Palindromes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDED

uva 11584 Partitioning by Palindromes 线性dp

// uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串的数目 // // f[i] = min(f[i],f[j-1] + 1(j到i是回文串)) // // 这道题还是挺简单的,继续练 #include <algorithm> #include <bitset> #include <cassert> #include <

Codeforces Round #316 (Div. 2)E. Pig and Palindromes DP

E. Pig and Palindromes Peppa the Pig was walking and walked into the forest. What a strange coincidence! The forest has the shape of a rectangle, consisting of n rows and m columns. We enumerate the rows of the rectangle from top to bottom with numbe

Codeforces #316 E Pig and Palindromes DP

// Codeforces #316 E Pig and Palindromes // // 题目大意: // // 给你一张地图,n*m每个点是一个字母,现在从(0,0)出发, // 每次只能往右或者往下走,求走到(n-1,m-1)形成回文串的方法数. // // 解题思路: // // 动态规划.首先.如果起点和终点的字母不相同,那么肯定 // 不能形成回文串,直接输出0.对于能形成回文串.我们设状态 // d(step,i,j)表示走了step步,从第0行走到i行,第n-1行走到j行的 /

UVA11584---Partitioning by Palindromes(dp)

We say a sequence of characters is a palindrome if it is the same written forwards and backwards. For exam- ple, ' racecar ' is a palindrome, but ' fastcar ' is not. A partition of a sequence of char- acters is a list of one or more disjoint non-empt

UVa 401 Palindromes(字符串,回文)

 Palindromes  A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDEDCBA" is a palindrome because it is the same when the string is read from left to right as when the string i

UVA 之401 - Palindromes

A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDEDCBA" is a palindrome because it is the same when the string is read from left to right as when the string is read from ri

1167: 零起点学算法74——Palindromes _easy version

1167: 零起点学算法74--Palindromes _easy version Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lldSubmitted: 1754  Accepted: 1023[Submit][Status][Web Board] Description "回文串"是一个正读和反读都一样的字符串,比如"level"或者"noon"等等就是回文串.请写一个

洛谷P1207 [USACO1.2]双重回文数 Dual Palindromes

P1207 [USACO1.2]双重回文数 Dual Palindromes 291通过 462提交 题目提供者该用户不存在 标签USACO 难度普及- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 如果一个数从左往右读和从右往左读都是一样,那么这个数就叫做“回文数”.例如,12321就是一个回文数,而77778就不是.当然,回文数的首和尾都应是非零的,因此0220就不是回文数. 事实上,有一些数(如21),在十进制时不是回文数,但在其它进制(如二进制时为10101)时就是回文数. 编