PAT(A) 101-116-2-2014-09-06

來源:互聯網
上載者:User
1084. Broken Keyboard (20) 時間限制 200 ms
記憶體限制 32000 kB
代碼長度限制 16000 B
判題程式 Standard 作者 CHEN, Yue

On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters corresponding to those keys will not appear on screen.

Now given a string that you are supposed to type, and the string that you actually type out, please list those keys which are for sure worn out.

Input Specification:

Each input file contains one test case. For each case, the 1st line contains the original string, and the 2nd line contains the typed-out string. Each string contains no more than 80 characters which are either English letters [A-Z] (case insensitive), digital numbers [0-9], or "_" (representing the space). It is guaranteed that both strings are non-empty.

Output Specification:

For each test case, print in one line the keys that are worn out, in the order of being detected. The English letters must be capitalized. Each worn out key must be printed once only. It is guaranteed that there is at least one worn out key. Sample Input:

7_This_is_a_test_hs_s_a_es
Sample Output:
7TI
簡單的字串處理問題:
 
#include <iostream>#include <cstdio>#include <string>using namespace std;int main(){string s1, s2, s = "";cin >> s1 >> s2;for (int i = 0; i < s1.size(); i++)s1[i] = toupper(s1[i]);for (int i = 0; i < s2.size(); i++)s2[i] = toupper(s2[i]);while (s1.size()){if (s2.size() != 0 && s1[0] != s2[0]){if (s.find(s1[0]) == string::npos)s += s1[0];s1.erase(s1.begin());}else if(s2.size() != 0 && s1[0] == s2[0]){s1.erase(s1.begin());s2.erase(s2.begin());}else if (s2.size() == 0){if (s.find(s1[0]) == string::npos)s += s1[0];s1.erase(s1.begin());}}cout << s << endl;return 0;}
 
 
#include <iostream>#include <string>using namespace std;int main(){string s1, s2, ans = "";cin >> s1 >> s2;for (int i = 0; i < s1.size(); ++i)s1[i] = toupper(s1[i]);for (int i = 0; i < s2.size(); ++i)s2[i] = toupper(s2[i]);for (int i = 0; i < s2.size(); ++i){if (s2[i] != s1[i]){ans += s1[i];char x = s1[i];for (int j = i; j < s1.size(); ++j){if (s1[j] == x){s1.erase(j, 1);--j;}}--i;}}if (s1.size() > s2.size()){for (int i = s2.size(); i < s1.size(); ++i){if (ans.find(s1[i]) == string::npos)ans += s1[i];}}cout << ans << endl;return 0;}



1085. Perfect Sequence (25) 時間限制 300 ms
記憶體限制 32000 kB
代碼長度限制 16000 B
判題程式 Standard 作者 CAO, Peng

Given a sequence of positive integers and another positive integer p. The sequence is said to be a "perfect sequence" if M <= m * p where M and m are the maximum and minimum numbers in the sequence, respectively.

Now given a sequence and a parameter p, you are supposed to find from the sequence as many numbers as possible to form a perfect subsequence.

Input Specification:

Each input file contains one test case. For each case, the first line contains two positive integers N and p, where N (<= 105) is the number of integers in the sequence, and p (<= 109) is the parameter. In the second line there are N positive integers, each is no greater than 109.

Output Specification:

For each test case, print in one line the maximum number of integers that can be chosen to form a perfect subsequence. Sample Input:

10 82 3 20 4 5 1 6 7 8 9
Sample Output:
8
#include <iostream>#include <cstdio>#include <algorithm>using namespace std;int a[100000];int main(){int n;long long p;scanf("%d%lld", &n, &p);for (int i = 0; i < n; i++)scanf("%d", &a[i]);sort(a, a + n);int max = 1;for (int i = 0; i < n-max; i++){for (int j = i+max; j < n; j++){if (a[i] * p < a[j])break;if (j - i + 1 > max)max = j - i + 1;}}printf("%d\n", max);return 0;}

另外貼一個我看到的又看不懂的解法:
 
#include <bits/stdc++.h>using namespace std;int a[100010];int main(){int n, p;scanf("%d%d", &n, &p);for (int i = 0; i < n; i++) scanf("%d", a + i);sort(a, a + n);int ans = 0;for (int i = 0; i < n; i++) {int delta = lower_bound(a, a + n, (int)ceil(1.0 * a[i] / p)) - a;ans = max(ans, i - delta + 1);}printf("%d\n", ans);return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.