1084 Broken Keyboard (20 point(s))

本文介绍了一种算法,用于识别在损坏键盘上输入时丢失的字符。通过比较预期字符串和实际输入的字符串,该算法能有效找出并列出所有损坏的键。

1084 Broken Keyboard (20 point(s))

On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters corresponding to those keys will not appear on screen.

Now given a string that you are supposed to type, and the string that you actually type out, please list those keys which are for sure worn out.

Input Specification:

Each input file contains one test case. For each case, the 1st line contains the original string, and the 2nd line contains the typed-out string. Each string contains no more than 80 characters which are either English letters [A-Z] (case insensitive), digital numbers [0-9], or _ (representing the space). It is guaranteed that both strings are non-empty.

Output Specification:

For each test case, print in one line the keys that are worn out, in the order of being detected. The English letters must be capitalized. Each worn out key must be printed once only. It is guaranteed that there is at least one worn out key.

Sample Input:

7_This_is_a_test
_hs_s_a_es

Sample Output:

7TI

注意点:1.STL set的使用;2.<ctype.h>可以很大程度上帮助我们节省编码时间。

#include<iostream>
#include<cstring>
#include<set>
#include<ctype.h>
using namespace std;
void upper(string &s){
	for(int i=0;i<s.length();i++){
		if(islower(s[i])) s[i]=toupper(s[i]);
	}
}
int main(void){
	string s1,s2;
	cin>>s1>>s2;
	set<char> s;
	upper(s1);upper(s2);
	int i,j;
	for(i=0,j=0;i<s1.length()&&j<s2.length();i++){
		if(s1[i]==s2[j]) j++;
		else{
			if(s.find(s1[i])==s.end()){
				cout<<s1[i];
				s.insert(s1[i]);
			}
		}
	}
	while(i<s1.length()){
		if(s.find(s1[i])==s.end()){
			cout<<s1[i];
			s.insert(s1[i]);
		}
		i++;
	}
	cout<<endl;return 0;
} 

 

在使用 `multiprocessing.Pool` 时,可能会遇到多种错误,例如进程无法启动、任务执行失败、通信问题等。以下是常见的错误原因及对应的解决方案。 ### 1. **PicklingError: Can't pickle ...** `multiprocessing.Pool` 使用 `pickle` 模块将函数和参数序列化以在进程间传递。如果传递的函数或参数无法被序列化,就会抛出 `PicklingError`。 **解决方案:** - 确保传递给 `Pool` 的函数是全局可访问的,而不是定义在某个函数或 `lambda` 表达式中。 - 避免传递无法被 `pickle` 序列化的对象,如打开的文件句柄、线程锁等。 - 使用 `dill` 等第三方库替代 `pickle`,以支持更复杂的对象序列化。 ```python from multiprocessing import Pool def my_function(x): return x * x if __name__ == '__main__': with Pool(4) as p: result = p.map(my_function, [1, 2, 3, 4]) print(result) ``` ### 2. **AssertionError: daemonic processes are not allowed to have children** 这个错误通常发生在 Windows 系统上,当尝试在 `Pool` 中使用嵌套的多进程操作时,即一个子进程又创建了新的子进程。 **解决方案:** - 避免在 `Pool` 的工作进程中再次创建新的 `Pool` 或 `Process`。 - 如果需要多级并行,考虑重构代码逻辑,将多级并行转换为单级并行。 - 在 Windows 上,确保 `if __name__ == '__main__':` 保护主模块,以防止子进程重复导入主模块。 ### 3. **TimeoutError: operation timed out** 当调用 `Pool.apply_async()` 或 `Pool.map_async()` 时,如果设置了超时时间并且任务未在指定时间内完成,就会抛出 `TimeoutError`。 **解决方案:** - 检查任务是否陷入死循环或长时间阻塞。 - 适当增加超时时间,确保任务有足够的时间完成。 - 使用 `AsyncResult.ready()` 方法检查任务是否完成,而不是立即调用 `get()`。 ```python from multiprocessing import Pool import time def long_task(seconds): time.sleep(seconds) return f"Done after {seconds} seconds" if __name__ == '__main__': with Pool(2) as p: result = p.apply_async(long_task, (5,)) try: print(result.get(timeout=3)) # 设置超时时间为3秒 except TimeoutError: print("The task took too long to complete.") ``` ### 4. **BrokenPipeError: [Errno 32] Broken pipe** 该错误通常发生在主进程或子进程意外终止时,导致进程间通信的管道被破坏。 **解决方案:** - 确保所有进程正常退出,避免在任务中抛出未捕获的异常。 - 使用 `try...except` 捕获异常,确保任务能够正常返回结果。 - 在 `Pool` 的上下文管理器中使用 `with` 语句,确保资源被正确释放。 ### 5. **MemoryError** 当任务消耗过多内存时,可能会导致 `MemoryError`,尤其是在处理大数据集或使用大量进程时。 **解决方案:** - 优化任务的内存使用,避免不必要的数据复制。 - 减少同时运行的进程数,使用 `Pool` 时指定合适的 `processes` 参数。 - 使用 `multiprocessing.sharedctypes` 或 `multiprocessing.Value` 等共享内存机制,减少内存占用。 ### 6. **WindowsError: [Error 6] The handle is invalid** 这个错误通常出现在 Windows 系统上,当尝试在非主模块中启动 `Pool` 时。 **解决方案:** - 确保 `Pool` 的创建和使用都在 `if __name__ == '__main__':` 保护的代码块中。 - 避免在交互式解释器或某些 IDE 中直接运行多进程代码,这些环境可能不支持 `multiprocessing` 的某些特性。 ### 7. **任务执行失败但未抛出异常** 有时任务可能在子进程中失败,但由于异常未被正确传递,主进程无法捕获到错误信息。 **解决方案:** - 使用 `apply_async()` 并调用 `get()` 方法来捕获任务中的异常。 - 在任务函数中使用 `try...except` 捕获异常,并返回错误信息以便主进程处理。 ```python from multiprocessing import Pool def faulty_function(x): try: return 1 / x except ZeroDivisionError as e: return str(e) if __name__ == '__main__': with Pool(2) as p: result = p.map(faulty_function, [0, 1, 2]) print(result) ``` ### 8. **性能问题** 尽管 `multiprocessing.Pool` 提供了并行处理的能力,但在某些情况下可能会导致性能下降,例如频繁的进程切换、过多的进程数等。 **解决方案:** - 根据 CPU 核心数量合理设置 `Pool` 的进程数,通常设置为 `os.cpu_count()` 返回的值。 - 使用 `map()` 和 `apply()` 代替 `map_async()` 和 `apply_async()`,减少异步调用的开销。 - 避免在 `Pool` 中频繁创建和销毁进程,尽量复用已有的 `Pool` 实例。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值