Problem D:

题意:给定一棵n个点树,每个点有一个字母,有m个询问,每次询问某个节点x的子树中所有深度为k的点能否组成一个回文串

分析:一堆点能组成回文串当且仅当数量为奇数的字母不多于1个,显然这个状态可以用二进制表示

那么对于单个询问如何快速找到所有符合要求的点呢?

这里可以考虑树的dfs序,我们把深度相同的点按照dfs序加入集合中,易知在同一颗子树中的点肯定形成了一个连续的区间。

因此每次可以通过二分子树根节点的进入dfs序的时间和出dfs序的时间来找到这个区间

找到区间后可以根据预处理出的异或前缀和直接得到这个区间的二进制状态,再check一下即可

复杂度 n+m*(logn+26)

代码:

 #include <bits/stdc++.h>
using namespace std;
vector<int>g[];
int a[];
char s[];
int in[];
int out[];
int n,m;
int t=;
struct node
{
int t,val;
bool operator <(const node &a)const
{
return t<a.t;
}
};
vector<node>ans[];
void dfs(int now,int d)
{
in[now]=++t;
ans[d].push_back(node{t,ans[d][ans[d].size()-].val^a[now]});
for(int i=;i<(int)g[now].size();i++)
{
int to=g[now][i];
dfs(to,d+);
}
out[now]=++t;
}
int main()
{
scanf("%d%d",&n,&m); for(int i=;i<=n;i++)
{
int x;
scanf("%d",&x);
g[x].push_back(i);
} scanf("%s",s);
for(int i=;i<=n;i++)
{
a[i]=<<(s[i-]-'a');
ans[i].push_back(node{,});
}
dfs(,);
while(m--)
{
int p,x;
scanf("%d%d",&p,&x);
auto l=lower_bound(ans[x].begin(),ans[x].end(),node{in[p],});
auto r=upper_bound(ans[x].begin(),ans[x].end(),node{out[p],});
l--;
r--;
int st=((*l).val)^((*r).val);
int cnt=;
for(int i=;i<;i++)
{
if(st&(<<i))
cnt++;
}
if(cnt>)
{
puts("No");
}
else
{
puts("Yes");
}
}
return ;
}

Problem E:

题意:给一个n*m的棋盘,每个格子有一个字母,现在从1,1走到n,m,每次只能向右或者向下走,问走的路径上形成的字符串是回文串的方案书是多少

分析:从回文串的中点,即x+y=(n+m)/2的地方开始dp ,dp[i][j][k]存储当前回文长度为2*i ,左端点的x为i,右端点的x为j的回文串有多少个。

然后按照对角线递推即可。转移时注意必须满足左端点的x必须小于右端点,y必须大于右端点

代码:

 #include <bits/stdc++.h>
using namespace std;
int n,m;
int dp[][][];
char s[][];
const int mod=1e9+;
int main()
{
scanf("%d%d",&n,&m);
for(int i=; i<n; i++)
{
scanf("%s",s[i]);
}
int ok=;
for(int i=(n+m)/-; i>=; i--)
{
memset(dp[i%],,sizeof(dp[i%]));
int j=n+m--i;
for(int x1=; x1<n; x1++)
{
int y1=i-x1;
if(y1<||y1>=m)
continue;
for(int x2=; x2<n; x2++)
{ int y2=j-x2;
if(y2<||y2>=m)
continue;
if(x1>x2||y1>y2)
continue;
if(s[x1][y1]==s[x2][y2])
{
if(ok==)
{
dp[i%][x1][x2]=;
continue;
}
for(int a=; a<; a++)
{
if(x1+a>=n)
continue;
for(int b=-; b<; b++)
{
if(x2+b<)
continue;
dp[i%][x1][x2]=(dp[i%][x1][x2]+dp[(i+)%][x1+a][x2+b])%mod;;
}
}
}
}
}
ok++;
}
printf("%d\n",dp[][][n-]);
return ;
}

Codeforces Round #316 (Div. 2) D、E的更多相关文章

  1. Codeforces Round #437 (Div. 2)[A、B、C、E]

    Codeforces Round #437 (Div. 2) codeforces 867 A. Between the Offices(水) 题意:已知白天所在地(晚上可能坐飞机飞往异地),问是否从 ...

  2. Codeforces Round #298 (Div. 2) A、B、C题

    题目链接:Codeforces Round #298 (Div. 2) A. Exam An exam for n students will take place in a long and nar ...

  3. Codeforces Codeforces Round #316 (Div. 2) C. Replacement set

    C. Replacement Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/proble ...

  4. Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树

    C. ReplacementTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/problem ...

  5. Codeforces Round #482 (Div. 2) C 、 Kuro and Walking Route(dfs)979C

    题目链接:http://codeforces.com/contest/979/problem/C 大致题意 给出n个点,有n-1个边将他们链接.给出x,y,当某一路径中出现x....y时,此路不通.路 ...

  6. Codeforces Round #316 (Div. 2) C. Replacement

    题意:给定一个字符串,里面有各种小写字母和' . ' ,无论是什么字母,都是一样的,假设遇到' . . ' ,就要合并成一个' .',有m个询问,每次都在字符串某个位置上将原来的字符改成题目给的字符, ...

  7. Codeforces Round #316 (Div. 2) B. Simple Game

    思路:把n分成[1,n/2],[n/2+1,n],假设m在左区间.a=m+1,假设m在右区间,a=m-1.可是我居然忘了处理1,1这个特殊数据.被人hack了. 总结:下次一定要注意了,提交前一定要看 ...

  8. Codeforces Round #604 (Div. 2) D、E、F题解

    Beautiful Sequence Beautiful Mirrors Beautiful Bracket Sequence (easy version) Beautiful Sequence \[ ...

  9. Codeforces Round #316 (Div. 2) D计算在一棵子树内某高度的节点

    题:https://codeforces.com/contest/570/problem/D 题意:给定一个以11为根的n个节点的树,每个点上有一个字母(a~z),每个点的深度定义为该节点到11号节点 ...

随机推荐

  1. Apache Shiro 使用手冊 链接文件夹整理

    1.Apache Shiro 使用手冊(一)Shiro架构介绍 2.Apache Shiro 使用手冊(二)Shiro 认证 3.Apache Shiro 使用手冊(三)Shiro 授权 4.Apac ...

  2. Linux shell入门基础(一)

    Linux shell入门基础(一): 01.增加删除用户: #useradd byf   userdel byf(主目录未删除)  userdel -r byf   该用户的属性:usermod 用 ...

  3. Volley 百财帮封装

    Activity public class MainActivity extends Activity implements OnClickListener {     private Context ...

  4. 第2章 来点C#的感觉

    创建控制台项目 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  5. bootstrap字体图标

    bootstrap字体图标 http://v3.bootcss.com/components/ <!DOCTYPE HTML> <html> <head> < ...

  6. 星座物语APP

    效果图: 这里的后台管理用的是duducat,大家可以去百度看说明.图片,文字都在duducat后台服务器上,可以自己修改的.(PS:图片这里是随便找的) http://www.duducat.com ...

  7. 系统管理中 bash shell 脚本常用方法总结

    在日常系统管理工作中,需要编写脚本来完成特定的功能,编写shell脚本是一个基本功了!在编写的过程中,掌握一些常用的技巧和语法就可以完成大部分功能了,也就是2/8原则 1. 单引号和双引号的区别 单引 ...

  8. MyEclipse汉化后问题

    今天为了教学生如何汉化MyEclipse10.7,所以讲IDE汉化了一下. 个人还是喜欢用英文版,所以就将D:\MyEclipse\MyEclipse 10目录下的配置文件myeclipse.ini里 ...

  9. angularjs使用directive实现分页组件

    闲来没事,分享下项目中自己写的分页组件.来不及了,直接上车. 效果: 输入框可任意输入,并会自动提交到该页 html: <ul class="page clearfix"&g ...

  10. 武汉科技大学ACM:1007: 文本编辑器

    Problem Description YB打算写一个功能强大的文本编辑器,并取一个炫酷拉风,高端优雅的名字,比如“YB牌文本编辑器”之类的.既然功能强大,那肯定得有个查找功能吧.但是他在完成这个功能 ...