首页 > 其他分享 >[ABC264G] String Fair

[ABC264G] String Fair

时间:2022-12-23 20:23:15浏览次数:40  
标签:10 20 String Fair int beauty times ABC264G string

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;
}

标签:10,20,String,Fair,int,beauty,times,ABC264G,string
From: https://www.cnblogs.com/mekoszc/p/17001545.html

相关文章

  • IllegalArgumentException: Wildcard string cannot be null or empty. Make sure per
    IllegalArgumentException:Wildcardstringcannotbenullorempty.Makesurepermissionstringsareproperlyformatted异常解决办法一.异常现象我在ssm项目中整合s......
  • 记录:去除list<Map<String,Object>>中主键重复的map
    /**mapKey 主键key**/publicstaticList<Map<String,Object>>removeRepeatMapByKey(List<Map<String,Object>>list,StringmapKey){List<Map<String,Object>......
  • Using Python to Check If List of Words in String
    TocheckifalistofwordsisinastringusingPython,theeasiestwayiswithlistcomprehension.>>>list_of_words=["this","words","string"]>>>string=......
  • Cstring转string
    //第一种方式:CStringstr=_T("CSDN");USES_CONVERSION;std::strings(W2A(str));//第二种方式:CStringstr=_T("CSDN");std::strings=(CT2A)str;string转CstringCS......
  • String和int互相转化
    String和int互相转化(java)1如何将字串String转换成整数int?A.有两个方法:1、 inti=Integer.parseInt([String]); 或 i=Integer.parseInt([String]......
  • 异常String不可以转换为Integer处理
    出现异常String不可以转换为Integerjava.lang.Stringcannotbecasttojava.lang.Integerjava.lang.Stringcannotbecasttojava.lang.Integer解决方案_「已注销」......
  • MFC中的CString类使用方法指南
    MFC中的CString类使用方法指南codeproject:CStringManagement【禾路:这是一篇比较老的资料了,但是对于MFC的程序设计很有帮助。我们在MFC中使用字符串的相关操作,首先想到的就......
  • String.contains空指针异常
    今天在写业务代码的时候,大致如下   然后a.contains报了空指针异常,让我很是诧异结果发现,是缓存获取到了一个nullnull.contains这种样子就会出现空指针......
  • 标准 C++ 中的 string 类的用法总结
     相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用。但是如果离开了MFC框架,还有没有这样使用起来......
  • Generates Regular Expressions That Match A Set of Strings
    GitHub-devongovett/regexgen:GenerateregularexpressionsthatmatchasetofstringsHowdoesitwork?GenerateaTriecontainingalloftheinputstrings.......