哈理工新生赛 马拉车+贪心 最大密度子图 AC自动机+DP
| J.Symmys | |||||
|
|||||
| Description | |||||
|
ClearY is not good at English. He often fails in his English test. To help him study English, his English teacher asks him to rewrite some sentence. But ClearY doesn’t want to rewrite; he wants to play games. He comes up with a solution to complete rewriting earlier. He uses both his hands to write but he can only write the same letter at the same time. Every time, he chooses a symmetry center, using his left hand writing from this position to left continually and using his right hand writing from this position to right continually. He doesn’t not care about writing same letter two or more times in one place. He wants to know how many symmetry center at least he need to choose to complete his work. |
|||||
| Input | |||||
|
First line contains an integer T (1 ≤ T ≤ 1000), represents there are T test cases. For each test case: one line contains a string of up to 106 lower case letters in length. It’s guaranteed that the sum length of all string will not exceed 5*106. |
|||||
| Output | |||||
|
For each test case, output one line containing "Case #X: Y"(without quotes), where X is the case number starting from 1, and Y is the minimum number of symmetry center at least ClearY need to choose. |
|||||
| Sample Input | |||||
|
2 aba abbaab |
|||||
| Sample Output | |||||
|
Case #1: 1 Case #2: 2 |
|||||
| Hint | |||||
|
For the second sample case, he can write "abba"(position from 1 to 4) and "baab"(position from 3 to 6). |
#include<cstdio>//踩坑 for循环里不能每次求strlen
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int N=1e6+;
char str[N],t[N<<];
int p[N<<];
void Manacher(){
t[]='$',t[]='#';
int tot=;
int tc=strlen(str);
for(int i=;i<tc;++i) t[tot++]=str[i],t[tot++]='#';
t[tot]=;
tc=strlen(t);
int mx=,id=;
for(int i=;i<tc;++i) {
p[i]=mx>i?min(p[*id-i],mx-i):;
while(t[i+p[i]]==t[i-p[i]]) ++p[i];
if(mx<i+p[i]) {
mx=i+p[i];
id=i;
}
}
}
int maxx[N<<];
int main(){
int cas=,T;
for(scanf("%d",&T);T--;){
scanf("%s",str);
Manacher();
int tot=,length=*strlen(str)+;
for(int i=;i<=length;++i) maxx[i]=;
for(int i=;i<=length;++i) maxx[i-p[i]+]=max(maxx[i-p[i]+],i+p[i]);
int l=,sum=,mx=;
for(int i=;i<=length;++i) maxx[i]=max(maxx[i-],maxx[i]);
while(l<=length) {
l=maxx[l];
++sum;
}
printf("Case #%d: %d\n",cas++,sum);
}
}
| F.GXZ is poison | |||||
|
|||||
| Description | |||||
|
GXZ is a famous ACMer, and he is very strong and poisonous. His most powerful weapons are some poisonous strings, which will make your code full of bugs. Now there is a code, your task is to make them healthy, which means you are to repair your code by changing least number of characters so that the code doesn't contain any poisonous strings. To make it simpler, let's define "code" as a string composed of three characters: "G", "X", "Z". |
|||||
| Input | |||||
|
The input consists of multiple (at most 10) test cases. Each test case starts with a line containing one integers N (1 ≤ N ≤ 50), which is the number of poisonous strings. The following N lines gives N non-empty strings of length not greater than 20 containing only characters in "GXZ", which are the poisonous strings. The last line of the test case is a non-empty string of length not greater than 1000 containing only characters in "GXZ", which is the code to be repaired. |
|||||
| Output | |||||
|
For each test case, print a line containing the test case number (beginning with 1) followed by the number of characters which need to be changed. If it's impossible to repair the given code, print -1. |
|||||
| Sample Input | |||||
|
2 GGG GGX GGGX 2 G ZX ZXGGZX 3 G X Z GXZ |
|||||
| Sample Output | |||||
|
Case 1: 1 Case 2: 4 Case 3: -1 |
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
char str[N];
int dp[N][N],cas=;
struct AC{
int ch[N][],fail[N],sz,rt;
bool flag[N];
void init(){
sz=rt=;
memset(ch[rt],-,sizeof(ch[rt]));
flag[rt]=;
}
void newnode(){
++sz;
for(int i=;i<;++i) ch[sz][i]=-;
flag[sz]=;
}
void insert(char *str){
int len=strlen(str);
int u=rt;
for(int i=;i<len;++i) {
int zc;
if(str[i]=='G') zc=;
else if(str[i]=='X') zc=;
else zc=;
if(ch[u][zc]==-) {
newnode();
ch[u][zc]=sz;
}
u=ch[u][zc];
}
flag[u]=;
}
void build(){
queue<int>Q;
for(int i=;i<;++i) {
if(ch[rt][i]==-) ch[rt][i]=rt;
else {
fail[ch[rt][i]]=rt;
Q.push(ch[rt][i]);
}
}
while(!Q.empty()) {
int u=Q.front();
Q.pop();
flag[u]|=flag[fail[u]];
for(int i=;i<;++i) {
if(ch[u][i]==-) ch[u][i]=ch[fail[u]][i];
else {
fail[ch[u][i]]=ch[fail[u]][i];
Q.push(ch[u][i]);
}
}
}
}
void DP(){
memset(dp,-,sizeof(dp));
dp[][]=;
for(int i=;str[i-];++i) for(int j=;j<=sz;++j) if(!flag[j]&&dp[i-][j]!=-)
for(int k=;k<;++k) {
if(flag[ch[j][k]]) continue;
if(str[i-]=='G'&&k==||str[i-]=='X'&&k==||str[i-]=='Z'&&k==) {
if(dp[i][ch[j][k]]==-) dp[i][ch[j][k]]=dp[i-][j];
else dp[i][ch[j][k]]=min(dp[i][ch[j][k]],dp[i-][j]);
}
else {
if(dp[i][ch[j][k]]==-) dp[i][ch[j][k]]=dp[i-][j]+;
else dp[i][ch[j][k]]=min(dp[i][ch[j][k]],dp[i-][j]+);
}
}
int ans=0x3f3f3f3f,len=strlen(str);
for(int i=;i<=sz;++i) if(dp[len][i]!=-) ans=min(ans,dp[len][i]);
printf("Case %d: %d\n",cas++,ans==0x3f3f3f3f?-:ans);
}
}ac;
int main(){
int n;
while(scanf("%d",&n)!=EOF){
ac.init();
for(int i=;i<=n;++i) {
scanf("%s",str);
ac.insert(str);
}
ac.build();
scanf("%s",str);
ac.DP();
}
}
| C.Getting More Money Early | |||||
|
|||||
| Description | |||||
|
There is an airline company. They accidentally found a new country with N cities. They want to build some airlines. But, before you have an airline, the start city and the end city must had built an airport. Building an airport need P unit(s) money. And the company has got the information that how much money a year some airline can earn. So, they would like to get profit as early as possible. Can you tell the company's director the earliest time they can start to get profits? But,we may get a decimal number. So, you just need to print the sum of the investment and the sum of profit in one year in the condition above. |
|||||
| Input | |||||
|
The first line contains an integer T (1 ≤ T ≤ 5), represents the number of test cases. For each test case: The first line contains three integers N, M, P (1 ≤ N ≤ 500, 1 ≤ M ≤ 1000, 1 ≤ P ≤ 1000) Each line of the following M lines three integers, ui, vi, pi(1 ≤ ui, vi ≤ N, ui != vi,1 ≤ pi ≤ 1000), which means the airline between ui and vi city, and the profit the airline can get each year. The data promise that the number of airlines between any two cities is less than or equal to one. |
|||||
| Output | |||||
|
For each test case, output one line containing "Case #X: "(without quotes) at first, where X is the case number starting from 1. Than print the answer of the two number we need. The first is the sum of the investment and the second is the profit the company can get every year in that condition. |
|||||
| Sample Input | |||||
|
1 5 6 4 1 5 3 5 4 1 4 2 2 2 5 4 1 2 1 3 1 5 |
|||||
| Sample Output | |||||
|
Case #1: 16 13 |
|||||
| Hint | |||||
|
Case 1: you can choose city 1,2,3,5 and cost 16 unit of money and you can get the profit from the 1.23 year. That is the earliest time the company can start to get profit. |
这题被卡精度。。先扔坑
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int N=;
const double eps=1e-;
const double INF=1e9+;
int n,m,head[N],tot,dis[N],S,T,q[N];
double p;
struct node{
int v,next;
double w;
}e[N<<];
struct py{
int u,v;
double val;
}ct[N];
void add(int u,int v,double w){
e[tot].v=v;e[tot].w=w;e[tot].next=head[u];head[u]=tot++;
}
bool bfs(){
for(int i=S;i<=T;++i) dis[i]=-;
dis[S]=;
int l=,r=;
q[r++]=S;
while(l<r) {
int u=q[l++];
for(int i=head[u];~i;i=e[i].next){
int v=e[i].v;
if(dis[v]==-&&e[i].w>eps) {
q[r++]=v;
dis[v]=dis[u]+;
if(v==T) return true;
}
}
}
return false;
}
double dfs(int s,double low){
if(s==T||low<eps) return low;
double ans=low,a;
for(int i=head[s];~i;i=e[i].next) {
if(e[i].w>&&dis[e[i].v]==dis[s]+){
a=dfs(e[i].v,min(e[i].w,ans));
if(a<eps) continue;
e[i].w-=a;
e[i^].w+=a;
ans-=a;
if(ans<eps) return low;
}
}
if(low-ans<eps) dis[s]=-;
return low-ans;
}
int main(){
int cas=,Ta;
for(scanf("%d",&Ta);Ta--;){
scanf("%d%d%lf",&n,&m,&p);
S=,T=n+m+;
double ans=;
for(int i=;i<=m;++i) scanf("%d%d%lf",&ct[i].u,&ct[i].v,&ct[i].val),ans+=ct[i].val;
double l=,r=1e6+;
for(int i=;i<;++i){
double mid=(l+r)/;
for(int i=;i<=n+m+;++i) head[i]=-;
tot=;
for(int i=;i<=m;++i) {
add(S,i,ct[i].val),add(i,S,);
add(i,ct[i].u+m,INF),add(ct[i].u+m,i,);
add(i,ct[i].v+m,INF),add(ct[i].v+m,i,);
}
double ret=;
for(int i=;i<=n;++i) add(i+m,T,mid*p),add(T,i+m,);
while(bfs()) ret+=dfs(S,INF);
if(ans-ret<=eps) r=mid;
else l=mid;
}
l-=eps;
double ying=,tt=;
for(int i=head[T];~i;i=e[i].next) if(l*p-e[i].w<=eps) tt+=p;
ying=tt*l;
int y=round(ying),t=round(tt);
printf("Case #%d: %d %d\n",cas++,t,y);
}
}
哈理工新生赛 马拉车+贪心 最大密度子图 AC自动机+DP的更多相关文章
- HDU 4758 Walk Through Squares (2013南京网络赛1011题,AC自动机+DP)
Walk Through Squares Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Oth ...
- 2018 焦作网络赛 L Poor God Water ( AC自动机构造矩阵、BM求线性递推、手动构造矩阵、矩阵快速幂 )
题目链接 题意 : 实际上可以转化一下题意 要求求出用三个不同元素的字符集例如 { 'A' .'B' .'C' } 构造出长度为 n 且不包含 AAA.BBB CCC.ACB BCA.CAC CBC ...
- 6.29 省选模拟赛 坏题 AC自动机 dp 图论
考场上随手构造了一组数据把自己卡掉了 然后一直都是掉线状态了. 最后发现这个东西不是subtask -1的情况不多 所以就没管无解直接莽 写题有点晚 故没调出来.. 考虑怎么做 容易想到建立AC自动机 ...
- SCNU 2015ACM新生赛决赛【F. Oyk闯机关】解题报告
题目大意:一个$N$$\times$$N$的阵列,每个格子有$X_{ij}$个调和之音,若每次只能选择走右边或下边,从左上角出发走到右下角,问最多能收集到多少个调和之音? ...
- SCNU ACM 2016新生赛初赛 解题报告
新生初赛题目.解题思路.参考代码一览 1001. 无聊的日常 Problem Description 两位小朋友小A和小B无聊时玩了个游戏,在限定时间内说出一排数字,那边说出的数大就赢,你的工作是帮他 ...
- POJ 3155 Hard Life(最大密度子图)
裸题.输入一个无向图,输出最大密度子图(输出子图结点数和升序编号). 看了<最小割模型在信息学竞赛中的应用——胡伯涛>的一部分,感觉01分数规划问题又是个大坑.暂时还看不懂. 参考http ...
- poj 3155 最大密度子图
思路: 这个还是看的胡伯涛的论文<最小割在信息学竞赛中的应用>.是将最大密度子图问题转化为了01分数规划和最小割问题. 直接上代码: #include <iostream> # ...
- UVA LA 7146 2014上海亚洲赛(贪心)
option=com_onlinejudge&Itemid=8&page=show_problem&category=648&problem=5158&mosm ...
- POJ3155 Hard Life [最大密度子图]
题意:最大密度子图 #include<iostream> #include<cstdio> #include<cstring> #include<algo ...
随机推荐
- HDU 6341 Let Sudoku Rotate
#include<bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=a;i<=b;++i) #defi ...
- 【Linux常见命令】date命令
Linux date命令:可以用来显示或设定系统的日期与时间. 在显示方面,使用者可以设定欲显示的格式,格式设定为一个加号后接数个标记,其中可用的标记列表如下: 时间方面: %H : 小时(00..2 ...
- Bootstrap表格组件 Bootstrap Table
Bootstrap Table是Bootstrap的一个组件 Bootstrap Table Demo:http://issues.wenzhixin.net.cn/bootstrap-table/i ...
- 地表最简单安装MySQL及配置的方法,没有之一
第一步下载我的压缩包 链接:https://pan.baidu.com/s/1EE40dU0j2U1d-bAfj7TeVA 提取码:n25c 复制这段内容后打开百度网盘手机App,操作更方便哦 第二步 ...
- 日常开发中常用的linux命令
本文并不将linux的常用命令全部罗列出来,列出一下常用.容易忘记的命令. 更详细的说明见:https://www.cnblogs.com/xuxinstyle/p/9609551.html 文件相关 ...
- Pika源码学习--pika的通信和线程模型
pika的线程模型有官方的wiki介绍https://github.com/Qihoo360/pika/wiki/pika-%E7%BA%BF%E7%A8%8B%E6%A8%A1%E5%9E%8B,这 ...
- 2020.2.27——STL初步
注:本文主要针对STL中的常用的操作进行总结 目录: 1.swap 2.sort 3.reverse 4.min,max(比较简单,暂且略过) 5._gcd 6.lower_bound &&a ...
- Dubbo(六):zookeeper注册中心的应用
Dubbo中有一个非常本质和重要的功能,那就是服务的自动注册与发现,而这个功能是通过注册中心来实现的.而dubbo中考虑了外部许多的注册组件的实现,zk,redis,etcd,consul,eurek ...
- E. Count The Blocks(找数学规律)
\(\color{Red}{先说一下自己的歪解(找规律)}\) \(n=1是答案是10\) \(n=2时答案是180\) \(n=3时模拟一下,很容易发现答案是2610\ \ 180\ \ 10\) ...
- spring 事务管理配置
本篇文章只涉及spring事务的配置,不进行事务的介绍. spring通过PlatformTransactionManager接口作为事务管理器来进行事务的管理,它本身并不进行事务的创建以及相关操作, ...