HackerRank String
2015-06-30 14:14
417 查看
Alternating Characters
思路:还是遍历,就看有几个连续且重复的就行
难度:Easy
Anagram
思路:计算字符串前半段和后半段的同一种字母数量差距的和的一半就行了
难度:Easy
类型:String Basic 字符串基础 Anagram 字谜
Bigger is Greater
思路:一个全排列的问题,用字典序列法解决
难度:Moderate
类型:Sorting 排序 Next Permutation 下个序列
Funny
思路:字符串内部的部分子串的比较
难度:Easy
类型:略
Game of Thrones - I
思路:回文的字符串的长度若是偶数,则每个字符必须出现偶数次,而奇数长度的至多只有一个字符出现的次数为奇数次
难度:Easy
类型:Palindrome回文 Dictionary 词典 Anagram 字谜
Gemstones
思路:基本就是开数组记录字母出现的次数就行
难度:Easy
类型:String Basics 字符串基础 Alphabets 字母表 Dictionary 字典
Make it Anagram
思路:开两个数组记录所有字母的出现次数,最后减去重复的字母
难度:Easy
类型:String Basic
Palindrome Index
思路:遍历回文字符串
难度:Easy
类型:Palindrome Index 回文
pangram
思路:遍历字符串
难度:Easy
类型:略
Sherlock and Anagrams
思路:查找字符串中连续且字母相同的子串
难度:Moderate
类型:Anagram字谜
Two Strings
思路:照题目意思,貌似有相同的字母就行了。。。
难度:Easy
类型:String Basic 字符串基础
思路:还是遍历,就看有几个连续且重复的就行
难度:Easy
#include<iostream> #include<string> using namespace std; int detele[10]; void to_detele(string test,int i) { for (int j = 0; j < test.length() - 1; j++) { if (test[j] == test[j + 1]) detele[i]++; } } int main() { int T; string test; cin >> T; for (int i = 0; i < T; i++){ cin >> test; to_detele(test,i); } for (int i = 0; i < T; i++) cout << detele[i] << endl; return 0; }
Anagram
思路:计算字符串前半段和后半段的同一种字母数量差距的和的一半就行了
难度:Easy
类型:String Basic 字符串基础 Anagram 字谜
#include<iostream> #include<string> #include<cmath> using namespace std; int result[100]; void Anagram(string input,int time) { int temp1[26] = {}, temp2[26] = {}; if (input.length() % 2 == 1) { result[time] = -1; return; } else { int half = input.length() / 2; for (int i = 0; i < half; i++) temp1[(int)(input[i] - 97)]++; for (int i = half; i < input.length(); i++) temp2[(int)(input[i] - 97)]++; } int total = 0; for (int i = 0; i < 26;i++) total+=abs(temp1[i] - temp2[i]); result[time] = total/2; } int main() { int N; string input; cin >> N; for (int i = 0; i < N; i++) { cin >> input; Anagram(input, i); } for (int i = 0; i < N; i++) cout << result[i] << endl; return 0; }
Bigger is Greater
思路:一个全排列的问题,用字典序列法解决
难度:Moderate
类型:Sorting 排序 Next Permutation 下个序列
#include<iostream> #include<string> #include<fstream> using namespace std; string result[100000]; void Permutation(string input, int time) { int length = input.length(); int last_one = length - 1; int lower = -1; for (int i = length - 1; i > 0; i--) { if (input[i - 1] < input[i]) { lower = i - 1; break; } } if (lower == -1) { result[time] = "no answer"; return; } char Max_Min = 'z' + 1; int index_Max_Min; for (int i = lower + 1; i < length; i++) { if (input[lower]<input[i] && input[i] <= Max_Min ) { Max_Min = input[i]; index_Max_Min = i; } } char temp = input[index_Max_Min]; input[index_Max_Min] = input[lower]; input[lower] = temp; result[time] = input; int j = 1; int middle = (length + lower + 1) / 2; for (int i = lower + 1; i < middle; i++) { char temp = input[i]; input[i] = input[length-j]; input[length - j] = temp; j++; } result[time]=input; } void save(string result[],int N) { ofstream file("My_result.doc"); if (file.is_open()) { for (int i = 0; i < N;i++) file << result[i] << endl; } file.close(); } int main() { int N; cin >> N; string input; for (int i = 0; i < N; i++) { cin >> input; Permutation(input,i); } for (int i = 0; i < N; i++) { cout << result[i] << endl; } return 0; }
Funny
思路:字符串内部的部分子串的比较
难度:Easy
类型:略
#include<iostream> #include<string> #include<cstdlib> using namespace std; string Input[10]; int result[10] = {}; void funny(int i) { string temp = Input[i]; int length = Input[i].length(); for (int j = 0; j < length-1; j++) { int s = abs(temp[j+1] - temp[j]); int r = abs(temp[length - j - 1] - temp[length - j-2]); if (s != r) return; } result[i] = 1; } int main() { int i, j, time; cin >> time; for (i = 0; i < time; i++){ cin >> Input[i]; funny(i); } for (i = 0; i < time; i++) if (result[i]) cout << "Funny" << endl; else cout << "Not Funny" << endl; return 0; }
Game of Thrones - I
思路:回文的字符串的长度若是偶数,则每个字符必须出现偶数次,而奇数长度的至多只有一个字符出现的次数为奇数次
难度:Easy
类型:Palindrome回文 Dictionary 词典 Anagram 字谜
#include<iostream> #include<string> using namespace std; int letter[26]; void Palidrome(string input) { for (int i = 0; i < input.length(); i++) { letter[(int)(input[i] - 97)]++; } if (input.length() % 2 == 0) { for (int i = 0; i < 26;i++) if (letter[i] % 2 != 0) { cout << "NO" << endl; return; } } else if (input.length() % 2 == 1) { int one = 0; for (int i = 0; i < 26; i++) { if (letter[i] %2== 1) { one++; if (one>1) { cout << "NO" << endl; return; } } } } cout << "YES" << endl; } int main() { string input; cin >> input; Palidrome(input); return 0; }
Gemstones
思路:基本就是开数组记录字母出现的次数就行
难度:Easy
类型:String Basics 字符串基础 Alphabets 字母表 Dictionary 字典
#include<iostream> #include<string> using namespace std; int letter[26]; void Gemstones(string input) { int temp_letter[26]; for (int i = 0; i < 26; i++) temp_letter[i] = 0; for (int i = 0; i < input.length(); i++) { if (temp_letter[(int)(input[i] - 97)] == 0) { temp_letter[(int)(input[i] - 97)]++; letter[(int)(input[i] - 97)]++; } } } int main() { int N,total = 0;; string input; cin >> N; for (int i = 0; i < N; i++) { cin >> input; Gemstones(input); } for (int i = 0; i < N; i++){ if (letter[i] >= N) total++; } cout << total << endl; return 0; }
Make it Anagram
思路:开两个数组记录所有字母的出现次数,最后减去重复的字母
难度:Easy
类型:String Basic
#include<iostream> #include<string> #include<cmath> using namespace std; int main() { int letter_a[26]; int letter_b[26]; string input_a; string input_b; int total = 0; for (int i = 0; i < 26; i++) { letter_a[i] = 0; letter_b[i] = 0; } cin >> input_a; cin >> input_b; int length_a = input_a.length(); int length_b = input_b.length(); int smaller; if (length_a <= length_b) smaller = length_a; else smaller = length_b; for (int i = 0; i <length_a; i++) { letter_a[(int)(input_a[i] - 97)]++; } for (int i = 0; i <length_b; i++) letter_b[(int)(input_b[i] - 97)]++; for (int i = 0; i < 26; i++) total += abs(letter_a[i] - letter_b[i]); cout << total << endl; return 0; }
Palindrome Index
思路:遍历回文字符串
难度:Easy
类型:Palindrome Index 回文
#include<iostream> #include<string> using namespace std; int result[20] = { -1 }; void Palindrome(string Input,int time) { int Index_one,Index_two; int Length = Input.length(); for (int i = 0; i < Length; i++) { if (i == Length / 2+1) break; if (Input[i] != Input[Length - 1 - i]) { Index_one = i; Index_two = Length - 1 - i; if (Input[Index_one + 1] == Input[Index_two]) { if (Input[Index_one + 2] == Input[Index_two - 1]) { result[time] = Index_one; break; } else { result[time] = Index_two; break; } } else { result[time] = Index_two; break; } } } } void Initliaze() { for (int i = 0; i < 20;i++) result[i] = -1; } int main() { int N; cin >> N; string Input; Initliaze(); for (int i = 0; i < N; i++) { cin >> Input; Palindrome(Input,i); } for (int i = 0; i < N; i++) cout << result[i] << endl; return 0; }
pangram
思路:遍历字符串
难度:Easy
类型:略
#include<iostream> #include<cstring> using namespace std; int letter[26]; char yes[15] = "pangram"; char no[15] = "not pangram"; int main() { int i,j,temp; char a[1000],b; for (i = 0; i < 1000; i++) { scanf("%c", &b); if (b != '\n') a[i] = b; else break; } for (j = 0; j <i; j++) { if (a[j] == ' ') continue; if (a[j] >= 'a' && a[j] <= 'z') { temp = a[j] - 97; letter[temp] = 1; } else { temp = a[j] - 65; letter[temp] = 1; } } j = 0; while (j <= 25) { if (letter[j] == 0) { puts(no); return 0; } else j++; } puts(yes); return 0; }
Sherlock and Anagrams
思路:查找字符串中连续且字母相同的子串
难度:Moderate
类型:Anagram字谜
#include<iostream> #include<string> #include<algorithm> using namespace std; int result[10]; int compare(string one,string two) { if (one == two) return 1; sort(one.begin(),one.end()); sort(two.begin(), two.end()); if (one == two) return 1; else return 0; } int find_sub_unorder_pair(string input, int sub_length) { int length = input.length(); int total_times =length - sub_length; int total_numbers = 0; for (int i = 0; i <=total_times; i++) for (int j = i + 1; j <= total_times; j++) { if (compare(input.substr(i, sub_length ), input.substr(j, sub_length )) == 1) { total_numbers++; } } return total_numbers; } void unorder(string input,int time) { int sub_length; int length = input.length(); for (sub_length = 1; sub_length < length ; sub_length++) { result[time] += find_sub_unorder_pair(input,sub_length); } } int main() { int T, i, j; string input; cin >> T; for (i = 0; i < T; i++) { cin >> input; unorder(input, i); } for (i = 0; i < T; i++) cout << result[i] << endl; return 0; }
Two Strings
思路:照题目意思,貌似有相同的字母就行了。。。
难度:Easy
类型:String Basic 字符串基础
#include<iostream> #include<string> using namespace std; int result[10]; void fun(string a,string b,int time) { int letter_a[26] = {}; int letter_b[26] = {}; for (int i = 0; i < a.length(); i++) letter_a[(int)(a[i]-97)]++; for (int j = 0; j < b.length(); j++) letter_b[(int)(b[j] - 97)]++; for (int k = 0; k < 26;k++) if (letter_a[k] && letter_b[k]) { result[time] = 1; return; } } int main() { int N; cin >> N; for (int i = 0; i < N; i++) { string a, b; cin >> a >> b; fun(a, b, i); } for (int i = 0; i < N; i++) if (result[i]) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
相关文章推荐
- 如何让虚拟目录里面的webconfig不继承网站的设置
- java动态爬虫jsoup以及正则表达式的运用
- Container With Most Water——LeetCode
- android_launcher的源码详细分析
- Android简化一个界面内的多个Listener
- 地形混合纹理生成
- Android 自己实现 NavigationView [Design Support Library(1)]
- 网络通信:URL 与 URLConnection
- Sql Server问题集锦
- org.tinygroup.flow-流程引擎
- Android 自己实现 NavigationView [Design Support Library(1)]
- oracle 导出数据到文件
- tr1::shared_ptr 代码范例
- 利用Apache POI读写Microsoft Excel文件
- CMD获取当前目录的绝对路径
- org.tinygroup.flowprocessor-流程加载器
- 基于Java实现批量下载网络图片
- 第七单元 7.10
- Linux下安装nginx
- Linux服务器管理: 系统的进程管理终止进程kill命令