Codeforces Round #313 (Div. 2) D. Equivalent Strings 字符串处理
来源:互联网 发布:时时宝典软件下载 编辑:程序博客网 时间:2024/05/19 14:18
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:
- They are equal.
- 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:
- a1 is equivalent to b1, and a2 is equivalent to b2
- 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!
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.
Print "YES" (without the quotes), if these two strings are equivalent, and "NO" (without the quotes) otherwise.
aabaabaa
YES
aabbabab
NO
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".
题意,给出两个字符串,确定,是否相等,定义字符串是否相等为,如果为奇数串,只能比较是否每个字符相同,如果为偶数串,第一个串分成两个相等长度的串为a1 b1,第二个串也分成a2 b2,a1== a2 && b1 == b2 || a1 == b2 && a2 == b2.
第一种方法,直接按照定义,偶数串的时候,两个都比较一下,得出答案,这样的复杂度很高,但测试数据不是很复杂,故也能过。由于字符串的特性,以及奇数串的时候,直接判定,几乎这种方法,是不可取的。时间几乎是第二种方法的20倍以上吧。
#define N 200500#define M 100005#define maxn 205#define MOD 1000000000000000007int n,len[2];char str[2][N];bool equalstr(char st1[],char st2[],int s1,int e1,int s2){ for(int i = s1;i<=e1;i++){ if(st1[i] != st2[s2++]) return false; } return true;}bool Check(char str1[],char str2[],int s1,int e1,int s2,int e2){ //printf("%d %d %d %d\n",s1,e1,s2,e2); int l = (e1 - s1 + 1); if(l & 1){ return equalstr(str1,str2,s1,e1,s2); } else { int mid = l / 2; if( Check(str1,str2,s1,s1 + mid - 1,s2,s2 + mid - 1) && Check(str1,str2,s1 + mid,e1,s2 + mid ,e2)) return true; if( Check(str1,str2,s1,s1 + mid - 1,s2 + mid ,e2) && Check(str1,str2,s1 + mid,e1,s2,s2 + mid - 1)) return true; } return false;}int main(){ while(SS(str[0])!=EOF) { SS(str[1]); len[0] = strlen(str[0]); len[1] = strlen(str[1]); if(Check(str[0],str[1],0,len[0] - 1,0,len[1] - 1)){ printf("YES\n"); } else printf("NO\n"); } return 0;}
第二种方法,直接把字符串内部排序。因为,偶数个字符串的,前部分与后部分,是哪个顺序,并不影响这个字符串的大小,所以我们要使一个字符串只有一种表达方法,也就是如果是偶串,则要求,前面的串比后半部分的串要小,这样就可以唯一化了,比第一种方法要快很多倍了。只是在处理字符串上花费时间,复杂度是线性的。
void Change(char str[],int s,int e){ int l = e - s + 1,s1,s2; if(l & 1) return ; l = l>>1;s1 = s;s2 = s + l; Change(str,s,s + l - 1); Change(str,s + l,e); for(int i = 0;i < l;i++,s1++,s2++){ if(str[s1] > str[s2]) { s1 = s;s2 = s + l; for(int j = 0;j< l;j++,s1++,s2++){ swap(str[s1],str[s2]); } break; } else if(str[s1] < str[s2]) break ; }}int main(){ while(SS(str[0])!=EOF) { SS(str[1]); len[0] = strlen(str[0]);Change(str[0],0,len[0] - 1);//cout<<str[0]<<endl; len[1] = strlen(str[1]);Change(str[1],0,len[1] - 1);//cout<<str[1]<<endl; if(!strcmp(str[0],str[1])){ printf("YES\n"); } else printf("NO\n"); } return 0;}
- Codeforces Round #313 (Div. 2) D. Equivalent Strings 字符串处理
- Codeforces Round #313 (Div. 2)D. Equivalent Strings(字符串)
- Codeforces Round #313 (Div. 2) D Equivalent Strings
- Codeforces Round #313 (Div. 2) Problem D - Equivalent Strings
- Equivalent Strings(Codeforces Round #313 (Div. 2) D)
- Codeforces Round #313 (Div. 2) 560D Equivalent Strings(dos)
- Codeforces Round #313 (Div. 2)560D. Equivalent Strings
- Codeforces Round #313 (Div. 2) D Equivalent Strings(dfs)
- Codeforces Round #313 (Div. 2) D. Equivalent Strings(简单搜索)
- Round #313 (Div. 2) D. Equivalent Strings-大水题递归
- Codeforces Round #313 (Div. 2) Equivalent Strings(搜索)
- Codeforces Round #313 (Div. 1) B. Equivalent Strings DFS暴力
- Codeforces Round #313 (Div. 1) B. Equivalent Strings
- codeforces Round 313 (Div. 1) B.Equivalent Strings
- Codeforces Round #313 D. Equivalent Strings(DFS)
- CodeForces#313 D. Equivalent Strings
- Codeforces Round #313 Equivalent Strings(递归)
- 规律+DFS Codeforces Round div2 D. Equivalent Strings
- leetcode之路001 Two Sum
- 博客搭建在github上,国内访问不稳定,在csdn备份一份
- 如何远程管理没有任何配置的新交换机?
- JS获取页面高度方法小结
- 取出内容中插入图片的地址
- Codeforces Round #313 (Div. 2) D. Equivalent Strings 字符串处理
- <s:action>标签
- 捕获错误信息
- ecos
- sqlplus执行脚本文件时如何传参数
- ART学习笔记Thread SuspendAll部分
- [AD]在组策略中将客户端的远程桌面都打开
- Linux大文件已删除,但df查看已使用的空间并未减少解决
- RebornDB:下一代分布式Key-Value数据库