阿里c++开发-字典序问题
#include<iostream> #include<string> #include<cstring> //字典序问题,输入两个字符串,计算两个字符串之间有多少字符串 //a<b //aaa aaa 0 //aaa b 1 //bbb aaa -1 //cout 0 1 -1 using namespace std; int order(int i, int k) { int j; int sum = 0; if (k == 1) { return 1; } else { for (j = i + 1; j <= 26; j++) { sum += order(j, k - 1); } } return sum; } int f(int k) { int sum = 0; for (int i = 1; i <= 26; i++) { sum += order(i, k); } return sum; } int func(string word) { int len = word.length(); int word_num[6]; int num = 0; for (int i = 0; i < len; i++) { word_num[i] = word[i] - 'a' + 1; } for (int i = len - 1; i>0; i--) { num += f(i); } for (int i = word_num[0] - 1; i >= 1; i--) { num += order(i, len); } for (int i = 1; i <= len; i++) { for (int j = word_num[i] - 1; j > word_num[i - 1]; j--) { num += order(j, len - 1); } } num++; return num; } int main() { int T; cin >> T; if (T<0 || T>100000) { return 0; } string A, B; int temp = 0; int arr[100000] = { 0 }; int k = 0; for (int i = 0; i < T; i++) { cin >> A >> B; if (A == B) { arr[k] = 0; } if (A>B) { arr[k] = -1; } else { arr[k]=temp = func(B) - func(A); } k++; } for (int j = 0; j < T; j++) { cout << arr[j]; if (j < T - 1) { cout << " "; } } return 0; }
#笔试题目##阿里巴巴#