Codeforces 245H Queries for Number of Palindromes

题意:给你一个字符串,再给你一个q(询问个数1000000),问你这个区间内回文串的个数。

解题思路:

1)dp,先把 i 到j 是回文串的真值赋值给 dp[i][j] ,然后从后往前dp    dp[i][j] += dp[i+1][j] + dp[i][j-1]  -dp[i+1][j-1];

解题代码:

 1 // File Name: 245h.dp.cpp
 2 // Author: darkdream
 3 // Created Time: 2015年03月30日 星期一 15时27分10秒
 4
 5 #include<vector>
 6 #include<list>
 7 #include<map>
 8 #include<set>
 9 #include<deque>
10 #include<stack>
11 #include<bitset>
12 #include<algorithm>
13 #include<functional>
14 #include<numeric>
15 #include<utility>
16 #include<sstream>
17 #include<iostream>
18 #include<iomanip>
19 #include<cstdio>
20 #include<cmath>
21 #include<cstdlib>
22 #include<cstring>
23 #include<ctime>
24 #define LL long long
25 #define maxn 5005
26 using namespace std;
27 int dp[maxn][maxn];
28 char str[maxn];
29 int n;
30 int main(){
31     scanf("%s",&str[1]);
32     n = strlen(&str[1]);
33     for(int i = 1;i <= n;i ++)
34     {
35       if(str[i] == str[i+1])
36           dp[i][i+1] = 1;
37       dp[i][i] = 1;
38     }
39     for(int i = n;i >= 1;i --)
40     {
41        for(int j = i+1;j <= n;j ++)
42        {
43           if(dp[i+1][j-1] == 1&& str[i] == str[j])
44           {
45             dp[i][j] = 1;
46           }
47        }
48     }
49     for(int i = n;i >= 1;i --)
50       for(int j = i ;j <= n ;j ++)
51       {
52         dp[i][j] += dp[i+1][j] + dp[i][j-1] -dp[i+1][j-1];
53       }
54     int q;
55     scanf("%d",&q);
56     while(q--)
57     {
58       int l , r ;
59       scanf("%d %d",&l,&r);
60       printf("%d\n",dp[l][r]);
61     }
62 return 0;
63 }

2)树状数组 + 离线  。。。 发现线段树过不了  毕竟我写的线段树常数太大。

解题代码:

  1 // File Name: 245h.cpp
  2 // Author: darkdream
  3 // Created Time: 2015年03月30日 星期一 11时09分19秒
  4
  5 #include<vector>
  6 #include<list>
  7 #include<map>
  8 #include<set>
  9 #include<deque>
 10 #include<stack>
 11 #include<bitset>
 12 #include<algorithm>
 13 #include<functional>
 14 #include<numeric>
 15 #include<utility>
 16 #include<sstream>
 17 #include<iostream>
 18 #include<iomanip>
 19 #include<cstdio>
 20 #include<cmath>
 21 #include<cstdlib>
 22 #include<cstring>
 23 #include<ctime>
 24 #define LL long long
 25
 26 using namespace std;
 27 char  str[6000];
 28 map<int ,int >dpj[5005];
 29 map<int ,int >dpo[5005];
 30 map<int ,int >::iterator tp;
 31 struct qu{
 32  int l , r,si;
 33 }a[1000005];
 34 int ans[1000005];
 35 int q, n ;
 36 int cmp(qu a, qu b)
 37 {
 38   return a.l > b.l ;
 39 }
 40 int tree[6005];
 41 int lowbit(int x)
 42 {
 43   return x & -x;
 44 }
 45 void update(int x)
 46 {
 47    while(x <= n)
 48    {
 49       tree[x] += 1;
 50       x += lowbit(x);
 51    }
 52 }
 53 int getsum(int r)
 54 {
 55   int ans  = 0 ;
 56   while(r >= 1)
 57   {
 58      ans += tree[r];
 59      r -= lowbit(r);
 60   }
 61   return ans;
 62 }
 63 inline void read_int(int &ret){
 64    char ch = getchar();
 65    while(!isdigit(ch)){
 66      ch = getchar();
 67    }
 68    ret =0 ;
 69    while(isdigit(ch)){
 70       ret = ret*10 + ch -‘0‘ ;
 71       ch = getchar();
 72    }
 73 }
 74 int main(){
 75  //   freopen("input","r",stdin);
 76 //    freopen("output","w",stdout);
 77     clock_t be ,en ;
 78     be = clock();
 79     scanf("%s",&str[1]);
 80      scanf("%d",&q);
 81      for(int i = 1;i <=q ;i ++)
 82      {
 83          read_int(a[i].l);
 84          read_int(a[i].r);
 85          a[i].si = i ;
 86      }
 87      sort(a+1,a+1+q,cmp);
 88      n = strlen(&str[1]);
 89      int tt = 1;
 90      for(int i = n;i >= 1;i --)
 91      {
 92         dpj[i][1] = i;
 93         update(i);
 94         if(str[i] == str[i+1])
 95         {
 96           dpo[i][2] = i+1;
 97           update(i+1);
 98         }
 99         for(tp = dpj[i+1].begin() ; tp != dpj[i+1].end();tp++)
100         {
101            if(str[i] == str[tp->second +1])
102            {
103                dpj[i][tp->first+2] = tp->second +1;
104                update(tp->second+1);
105            }
106         }
107         for(tp = dpo[i+1].begin() ; tp != dpo[i+1].end();tp++)
108         {
109            if(str[i] == str[tp->second +1])
110            {
111                dpo[i][tp->first+2] = tp->second +1;
112                update(tp->second+1);
113            }
114         }
115         while(a[tt].l == i && tt <= q)
116         {
117            ans[a[tt].si] = getsum(a[tt].r);
118            tt++;
119         }
120         if(tt >q)
121             break;
122         dpj[i+1].clear();
123         dpo[i+1].clear();
124      }
125      for(int i = 1;i <= q; i ++)
126          printf("%d\n",ans[i]);
127      en = clock();
128 //     printf("%lf\n",(en -be)*1.0/CLOCKS_PER_SEC);
129 return 0;
130 }

时间: 2024-10-10 17:59:55

Codeforces 245H Queries for Number of Palindromes的相关文章

dp --- Codeforces 245H :Queries for Number of Palindromes

Queries for Number of Palindromes Problem's Link:   http://codeforces.com/problemset/problem/245/H Mean: 给你一个字符串,然后q个询问:从i到j这段字符串中存在多少个回文串. analyse: dp[i][j]表示i~j这段的回文串数. 首先判断i~j是否为回文,是则dp[i][j]=1,否则dp[i][j]=0; 那么dp[i][j]=dp[i][j]+dp[i][j-1]+dp[i+1[j

Codeforces 245H Queries for Number of Palindromes:区间dp

题目链接:http://codeforces.com/problemset/problem/245/H 题意: 给你一个字符串s. 然后有t个询问,每个询问给出x,y,问你区间[x,y]中的回文子串的个数. 题解: 表示状态: dp[x][y] = numbers 表示区间[x,y]中的回文子串个数. 找出答案: 每次询问:ans = dp[x][y] 如何转移: dp[x][y] = dp[x][y-1] + dp[x+1][y] - dp[x+1][y-1] + pal[x][y] 用到了容

「Codeforces」245H Queries for Number of Palindromes (区间dp)

题意:原题在这 You've got a string s = s1s2... s|s| of length |s|, consisting of lowercase English letters. There also are q queries, each query is described by two integers li, ri (1 ≤ li ≤ ri ≤ |s|). The answer to the query is the number of substrings of

codeforces 245H H. Queries for Number of Palindromes(区间dp)

题目链接: codeforces 245H 题目大意: 给出一个字符串,询问任意区间内的回文子串的个数. 题目分析: 定义isPar[i][j]表示区间字符串[i,j]是否是回文,可以通过isPar[i+1][j-1]递推得到. 定义dp[i][j]表示及区间[i,j]内的回文子串的个数,转移方程如下: dp[i][j]=dp[i+1][j]+dp[i][j?1]?dp[i+1][j?1]+isPar[i][j] 用到了一点容斥的思想. AC代码: #include <iostream> #i

CF245H Queries for Number of Palindromes(回文树)

题意翻译 题目描述 给你一个字符串s由小写字母组成,有q组询问,每组询问给你两个数,l和r,问在字符串区间l到r的字串中,包含多少回文串. 输入格式 第1行,给出s,s的长度小于5000 第2行给出q(1<=q<=10^6) 第2至2+q行 给出每组询问的l和r 输出格式 输出每组询问所问的数量. 题目描述 You've got a string s=\(s_{1}\)\(s_{2}\)...\(s_{|s|}\) of length |s| , consisting of lowercase

[CF245H] Queries for Number of Palindromes (容斥原理dp计数)

题目链接:http://codeforces.com/problemset/problem/245/H 题目大意:给你一个字符串s,对于每次查询,输入为一个数对(i,j),输出s[i..j]之间回文串的个数. 容斥原理: dp[i][j] = dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1]; if( str[i]==str[j] 并且 str[i+1..j-1]是回文串 ) dp[i][j]++; 代码: 1 #include <cstdio> 2 #include &l

HDOJ 3948 The Number of Palindromes 后缀数组

后缀数组求有多少个不同的回文串 The Number of Palindromes Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1976    Accepted Submission(s): 690 Problem Description Now, you are given a string S. We want to kno

HDOJ 3948 The Number of Palindromes 回文串自动机

看上去像是回文串自动机的模板题,就来了一发 The Number of Palindromes Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1992    Accepted Submission(s): 694 Problem Description Now, you are given a string S. We want

hdu 3948 The Number of Palindromes

The Number of Palindromes Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)http://acm.hdu.edu.cn/showproblem.php?pid=3948 Problem Description Now, you are given a string S. We want to know how many distinct substri