cf#329-A. 2Char-烦人的水题

本文介绍了一种针对特定条件下的字符串匹配算法,旨在从一系列由26个字母组成的单词中选取若干个单词,使得组合后的字符串中不同的字母数量不超过两种,并输出最长的字符串长度。通过预处理排除不合适的单词并采用双遍历的方法来实现这一目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

给你n个由26个字母写成的单词


选出 k个使得 这些单词拼起来的文章中 不同字母数不超过 2,输出最长的文章长度


for 一遍把 单词中 字母种类大于2的单词标记掉


接着对于每一个 有两种字母的单词

以该被选单词为基准  for 一遍 从1到n ,看有多少个单词能被选上  (即其他单词中 没出现 基准单词中没的字母)

不断更新长度


开一个vis数组【26】

然后对每一个 只有一种字母的单词  把 其 长度 累加在 vis【该字母】

最后取  长度最长的两个字母


两种情况答案取最大


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <set>
#include <vector>
using namespace std;

vector<char> tm[105];
char tmp[1005];
int legal[106];
int max(int a,int b)
{return a<b?b:a;}
int main()
{
	int n;
	scanf("%d",&n);
	int i;
	int j;
	for (i=1;i<=n;i++)
	{
		scanf("%s",tmp);
		int len=strlen(tmp);
		int vis[27];
		memset(vis,0,sizeof(vis));
		for (j=0;j<len ;j++)
		{
			vis[tmp[j]-'a']++;
		}
		int cun=0;
		for (j=0;j<26;j++)
			if (vis[j]) cun++;
			
			if (cun>2) legal[i]=-1;
			else
			{
				for (j=0;j<26;j++)
					if (vis[j]) 
						tm[i].push_back(j+'a');
					legal[i]=len;
					
			}
			
	}
	
	
	int maxx=0;
	for (i=1;i<=n;i++)		//处理两个不同字母的情况
	{
		if (legal[i]==-1) continue;
		int tmp_max=legal[i];
		if (tm[i].size()!=2) continue;
		int vis[27];
		memset(vis,0,sizeof(vis));
		int k;
		for(k=0;k<tm[i].size();k++)
		{
			vis[tm[i][k]-'a']=1;
		}
		
		for (j=1;j<=n;j++)
		{ 
			if (legal[j]==-1) continue;
			if (j==i) continue;
			int flag=0;
			for (k=0;k<tm[j].size();k++)
			{
				if (vis[tm[j][k]-'a']==0)
				{
					flag=1;
					break;
				}
			}
			if (flag)
				continue;
			else
			{
				tmp_max+=legal[j];
			} 
		}
		//if (flag) continue;
		maxx=max(maxx,tmp_max); 
		
	}
		int one[1005];
		int ok=0;
		int vis[27];
		memset(vis,0,sizeof(vis));
	for (i=1;i<=n;i++)
	{
		if (tm[i].size()!=1) continue;
		if (legal[i]==-1) continue;
		vis[tm[i][0]-'a' ]+=legal[i];
	}
	sort(vis,vis+26);
	int tmp_one=vis[25]+vis[24];
 	maxx=max(maxx,tmp_one); 
		
		
	 
	printf("%d\n",maxx);
	
	
	
	
	
	
	
	return 0;
	
}


这个配置单有问吗:# dhcpd.conf # # Sample configuration file for ISC dhcpd # # Attention: If /etc/ltsp/dhcpd.conf exists, that will be used as # configuration file instead of this file. # # option definitions common to all supported networks... # option domain-name "example.org"; # option domain-name-servers ns1.example.org, ns2.example.org; default-lease-time 60; max-lease-time 120; # The ddns-updates-style parameter controls whether or not the server will # attempt to do a DNS update when a lease is confirmed. We default to the # behavior of the version 2 packages (&#39;none&#39;, since DHCP v2 didn&#39;t # have support for DDNS.) ddns-update-style none; # If this DHCP server is the official DHCP server for the local # network, the authoritative directive should be uncommented. authoritative; # Use this to send dhcp log messages to a different log file (you also # have to hack syslog.conf to complete the redirection). #log-facility local7; # No service will be given on this subnet, but declaring it helps the # DHCP server to understand the network topology. #subnet 10.152.187.0 netmask 255.255.255.0 { #} # This is a very basic subnet declaration. subnet 192.168.50.0 netmask 255.255.255.0 { range 192.168.50.40 192.168.50.50; option routers 192.168.50.10; host soho{ hardware ethernet 08:00:07:26:c0:a5; fixed-address 192.168.50.40; } } # This declaration allows BOOTP clients to get dynamic addresses, # which we don&#39;t really recommend. #subnet 10.254.239.32 netmask 255.255.255.224 { # range dynamic-bootp 10.254.239.40 10.254.239.60; # option broadcast-address 10.254.239.31; # option routers rtr-239-32-1.example.org; #} # A slightly different configuration for an internal subnet. #subnet 10.5.5.0 netmask 255.255.255.224 { # range 10.5.5.26 10.5.5.30; # option domain-name-servers ns1.internal.example.org; # option domain-name "internal.example.org"; # option subnet-mask 255.255.255.224; # option routers 10.5.5.1; # option broadcast-address 10.5.5.31; # default-lease-time 600; # max-lease-time 7200; #} # Hosts which require special configuration options can be listed in # host statements. If no address is specified, the address will be # allocated dynamically (if possible), but the host-specific information # will still come from the host declaration. #host passacaglia { # hardware ethernet 0:0:c0:5d:bd:95; # filename "vmunix.passacaglia"; # server-name "toccata.example.com"; #} # Fixed IP addresses can also be specified for hosts. These addresses # should not also be listed as being available for dynamic assignment. # Hosts for which fixed IP addresses have been specified can boot using # BOOTP or DHCP. Hosts for which no fixed address is specified can only # be booted with DHCP, unless there is an address range on the subnet # to which a BOOTP client is connected which has the dynamic-bootp flag # set. # You can declare a class of clients and then do address allocation # based on that. The example below shows a case where all clients # in a certain class get addresses on the 10.17.224/24 subnet, and all # other clients get addresses on the 10.0.29/24 subnet. #class "foo" { # match if substring (option vendor-class-identifier, 0, 4) = "SUNW"; #} #shared-network 224-29 { # subnet 10.17.224.0 netmask 255.255.255.0 { # option routers rtr-224.example.org; # } # subnet 10.0.29.0 netmask 255.255.255.0 { # option routers rtr-29.example.org; # } # pool { # allow members of "foo"; # range 10.17.224.10 10.17.224.250; # } # pool { # deny members of "foo"; # range 10.0.29.10 10.0.29.230; # } #}
最新发布
07-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值