哈理工新生赛 马拉车+贪心 最大密度子图 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 ...
随机推荐
- Spring5参考指南:基于注解的容器配置
文章目录 @Required @Autowired @primary @Qualifier 泛型 @Resource @PostConstruct和@PreDestroy Spring的容器配置可以有 ...
- 用两张图告诉你,为什么你的App会卡顿?
有什么料? 从这篇文章中你能获得这些料: 知道setContentView()之后发生了什么? 知道Android究竟是如何在屏幕上显示我们期望的画面的? 对Android的视图架构有整体把握. 学会 ...
- 由JS数组去重说起
一.问题描述: var array=[1,45,3,1,4,67,45],请编写一个函数reDup来去掉其中的重复项,即 reDup(array); console.log(array);//[1,4 ...
- ACM算法--二分法--模板
// 在单调递增序列a中查找>=x的数中最小的一个(即x或x的后继) while (l < r) { int mid = (l + r) / 2; if (a[mid] >= x) ...
- C++获取当前系统时间并格式化输出
C++中与系统时间相关的函数定义在头文件中. 一.time(time_t * )函数 函数定义如下: time_t time (time_t* timer); 获取系统当前日历时间 UTC 1970- ...
- 网络流--最大流--POJ 1459 Power Network
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> #incl ...
- 图论-欧拉图-欧拉回路-Euler-Fluery-Hierholzer-逐步插入回路法-DFS详解-并查集
欧拉图性质: 1.无向连通图G是欧拉图,当且仅当G不含奇数度结点(G的所有结点度数为偶数): 2.无向连通图G含有欧拉通路,当且仅当G有零个或两个奇数度的结点: 3.有向连通图D是欧拉图,当且仅当该图 ...
- P3588 【[POI2015]PUS】(线段树优化建边)
P3588 [[POI2015]PUS] 终于有个能让我一遍过的题了,写篇题解纪念一下 给定长度为n的序列和其中部分已知的数,还有m个大小关系:区间\([l,r]\)中,有k个给定的数比剩下的\(r- ...
- OSG程序设计之Hello World 4.0
代码如下: //需要添加两个库:osgUtild.lib.osgTextd.lib #include <osgDB/ReadFile> #include <osgUtil/Optim ...
- Web框架,Hibernate向数据库插入数据,数据库没有值怎么办?
用web框架技术,使用Hibernate向数据库添加信息,控制台显示插入成功的语句,可是数据库却没有值:错误如下: (1)不要自己创建数据库!!,Web框架可以自己自动生成,自己创建可能会报错! (2 ...