Problem Statement

In a string fair, they determine the beauty of a non-empty string $S$ consisting of lowercase English letters.

The beauty of string $S$ equals the sum of $N$ scores determined by $N$ criteria.
For $i = 1, 2, \ldots, N$, the score determined by the $i$-th criterion is
"the number of occurrences of a string $T_i$ (of length at most $3$ given in the Input) in $S$ as a consecutive subsequence" multiplied by $P_i$.

Print the maximum possible beauty of a non-empty string $S$ consisting of lowercase English letters.
If it is possible to obtain infinitely large beauty, print Infinity instead.

Here, the number of occurrences of a string $V$ in a string $U = U_1U_2\ldots U_{|U|}$ as a consecutive subsequence is defined to be
the number of integers $i$ such that $1 \leq i \leq |U|-|V|+1$ and $U_iU_{i+1}\ldots U_{i+|V|-1} = V$.

Constraints

  • $1 \leq N \leq 18278$
  • $N$ is an integer.
  • $T_i$ is a string of length between $1$ and $3$ consisting of lowercase English letters.
  • $i \neq j \Rightarrow T_i \neq T_j$
  • $-10^9 \leq P_i \leq 10^9$
  • $P_i$ is an integer.

Input

Input is given from Standard Input in the following format:

$N$
$T_1$ $P_1$
$T_2$ $P_2$
$\vdots$
$T_N$ $P_N$

Output

Print the maximum possible beauty of a non-empty string $S$ consisting of lowercase English letters.
If it is possible to obtain infinitely large beauty, print Infinity instead.


Sample Input 1

3
a -5
ab 10
ba -20

Sample Output 1

Infinity

For example, if $S =$ abzabz:

  • The score determined by the $1$-st criterion is $2 \times (-5) = -10$ points, because a occurs twice in $S$ as a consecutive subsequence.
  • The score determined by the $2$-nd criterion is $2 \times 10 = 20$ points, because ab occurs twice in $S$ as a consecutive subsequence.
  • The score determined by the $3$-rd criterion is $0 \times (-20) = 0$ points, because ba occurs $0$ times in $S$ as a consecutive subsequence.

Thus, the beauty of $S$ is $(-10) + 20 + 0 = 10$.

As another example, if $S =$ abzabzabz:

  • The score determined by the $1$-st criterion is $3 \times (-5) = -15$ points, because a occurs $3$ times in $S$ as a consecutive subsequence.
  • The score determined by the $2$-nd criterion is $3 \times 10 = 30$ points, because ab occurs $3$ times in $S$ as a consecutive subsequence.
  • The score determined by the $3$-rd criterion is $0 \times (-20) = 0$ points, because ba occurs $0$ times in $S$ as a consecutive subsequence.

Thus, the beauty of $S$ is $(-15) + 30 + 0 = 15$.

In general, for a positive integer $X$, if $S$ is a concatenation of $X$ copies of abz, then the beauty of $S$ is $5X$.
Since you can obtain as large beauty as you want, Infinity should be printed.


Sample Input 2

28
a -5
ab 10
ba -20
bb -20
bc -20
bd -20
be -20
bf -20
bg -20
bh -20
bi -20
bj -20
bk -20
bl -20
bm -20
bn -20
bo -20
bp -20
bq -20
br -20
bs -20
bt -20
bu -20
bv -20
bw -20
bx -20
by -20
bz -20

Sample Output 2

5

$S = $ ab achieves the maximum beauty possible.


Sample Input 3

26
a -1
b -1
c -1
d -1
e -1
f -1
g -1
h -1
i -1
j -1
k -1
l -1
m -1
n -1
o -1
p -1
q -1
r -1
s -1
t -1
u -1
v -1
w -1
x -1
y -1
z -1

Sample Output 3

-1

Note that $S$ should be a non-empty string.

只有长度为 3 的串,这是一个重要条件。

考虑一个结尾为 \(s_1s_2\) 的串,那么此时增加一个字符 \(s_3\),那么我们可以轻易计算出会增加多少的权值,然后这个字符串的结尾就变成 \(s_2s_3\)

然后想到了建图。以两个字符作为一个点,从 \(s_1s_2\) 到 \(s_2s_3\) 计算出会增加 \(w\) 权值,然后连一条权值为 \(w\) 的边。这样就代表在串中增加了字符 \(s_3\)。

如果这样连出来的图有正环,答案为 \(\infin\),否则跑一个最长路就行了。

但是我们一开始加入堆两个字符怎么办?可以建一个虚拟节点,连向每个点,边权时这两个字符带来的代价。

有负权边,跑spfa,复杂度 \(26^5\)(边只有 \(26^3\)条)

#include<bits/stdc++.h>
using namespace std;
const int N=2e4+5,M=805;
string str;
char ss[5];
int n,p[N],mp[1000005],v[N],hd[N],e_num,q[N*M],c[N],l,r,cnt[N];
long long ans=-1e18,f[N];
struct edge{
int v,nxt;
long long w;
}e[M*M];
int dfs(int x)
{
v[x]=1;
for(int i=hd[x];i;i=e[i].nxt)
{
if(f[x]+e[i].w>f[e[i].v])
{
f[e[i].v]=f[x]+e[i].w;
if(v[e[i].v]>0||dfs(e[i].v))
return 1;
}
}
v[x]=-1;
return 0;
}
void add_edge(int u,int v,long long w)
{
// if(u==676&&v==77)
// printf("%d %d %d\n",u,v,w);
// if(w>0)
// printf("%c%c%c\n",u/26+'a'-1,u%26+'a',v%26+'a');
e[++e_num]=(edge){v,hd[u],w};
hd[u]=e_num;
}
void spfa(int x)
{
v[q[l=r=1]=x]=1;
while(l<=r)
{
for(int i=hd[q[l]];i;i=e[i].nxt)
{
if(f[e[i].v]<f[q[l]]+e[i].w)
{
f[e[i].v]=f[q[l]]+e[i].w;
cnt[e[i].v]++;
if(cnt[e[i].v]>20000)
{
puts("Infinity");
exit(0);
}
if(!v[e[i].v])
v[e[i].v]=1,q[++r]=e[i].v;
}
}
v[q[l++]]=0;
}
// printf("%d\n",f[27]);
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s%d",ss,p+i);
int k=strlen(ss);
if(k==1)
mp[ss[0]-'a'+1]=p[i];
else if(k==2)
mp[(ss[0]-'a'+1)*27+ss[1]-'a'+1]=p[i];
else
mp[(ss[0]-'a'+1)*729+(ss[1]-'a'+1)*27+ss[2]-'a'+1]=p[i];
}
for(int i=1;i<=26;i++)
ans=max(ans,mp[i]*1LL);
for(int i=1;i<=26;i++)
{
for(int j=0;j<26;j++)
{
for(int k=0;k<26;k++)
{
add_edge(i*26+j,(j+1)*26+k,1LL*mp[k+1]+mp[(j+1)*27+k+1]+mp[i*729+(j+1)*27+k+1]);
}
}
}
// printf("%d \n",mp[1]);
for(int i=1;i<=26;i++)
{
for(int j=0;j<26;j++)
{
add_edge(0,i*26+j,1LL*mp[j+1]+mp[i]+mp[i*27+j+1]);
}
}
// for(int i=hd[27];i;i=e[i].nxt)
// printf("%c%c %lld\n",e[i].v/26-1+'a',e[i].v%26+'a',e[i].w);
memset(f,-0x7f,sizeof(f));
memset(v,f[0]=0,sizeof(v));
spfa(0);
for(int i=1;i<=26;i++)
for(int j=0;j<26;j++)
ans=max(ans,f[i*26+j]);
// printf("%d\n",f[27]);
printf("%lld",ans);
// for(int i=1;i<=26;i++)
// for(int j=0;j<26;j++)
// if(f[i*26+j])
// printf("%c%c %d\n",i+'a'-1,j+'a',f[i*26+j]);
return 0;
}

[ABC264G] String Fair的更多相关文章

  1. Codeforces Round #526 (Div. 2) C. The Fair Nut and String

    C. The Fair Nut and String 题目链接:https://codeforces.com/contest/1084/problem/C 题意: 给出一个字符串,找出都为a的子序列( ...

  2. C. The Fair Nut and String 递推分段形dp

    C. The Fair Nut and String 递推分段形dp 题意 给出一个字符串选择一个序列\({p_1,p_2...p_k}\)使得 对于任意一个\(p_i\) , \(s[p_i]==a ...

  3. CF1083B The Fair Nut and String

    题意 给出两个长度为n的01字符串S和T. 选出k个字典序在S和T之间的长度为n的01字符串,使得尽可能多的字符串满足其是所选字符串中至少一个串的前缀. 这是一道思路比较奇怪的类似计数dp的题. 首先 ...

  4. Codeforces Round #526 C - The Fair Nut and String /// 组合递推

    题目大意: 给定原字符序列 找出其中所有子序列满足 1.序列内字符都为a 2.若有两个以上的字符 则相邻两个字符在原序列中两者之间存在字符b 的数量 将整个字符序列用b分开 此时再得到每个b之间a的数 ...

  5. Codeforces CF#628 Education 8 F. Bear and Fair Set

    F. Bear and Fair Set time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  6. java线程 公平锁 ReentrantLock(boolean fair)

    一.公平锁 1.为什么有公平锁 CPU在调度线程的时候是在等待队列里随机挑选一个线程,由于这种随机性所以是无法保证线程先到先得的(synchronized控制的锁就是这种非公平锁).但这样就会产生饥饿 ...

  7. Fair Scheduler中的Delay Schedule分析

    延迟调度的主要目的是提高数据本地性(data locality),减少数据在网络中的传输.对于那些输入数据不在本地的MapTask,调度器将会延迟调度他们,而把slot分配给那些具备本地性的MapTa ...

  8. BZOJ3540: [Usaco2014 Open]Fair Photography

    3540: [Usaco2014 Open]Fair Photography Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 72  Solved: 29 ...

  9. codeforces 628F. Bear and Fair Set 网络流

    题目链接 F. Bear and Fair Set time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  10. codeforces 897A Scarborough Fair 暴力签到

    codeforces 897A Scarborough Fair 题目链接: http://codeforces.com/problemset/problem/897/A 思路: 暴力大法好 代码: ...

随机推荐

  1. WPF学习 - 闭坑(持续更新)

    坑1:自定义控件设计原则: 既然称之为控件,那么就必定有界面与行为两部分. 界面就是展示给用户看的,用于承载类的属性.方法.事件等. 行为就是类的方法,以及这些方法需要用到的属性.字段等. WPF设计 ...

  2. c++中的宏#define用途

    宏的一些作用,包括但不限于这些 定义一个变量.字符串.类型 定义一个函数.条件表达式 条件编译.调试信息,异常类 定义结构体.命名空间 定义模版.枚举.函数对象 #define宏定义在C++中用于定义 ...

  3. 微信小程序2--WXML与WXSS

    编辑WXML文件 我们在开发者工具里打开之前修改的模板小程序home文件夹下的home.wxml,里面有如下代码 <!--pages/home/home.wxml--> <text& ...

  4. [Maven] maven插件系列之maven-shade-plugin

    [Maven] maven插件系列之maven-shade-plugin 1 插件简述/Plugin Overview 1.1 定义与目的/Definition & Goals Officia ...

  5. 吴恩达人工智能-python实现逻辑回归

    吴恩达人工智能 逻辑回归python代码实现 逐行注释 import numpy as np import pandas as pd from matplotlib import pyplot as ...

  6. Vue3搭建后台管理系统模板

    搭建后台管理系统模板 2.1项目初始化 今天来带大家从0开始搭建一个vue3版本的后台管理系统.一个项目要有统一的规范,需要使用eslint+stylelint+prettier来对我们的代码质量做检 ...

  7. Spring Boot 目录遍历--表达式注入--代码执行--(CVE-2021-21234)&&(CVE-2022-22963)&&(CVE-2022-22947)&&(CVE-2022-2296)

    Spring Boot 目录遍历--表达式注入--代码执行--(CVE-2021-21234)&&(CVE-2022-22963)&&(CVE-2022-22947)& ...

  8. ptaCCF

    返回首页 English站点地图联系我们常见问题CCF招聘登录 加入CCF 计算机 CCF简介   中国计算机学会(CCF)成立于1962年,全国性学会,独立社团法人,中国科学技术协会成员. CCF是 ...

  9. 黄金眼PAAS化数据服务DIFF测试工具的建设实践

    一.背景介绍 黄金眼PAAS化数据服务是一系列实现相同指标服务协议的数据服务,各个服务间按照所生产指标的主题作划分,比如交易实时服务提供实时交易指标的查询,财务离线服务提供离线财务指标的查询.黄金眼P ...

  10. Maze 1D 题解

    题目大意 在数轴上给定一串行动指令,类型有两种:向左移动一个单位 / 向右移动一个单位.要求最后一步到达一个没有到达过的位置.可以在数轴上放置若干个障碍物阻碍移动,问在放置的障碍物最少的情况下有多少放 ...