C. Dominant Character

该博客讨论了一道编程题目,要求找到一个由'a', 'b', 'c'组成的字符串中最短的子串,使得'a'出现的次数多于'b'和'c'。博主通过逐个检查字符串中的'a'字符,分析了不同数量'a'的情况,并给出了相应的解决方案。代码示例使用C++实现,展示了如何遍历字符串并找到满足条件的最短子串长度。

传送门

题意:给你一个字符串,这个字符串只由'a','b','c'组成,让你求最短的一个字串,满足下面三个条件:

  • 最小子串的长度至少为 2

  • ‘a’ 在这个子字符串中出现的次数比在 ‘b’ 中出现的次数要多

  • ‘a’ 在这个子字符串中出现的次数比在 ‘c’ 中出现的次数要多

让你输出这个字符串的最短长度。

思路:

从最短到最长的情况进行考虑

首先如果是a有两个的情况,aa,aXa,aXXa都能满足(两个a中间要保证'b','c'最多只出现一次)

如果是三个a,那么每两个a中间一定要有两个b或者两个c,满足的条件就只有abbacca或者accabba

如果是四个a,因为每两个a中间要有两个b或者两个c或者更多,那么无论怎么组合一定没有合法的答案。

那么只要找2个a的情况和3个a的情况的答案就可以了

直接遍历一遍,然后找每两个相邻的a,如果中间两个是相同的就再往后判断一个。

代码:


#include<cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include<vector>
#include<queue>
#include<map>
#define sc_int(x) scanf("%d", &x)
#define sc_ll(x) scanf("%lld", &x)
#define pr_ll(x) printf("%lld", x)
#define pr_ll_n(x) printf("%lld\n", x)
#define pr_int_n(x) printf("%d\n", x)
#define ll long long 
using namespace std;

const int N=2000000+100;
int n ,m,h;
char s[N];

void solve()
{
	cin>>n>>s+1;

	int res=1e9;
	int l=0,r=0;
	for(int i =1;i<=n;i++)
	{
		if(s[i]=='a')
		{

			if(!l)l=i;
			else {
				r=i;
				int b=0,c=0;

				for(int j =l+1;j<r;j++)
				if(s[j]=='b')b++;
				else c++;
				
				if(max(b,c)<2)
				res=min(res,r-l+1);
				else {
					if(((b==2&&c==0&&s[r+1]=='c'&&s[r+2]=='c'&&s[r+3]=='a')||(c==2&&b==0&&s[r+1]=='b'&&s[r+2]=='b'&&s[r+3]=='a'))&&r+3<=n)
					res=min(res,7);
				}
				l=i;
			}
		}
	}
	if(res!=1e9)
	cout<<res<<endl;
	else cout<<"-1\n";
	
	return ;
}


int main()
{
	int t;
	sc_int(t);
	while(t--)solve();
	
	return 0;
}

Event Names Here is a list of all EventHandler events that the controller uses, along with a brief description of what each event is used for and its parameters, if it has any: Event Name Parameters Description OnAbilityActive Ability ability, bool active The has started or stoppedability OnAbilityMessageCanStart Ability ability, bool canStart The can start or no longer stop.ability OnAbilityUnequipItemComplete CharacterItem item, int slotID The has been unequipped from the slot .itemslotID OnAbilityWillEquipItem CharacterItem item, int slotID The will be equipped in the slot .itemslotID OnAddCrosshairsSpread bool start, bool fromRecoil The crosshair spread should be started/stopped, either from recoil or set to max spread. OnAddSecondaryCameraForce Vector3 positionalForce, Vector3 rotationalForce, float restAccumulation Adds a secondary positional and rotational force to the ViewTypes. OnAddSecondaryForce int slotID, Vector3 positionalForce, Vector3 rotationalForce, bool globalForce Adds a positional and rotational force to the IK target. OnAimAbilityAim bool aim The aim ability is/is not aiming. OnAimAbilityStart bool aim, bool inputStart The aim ability was started/stopped. describes whether it was started/stopped from user input.inputStart OnAnimatorSnapped The animator has been snapped. OnAnimatorWillSnap The animator will snap. OnAnchorOffsetUpdated The camera’s anchor offset value has been changed. OnAttributeModifierAutoUpdateEnabled AttributeModifier attributeModifier, bool enable The ‘s auto-update has been enabled/disabled.attributeModifier OnAttributeReachedDestinationValue An attribute has reached its destination value. OnAttributeUpdateValue Attribute attribute The ‘s value has been changed.attribute OnCameraAttachCharacter GameObject character The has been attached to the camera.character OnCameraChangePerspectives bool firstPersonPerspective The camera has swithced between first and third person perspectives. OnCameraChangeViewTypes ViewType viewType, bool activate The was activated/deactivated.viewType OnCameraRotationalForce Vector3 force Add the rotational to the camera.force OnCameraWillChangePerspectives bool firstPersonPerspective The camera will switch between first and third person perspectives. OnCameraZoom bool zoom The camera has zoomed in/out. OnCharacterAbilityActive Ability ability, bool active The has been activated/deactivated.ability OnCharacterActivate bool active The character itself has been activated/deactivated. OnCharacterAttachCamera CameraController cameraController The has been attached to the character.cameraController OnCharacterAttachLookSource ILookSource lookSource The has been attached to the character.ILookSource OnCharacterChangeMovementType MovementType movementType The character’s movement type will be changed to .movementType OnCharacterChangeMovingPlatforms Transform platform The character has been attached to the moving .platform OnCharacterChangePerspectives bool firstPersonPerspective The camera will switch between first and third person perspectives. OnCharacterChangeTimeScale bool value The character’s time scale will be changed to .value OnCharacterChangeUpdateLocation UpdateLocation updateLocation The character’s update location will be changed to the specified .updateLocation OnCharacterDestroyed The Ultimate Character Locomotion component has been destroyed. OnCharacterEnterUIZone bool inUIZone The character has entered/exited a zone which should show the UI and cursor. OnCharacterForceIndependentLook bool independentLook Independent look will be enabled/disabled on the character to allow the ability to have control over rotation. OnCharacterGrounded bool grounded The character’s grounded state has changed to .grounded OnCharacterImmediateTransformChange bool snapAnimator The character’s transform position/rotation has been immediately changed. OnCharacterIndependentFade bool enable, bool revertFade Another object has started or stopped taking control of the character fade. OnCharacterItemAbilityActive ItemAbility itemAbility, bool active The has been activated/deactivated.itemAbility OnCharacterLand float height The character has landed from .height OnCharacterLean float distance, float tilt, float itemTiltMultiplier The character is leaning the specified and amount.distancetilt OnCharacterMoving bool moving The character has either started or stopped moving. OnCharacterSnapAnimator The character’s animator should be snapped to match its current state. OnDeath Vector3 position, Vector3 force, GameObject attacker An object with the Health component has died. See .OnHealthDamage OnEnableGameplayInput bool enable Gameplay input should be enabled/disabled. OnEquipUnequipItemSetIndexChange int itemSetIndex The active ItemSet index has changed to .itemSetIndex OnEquipUnequipVerifyUnequipItem int itemSetCategoryIndex, int slotID Verify that the unequipping item specified by the in the slot still needs to be unequipped.itemSetCategoryIndexslotID OnFirstPersonPerspectiveActivate FirstPersonPerspectiveItem perspectiveItem, bool active The first person perspective item has been activated/deactivated. OnHealthDamage float amount, Vector3 position, Vector3 force, GameObject attacker, Collider hitCollider Damage has been applied to the Health component. OnHealthHeal float amount The Health component has been healed by .amount OnHeightChangeAdjustHeight float heightAdjustment The character’s height has been adjusted by the specified amount. OnInputControllerConnected bool controllerConnected A controller has been connected/disconnected. OnInventoryAddItem CharacterItem item The has been added to the inventory.item OnInventoryAdjustItemIdentifierAmount IItemIdentifier itemIdentifier, int remaining The amount of the item has been adjusted, leaving of that item left.itemIdentifierremaining OnInventoryEquipItem CharacterItem item, int slotID The in slot has been equipped.itemslotID OnInventoryPickupItem CharacterItem item, int amount, bool immediatePickup, bool forceEquip The of has been picked up and will/will not be picked up immediately and forcefully equipped.amountitem OnInventoryPickupItemIdentifier IItemIdentifier itemIdentifier, int amount, bool immediatePickup, bool forceEquip The of item has been picked up and will/will not be picked up immediately and forcefully equipped.amountitemIdentifier OnInventoryRemoveItem OnInventoryRespawned The inventory has respawned. OnInventoryUnequipItem CharacterItem item, int slotID The in slot has been unequipped.itemslotID OnItemPickupStartPickup An ItemPickup has been started to be picked up. OnItemPickupStopPickup An ItemPickup has been stopped to be picked up. OnItemSetIndexChange int categoryIndex, int itemSetIndex The ItemSetManager has changed the category ‘s active ItemSet index to .categoryIndexitemSetIndex OnItemSetManagerUpdateItemSet int categoryIndex, int itemSetIndex Updates the currently active item set in the category to .categoryIndexitemSetIndex OnItemSetManagerUpdateNextItemSet int categoryIndex, int prevItemSetIndex, int itemSetIndex Updates the next item set in category , including the previous and next item set indices.categoryIndex OnItemShowFullScreenUI int fullScreenUIID, bool showFullScreenUI Shows/hides the full screen UI object based on its UI ID. OnItemStartUse IUsableItem item, bool use The is starting/stopping use.item OnItemTryReload int slotID, IItemIdentifier itemIdentifier, bool immediateReload, bool equipCheck The item at slot is trying to reload using .slotIDitemIdentifier OnItemUpdateDominantItem CharacterItem item, bool dominantItem Sets the as the dominant/not dominant item.item OnMagicItemCast CharacterItem item The magic has cast an action.item OnMagicItemImpact CharacterItem item, SurfaceImpact surfaceImpact The magic has collided with a surface.item OnMagicItemStartStopBeginEndActions CharacterItem item, bool beginActions, bool start A magic ‘s begin/end actions have been started/stopped.item OnNextItemSet CharacterItem item, bool unequipOnFailure The wants to change to equip the next item from the item set.item OnObjectImpact ImpactCallbackContext ctx An object has been impacted by another object. OnObjectPickedUp ObjectPickup objectPickup The has been picked up.objectPickup OnRespawn The object has respawned. OnShieldImpact Shield shield, object source The has been impacted by .shieldobject OnShootableWeaponShowProjectile GameObject projectile, bool show The ShootableWeapon ItemAction has shown or hidden a .projectile OnShowUI bool show The UI should be shown/hidden. OnStateChange GameObject gameObject, string stateName, bool active The has activated/deactivated the state.gameObjectstateName OnStoredInputAbilityResetStoredInputs Resets the ability’s stored user inputs. OnTryRecenterTracking VR only – The camera’s rotation has changed, so tracking recentering should occur. OnUseAbilityStart bool start, Use useAbility The specified has been started/stopped.useAbility OnUseAbilityUsedItem IUsableItem item The Use ability has used .item OnWillRespawn The object will respawn.把这些和上面的结合起来看,如果我要监控某一个Ability的启动怎么处理
最新发布
09-30
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值