389. Find the Difference的C++解法

本文介绍了一种利用XOR操作高效找出两个字符串中不同字符的方法,并提供了两种解决方案的代码实现。一种是逐个匹配并删除相同字符的方式,另一种则是通过XOR操作简化流程。

要考虑到统计重复字母的个数,比如s里有3个a,t里面有4个a,所以多出来的是a。我的解决办法就是找到一个删除一个,要使用迭代器。

class Solution {
public:
	char findTheDifference(string s, string t) {
		int i;
		for (i = 0; i < t.length(); i++)
		{
			int flag = 1;
			string::iterator it;
			for (it = s.begin(); it != s.end(); ++it)
			{
				if (*it == t[i])
				{
					s.erase(it);
					flag = 0;
					break;
				}
			}
			if (flag==1) return t[i];

		}
	}
};

看了下最优解发现又忽略了XOR的作用!!!这种只多出一个来的用XOR解决起来超级简单啊!!我是真的傻!!!

最优解算法:

class Solution {
public:
    char findTheDifference(string s, string t) {
        char r=0;
        for(char c:s) r ^=c;
        for(char c:t) r ^=c;
        return r;
    }
};


12000 K-Transformed Permutations Consider a sequence of n integers < 1 2 3 4 ... n >. Since all the values are distinct, we know that there are n factorial permutations. A permutation is called K-transformed if the absolute difference between the original position and the new position of every element is at most K. Given n and K, you have to find out the total number of K-transformed permutations. Example: n = 4, K =2 1 2 3 4 Valid Annotation (position) P1 1234 Yes Theoriginalsequence. All the elements are in their original position P2 1243 Yes 3and4arereordered, but each is shifted by 1 position only. P3 1324 Yes P4 1342 Yes 2isshiftedby2positions. 2≤K, so it’s a valid one. P5 1423 Yes P6 1432 Yes P7 2134 Yes P8 2143 Yes P9 2314 Yes P10 2 3 4 1 No 1isshiftedby3positions. 3>K and so this is an invalid permutation P11 2 4 1 3 Yes P12 2 4 3 1 No P13 3 1 2 4 Yes P14 3 1 4 2 Yes P15 3 2 1 4 Yes P16 3 2 4 1 No P17 3 4 1 2 Yes P18 3 4 2 1 No P19 4 1 2 3 No 4isshiftedby3positions. 3>K and so this is also invalid P20 4 1 3 2 No P21 4 2 1 3 No P22 4 2 3 1 No P23 4 3 1 2 No P24 4 3 2 1 No Here,both4and1are breaking the property. So, for the above case, there are 14 2-transformed permutations. Input The first line of input is an integer T (T < 20) that indicates the number of test cases. Each case consists of a line containing two integers n and K. (1 ≤ n ≤ 109) and (0 ≤ K ≤ 3). Output For each case, output the case number first followed by the required result. Since the result could be huge, output result modulo 73405. UniversidaddeValladolidOJ:12000–K-TransformedPermutations 2/2 SampleInput 3 4 2 100 0 101 SampleOutput Case1:14 Case2:1 Case3:89 UVA12000 可以給我C++解法分析嗎
08-01
Problem Statement At the entrance of AtCoder Inc., there is a special pass-code input device. The device consists of a screen displaying one string, and two buttons. Let t be the string shown on the screen. Initially, t is the empty string. Pressing a button changes t as follows: Pressing button A appends 0 to the end of t. Pressing button B replaces every digit in t with the next digit: for digits 0 through 8 the next digit is the one whose value is larger by 1; the next digit after 9 is 0. For example, if t is 1984 and button A is pressed, t becomes 19840; if button B is then pressed, t becomes 20951. You are given a string S. Starting from the empty string, press the buttons zero or more times until t coincides with S. Find the minimum number of button presses required. 关于这个题有这串代码 #include <bits/stdc++.h> using namespace std; int main() { string S; cin >> S; int N = S.size(); long long ans = 0; // Step 1: Generate the first character of S. // Press A once to add '0', then press B (S[0] - '0') times. ans += (S[0] - '0'); // Presses for button B to get the first digit. // Step 2: Generate the rest of the characters. for (int i = 1; i < N; ++i) { // After pressing A, the last digit becomes (previous_digit + 1) % 10. int current_digit = (S[i - 1] - '0' + 1) % 10; int target_digit = S[i] - '0'; // Calculate the difference between current_digit and target_digit. int diff = (target_digit - current_digit + 10) % 10; // Press A once to add a '0'. ans += 1; // Press B 'diff' times to adjust the last digit. ans += diff; } cout << ans << endl; return 0; } 样例输入21,样例输出4呀,运行结果错了
05-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值