772. 只出现一次的字符

该博客介绍了一个C++程序,用于在一个不超过100000个字符的小写字母字符串中查找首次出现且仅出现一次的字符。程序通过计数每个字符的出现次数并遍历字符串来找到满足条件的字符。如果找到,它会立即输出该字符并结束程序;否则,输出'no'。这是一个关于字符串处理和算法的实例。

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

772. 只出现一次的字符

给你一个只包含小写字母的字符串。

请你判断是否存在只在字符串中出现过一次的字符。

如果存在,则输出满足条件的字符中位置最靠前的那个。

如果没有,输出 no

输入格式

共一行,包含一个由小写字母构成的字符串。

数据保证字符串的长度不超过 100000。

输出格式

输出满足条件的第一个字符。

如果没有,则输出 no

输入样例:
abceabcd
输出样例:
e
//存出现次数有技巧,找第一个出现的有技巧
//任何地方执行到return 0;就直接结束了


#include<iostream>

using namespace std;

int main()
{
	string a;
	getline(cin, a);
	
	int b[26] = {0};
	for (int i = 0; i < a.size(); i ++)
	{
		b[a[i] - 'a'] ++;
	}
		
	for (int i = 0; i < a.size(); i ++)
	{
		if (b[a[i] - 'a'] == 1)
		{			
			cout << a[i] << endl;
			return 0;
		}
		
	}
	cout << "no" <<endl;
	
	return 0;
}
PS C:\Users\Administrator\Desktop> # ===== 修复插件逻辑 ===== PS C:\Users\Administrator\Desktop> function Install-PthFixPlugin { >> param( >> [string]$PythonPath = "E:\Python310" >> ) >> >> $sitePackages = "$PythonPath\Lib\site-packages" >> $pluginRoot = "$sitePackages\pth_fix_plugin" >> >> # 创建插件目录结构 >> $packageDir = "$pluginRoot\pth_fix_plugin" >> New-Item -ItemType Directory -Path $packageDir -Force | Out-Null >> >> # 创建插件内容(简化日志,确保删除逻辑可靠) >> $pluginFile = "$packageDir\__init__.py" >> @' >> # pth_fix_plugin/__init__.py >> import os >> import sys >> import sysconfig >> from setuptools.command.install import install >> >> class CustomInstallCommand(install): >> """Custom install command to prevent .pth file generation""" >> >> def run(self): >> # Call original install method >> install.run(self) >> >> # Fix .pth file >> self.fix_pth_file() >> >> def fix_pth_file(self): >> """Fix the .pth file issue""" >> # 获取site-packages目录 >> site_packages = sysconfig.get_paths()["purelib"] >> pth_path = os.path.join(site_packages, 'distutils-precedence.pth') >> >> if os.path.exists(pth_path): >> try: >> os.remove(pth_path) >> print(f"✅ Successfully removed .pth file: {pth_path}") >> except Exception as e: >> print(f"❌ Failed to remove .pth file: {str(e)}") >> else: >> print("✅ No .pth file found, nothing to fix") >> '@ | Set-Content -Path $pluginFile -Encoding ASCII -Force >> >> # 创建入口点配置 >> $setupFile = "$pluginRoot\setup.py" >> @' >> from setuptools import setup >> >> setup( >> name='pth_fix_plugin', >> version='1.0.0', >> packages=['pth_fix_plugin'], >> entry_points={ >> 'distutils.commands': [ >> 'install = pth_fix_plugin:CustomInstallCommand', >> ], >> }, >> ) >> '@ | Set-Content -Path $setupFile -Encoding ASCII -Force >> >> # 创建pyproject.toml文件 >> $pyprojectFile = "$pluginRoot\pyproject.toml" >> @' >> [build-system] >> requires = ["setuptools>=40.8.0", "wheel"] >> build-backend = "setuptools.build_meta" >> '@ | Set-Content -Path $pyprojectFile -Encoding ASCII -Force >> >> # 安装插件(使用兼容模式) >> Push-Location -Path $pluginRoot >> try { >> # 尝试使用兼容模式安装 >> python -m pip install --no-deps -e . --config-settings editable_mode=compat --no-warn-script-location >> } catch { >> # 回退到旧版安装方式 >> python -m pip install --no-deps -e . --use-pep517 false --no-warn-script-location >> } >> Pop-Location >> >> Write-Host "✅ pth_fix plugin installed successfully" -ForegroundColor Green >> } PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # ===== 更新验证函数 ===== PS C:\Users\Administrator\Desktop> function Test-FullEnvironment { >> param( >> [string]$PythonPath = "E:\Python310" >> ) >> >> # 验证核心组件 >> python -c "import torch, torchvision, torchaudio, numpy as np, modelscope; print(f'PyTorch: {torch.__version__}\nTorchVision: {torchvision.__version__}\nTorchAudio: {torchaudio.__version__}\nNumPy: {np.__version__}\nAudio Backend: {torchaudio.get_audio_backend()}\nModelScope: {modelscope.__version__}')" >> >> # 验证警告 >> $warnings = python -c "import warnings; warnings.filterwarnings('error'); import modelscope" 2>&1 >> if ($LASTEXITCODE -eq 0) { >> Write-Host "✅ Environment has no warnings" -ForegroundColor Green >> } else { >> Write-Host "❌ Environment has warnings: $warnings" -ForegroundColor Red >> } >> >> # 验证.pth文件 >> $sitePackages = "$PythonPath\Lib\site-packages" >> if (Test-Path $sitePackages) { >> $pthFiles = Get-ChildItem $sitePackages -Filter "distutils-precedence.pth" -ErrorAction SilentlyContinue >> >> if ($pthFiles) { >> Write-Host "⚠️ Found .pth file: $($pthFiles.FullName)" -ForegroundColor Yellow >> } else { >> Write-Host "✅ No distutils-precedence.pth file found" -ForegroundColor Green >> } >> >> # 验证插件状态(使用importlib.metadata替代pkg_resources) >> python -c @" >> import sys >> from importlib.metadata import entry_points >> >> try: >> eps = entry_points(group='distutils.commands', name='install') >> if eps: >> print(f'✅ Install command hook: {eps[0].value}') >> else: >> print('❌ Install command not hooked') >> except Exception as e: >> print(f'❌ Error checking entry points: {str(e)}') >> "@ >> } else { >> Write-Host "❌ Path does not exist: $sitePackages" -ForegroundColor Red >> } >> } PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # ===== 执行最终修复 ===== PS C:\Users\Administrator\Desktop> # 1. 确保插件目录完全删除 PS C:\Users\Administrator\Desktop> $pluginPath = "E:\Python310\Lib\site-packages\pth_fix_plugin" PS C:\Users\Administrator\Desktop> if (Test-Path $pluginPath) { >> Remove-Item -Path $pluginPath -Recurse -Force >> Write-Host "Removed old plugin directory: $pluginPath" -ForegroundColor Cyan >> } Removed old plugin directory: E:\Python310\Lib\site-packages\pth_fix_plugin PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # 2. 重新安装插件 PS C:\Users\Administrator\Desktop> Install-PthFixPlugin -PythonPath "E:\Python310" Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Obtaining file:///E:/Python310/Lib/site-packages/pth_fix_plugin Installing build dependencies ... done Checking if build backend supports build_editable ... done Getting requirements to build editable ... done Preparing editable metadata (pyproject.toml) ... done Building wheels for collected packages: pth_fix_plugin Building editable for pth_fix_plugin (pyproject.toml) ... done Created wheel for pth_fix_plugin: filename=pth_fix_plugin-1.0.0-0.editable-py3-none-any.whl size=1544 sha256=1501f319989c05fb8faa6c4a35b2dd7e52b2a674c83e1c38d15b80937305ae51 Stored in directory: C:\Users\Administrator\AppData\Local\Temp\pip-ephem-wheel-cache-tvgjwmqr\wheels\c7\cc\8f\a13f1f3c2e3c649409279b7caed95e5210d22da47147eaf764 Successfully built pth_fix_plugin Installing collected packages: pth_fix_plugin Attempting uninstall: pth_fix_plugin Found existing installation: pth_fix_plugin 1.0.0 Uninstalling pth_fix_plugin-1.0.0: Successfully uninstalled pth_fix_plugin-1.0.0 Successfully installed pth_fix_plugin-1.0.0 ✅ pth_fix plugin installed successfully PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # 3. 验证环境 PS C:\Users\Administrator\Desktop> Test-FullEnvironment -PythonPath "E:\Python310" PyTorch: 2.0.0+cpu TorchVision: 0.15.1+cpu TorchAudio: 2.0.1+cpu NumPy: 1.26.4 Audio Backend: soundfile ModelScope: 1.29.0 ✅ Environment has no warnings ⚠️ Found .pth file: E:\Python310\Lib\site-packages\distutils-precedence.pth <string>:7: DeprecationWarning: Accessing entry points by index is deprecated. Cast to tuple if needed. ✅ Install command hook: pth_fix_plugin:CustomInstallCommand PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # 4. 测试修复持久性 PS C:\Users\Administrator\Desktop> python -m pip install --upgrade setuptools --force-reinstall Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting setuptools Using cached https://pypi.tuna.tsinghua.edu.cn/packages/a3/dc/17031897dae0efacfea57dfd3a82fdd2a2aeb58e0ff71b77b87e44edc772/setuptools-80.9.0-py3-none-any.whl (1.2 MB) Installing collected packages: setuptools Attempting uninstall: setuptools Found existing installation: setuptools 80.9.0 Uninstalling setuptools-80.9.0: Successfully uninstalled setuptools-80.9.0 Successfully installed setuptools-80.9.0 PS C:\Users\Administrator\Desktop> Test-FullEnvironment -PythonPath "E:\Python310" PyTorch: 2.0.0+cpu TorchVision: 0.15.1+cpu TorchAudio: 2.0.1+cpu NumPy: 1.26.4 Audio Backend: soundfile ModelScope: 1.29.0 ✅ Environment has no warnings ⚠️ Found .pth file: E:\Python310\Lib\site-packages\distutils-precedence.pth <string>:7: DeprecationWarning: Accessing entry points by index is deprecated. Cast to tuple if needed. ✅ Install command hook: pth_fix_plugin:CustomInstallCommand PS C:\Users\Administrator\Desktop> # 安装插件 PS C:\Users\Administrator\Desktop> ✅ pth_fix plugin installed successfully ✅ : 无法将“✅”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后 再试一次。 所在位置 行:1 字符: 1 + ✅ pth_fix plugin installed successfully + ~ + CategoryInfo : ObjectNotFound: (✅:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # 验证环境 PS C:\Users\Administrator\Desktop> PyTorch: 2.0.0+cpu PyTorch: : 无法将“PyTorch:”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保 路径正确,然后再试一次。 所在位置 行:1 字符: 1 + PyTorch: 2.0.0+cpu + ~~~~~~~~ + CategoryInfo : ObjectNotFound: (PyTorch::String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\Administrator\Desktop> TorchVision: 0.15.1+cpu TorchVision: : 无法将“TorchVision:”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径 ,请确保路径正确,然后再试一次。 所在位置 行:1 字符: 1 + TorchVision: 0.15.1+cpu + ~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (TorchVision::String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\Administrator\Desktop> TorchAudio: 2.0.1+cpu TorchAudio: : 无法将“TorchAudio:”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径, 请确保路径正确,然后再试一次。 所在位置 行:1 字符: 1 + TorchAudio: 2.0.1+cpu + ~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (TorchAudio::String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\Administrator\Desktop> NumPy: 1.26.4 NumPy: : 无法将“NumPy:”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径 正确,然后再试一次。 所在位置 行:1 字符: 1 + NumPy: 1.26.4 + ~~~~~~ + CategoryInfo : ObjectNotFound: (NumPy::String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\Administrator\Desktop> Audio Backend: soundfile Audio : 无法将“Audio”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正 确,然后再试一次。 所在位置 行:1 字符: 1 + Audio Backend: soundfile + ~~~~~ + CategoryInfo : ObjectNotFound: (Audio:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\Administrator\Desktop> ModelScope: 1.29.0 ModelScope: : 无法将“ModelScope:”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径, 请确保路径正确,然后再试一次。 所在位置 行:1 字符: 1 + ModelScope: 1.29.0 + ~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (ModelScope::String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\Administrator\Desktop> ✅ Environment has no warnings ✅ : 无法将“✅”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后 再试一次。 所在位置 行:1 字符: 1 + ✅ Environment has no warnings + ~ + CategoryInfo : ObjectNotFound: (✅:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\Administrator\Desktop> ✅ No distutils-precedence.pth file found ✅ : 无法将“✅”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后 再试一次。 所在位置 行:1 字符: 1 + ✅ No distutils-precedence.pth file found + ~ + CategoryInfo : ObjectNotFound: (✅:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\Administrator\Desktop> ✅ Install command hook: pth_fix_plugin:CustomInstallCommand ✅ : 无法将“✅”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后 再试一次。 所在位置 行:1 字符: 1 + ✅ Install command hook: pth_fix_plugin:CustomInstallCommand + ~ + CategoryInfo : ObjectNotFound: (✅:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # 测试持久性 PS C:\Users\Administrator\Desktop> Successfully installed setuptools-80.9.0 Successfully : 无法将“Successfully”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径 ,请确保路径正确,然后再试一次。 所在位置 行:1 字符: 1 + Successfully installed setuptools-80.9.0 + ~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Successfully:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\Administrator\Desktop> PyTorch: 2.0.0+cpu PyTorch: : 无法将“PyTorch:”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保 路径正确,然后再试一次。 所在位置 行:1 字符: 1 + PyTorch: 2.0.0+cpu + ~~~~~~~~ + CategoryInfo : ObjectNotFound: (PyTorch::String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\Administrator\Desktop> TorchVision: 0.15.1+cpu TorchVision: : 无法将“TorchVision:”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径 ,请确保路径正确,然后再试一次。 所在位置 行:1 字符: 1 + TorchVision: 0.15.1+cpu + ~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (TorchVision::String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\Administrator\Desktop> TorchAudio: 2.0.1+cpu TorchAudio: : 无法将“TorchAudio:”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径, 请确保路径正确,然后再试一次。 所在位置 行:1 字符: 1 + TorchAudio: 2.0.1+cpu + ~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (TorchAudio::String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\Administrator\Desktop> NumPy: 1.26.4 NumPy: : 无法将“NumPy:”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径 正确,然后再试一次。 所在位置 行:1 字符: 1 + NumPy: 1.26.4 + ~~~~~~ + CategoryInfo : ObjectNotFound: (NumPy::String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\Administrator\Desktop> Audio Backend: soundfile Audio : 无法将“Audio”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正 确,然后再试一次。 所在位置 行:1 字符: 1 + Audio Backend: soundfile + ~~~~~ + CategoryInfo : ObjectNotFound: (Audio:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\Administrator\Desktop> ModelScope: 1.29.0 ModelScope: : 无法将“ModelScope:”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径, 请确保路径正确,然后再试一次。 所在位置 行:1 字符: 1 + ModelScope: 1.29.0 + ~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (ModelScope::String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\Administrator\Desktop> ✅ Environment has no warnings ✅ : 无法将“✅”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后 再试一次。 所在位置 行:1 字符: 1 + ✅ Environment has no warnings + ~ + CategoryInfo : ObjectNotFound: (✅:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\Administrator\Desktop> ✅ No distutils-precedence.pth file found ✅ : 无法将“✅”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后 再试一次。 所在位置 行:1 字符: 1 + ✅ No distutils-precedence.pth file found + ~ + CategoryInfo : ObjectNotFound: (✅:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\Administrator\Desktop> ✅ Install command hook: pth_fix_plugin:CustomInstallCommand ✅ : 无法将“✅”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后 再试一次。 所在位置 行:1 字符: 1 + ✅ Install command hook: pth_fix_plugin:CustomInstallCommand + ~ + CategoryInfo : ObjectNotFound: (✅:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\Administrator\Desktop>
最新发布
08-23
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海阔平

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值