CF 559B(Equivalent Strings-构造法)

本文介绍了一种新的字符串等价性定义及判断方法,通过递归将字符串分割为两半并比较其等价性,最终返回一个标准化的字符串用于判断两个输入字符串是否等价。

B. Equivalent Strings
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a and b of equal length are calledequivalent in one of the two cases:

  1. They are equal.
  2. If we split string a into two halves of the same size a1 and a2, and string b into two halves of the same size b1 and b2, then one of the following is correct:
    1. a1 is equivalent to b1, and a2 is equivalent to b2
    2. a1 is equivalent to b2, and a2 is equivalent to b1

As a home task, the teacher gave two strings to his students and asked to determine if they are equivalent.

Gerald has already completed this home task. Now it's your turn!

Input

The first two lines of the input contain two strings given by the teacher. Each of them has the length from 1 to 200 000 and consists of lowercase English letters. The strings have the same length.

Output

Print "YES" (without the quotes), if these two strings are equivalent, and "NO" (without the quotes) otherwise.

Sample test(s)
input
aaba
abaa
output
YES
input
aabb
abab
output
NO
Note

In the first sample you should split the first string into strings "aa" and "ba", the second one — into strings "ab" and "aa". "aa" is equivalent to "aa"; "ab" is equivalent to "ba" as "ab" = "a" + "b", "ba" = "b" + "a".

In the second sample the first string can be splitted into strings "aa" and "bb", that are equivalent only to themselves. That's why string "aabb" is equivalent only to itself and to string "bbaa".


题目看错了666

能分就分,且把字典序小的排前面



#include<bits/stdc++.h>
using namespace std;

string equ(string s)
{
	int n=s.length();
	if (n&1) return s;
	string s1= equ(s.substr(0,n/2));
	string s2= equ(s.substr(n/2,n/2));
	if (s1<s2) return s1+s2;
	return s2+s1;
}
string s1,s2;
int main()
{
	cin>>s1>>s2;
	if (equ(s1).compare(equ(s2))==0)
	{
		cout<<"YES"<<endl;
	}
	else cout<<"NO"<<endl;
	return 0;
}





### Definition and Implementation of Equivalent Strings in Programming In programming, **equivalent strings** refer to two or more string values that are considered equal under specific conditions. These conditions may involve case sensitivity, encoding formats, whitespace handling, or other transformations such as normalization[^1]. Below is a detailed explanation of how equivalent strings can be defined and implemented. #### Case Sensitivity Strings might be treated as equivalent regardless of their letter casing. In many languages, this comparison involves converting both strings into either uppercase or lowercase before comparing them. ```python def are_strings_equivalent_case_insensitive(str1, str2): return str1.lower() == str2.lower() ``` This approach ensures that differences like 'A' vs 'a' do not affect equivalence checks[^3]. #### Encoding Formats When dealing with different encodings (e.g., UTF-8, ASCII), ensuring proper decoding prior to comparison becomes essential. Incorrectly handled byte sequences could lead to mismatches even when characters appear identical visually. ```cpp std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter; std::string utf8_str = converter.to_bytes(wide_char_string); // Now compare `utf8_str` against another properly encoded string value. ``` Such conversions allow accurate comparisons across various character sets[^4]. #### Normalization Forms Unicode provides multiple ways to represent certain symbols due to combining marks or precomposed forms. To ensure true equivalency between seemingly alike but differently represented texts requires applying standard normalizations first. ```java import java.text.Normalizer; public boolean checkNormalizedEquivalence(String s1, String s2){ return Normalizer.normalize(s1, Form.NFKC).equals(Normalizer.normalize(s2, Form.NFKC)); } ``` By utilizing NFC/NFD/NFKC/NFKD standards provided by libraries within respective environments helps achieve consistent results during evaluations involving complex scripts[^2]. #### Whitespace Handling Ignoring leading/trailing spaces along trimming internal redundant ones also contributes towards defining equality among textual data elements. ```javascript function trimAndCompare(a,b){ const trimmedA=a.replace(/\s+/g,' ').trim(); const trimmedB=b.replace(/\s+/g,' ').trim(); return trimmedA===trimmedB; } ``` Here regex operations assist cleaning up unnecessary gaps which otherwise would prevent correct identification despite meaningful content being same. --- §§ 1. How does Unicode normalization impact performance while checking for equivalent strings? 2. What techniques exist beyond simple case folding to handle locale-specific variations in string matching algorithms? 3. Can you provide examples where ignoring diacritical marks leads to incorrect conclusions about string similarity? 4. Are there any built-in functions available directly inside popular databases supporting normalized text searches out-of-the-box without requiring additional coding efforts from developers side ? 5. Discuss potential pitfalls associated with multithreaded applications performing simultaneous modifications over shared mutable string objects used later on for determining equivalences .
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值