每日编程-20170401

题目:XXXX年突然有外星人造访,但大家语言不通,不过科学家们经过研究发现外星人用26个英文字母组成的单词中最长不降子序列的长度来表述数字,且英文字母的排列顺序不同,现给出其排列顺序,再给出外星人说的每个数字(其实是每个英文单词,用空格隔开),翻译出外星人所说的数字(连续输出,最后加回车)。 (因为是最长不降子序列,所以数字中没有0,也就是说外星人的数字是> =1的数字)

例如

我们正常的字母排列顺序是abcdefg…….xyz,代表a< b< c< …..< x< y< z abcd efg hhh ihg四个字符串的最长不降子序列的长度分别为4 3 3 1

输入格式:

第1,2行为字符串 含义如题描述

输出格式:

输出答案 含义如题描述

1< =第二行长度< =255

样例输入

abcdefghijklmnopqrstuvwxyz
abcd efg hhh ihg
样例输出

4331

解答:

对于输入的字幕顺序,保存为字典,第一个字母对应0,最后一个对应25

将第二行的每个单词都用字典翻译一遍

接下来就是比数字大小

如果数组递增,tmp递增长度+1,直到递增停止,比最大递增长度与tmp递增长度

 1 #include <iostream>
 2 #include <sstream>
 3 #include <map>
 4 #include <vector>
 5 using namespace std;
 6 int Max(int a, int b) { return a < b ? b : a; }
 7 int LongNotDown(vector<int> v) {
 8     int maxLong = 1,tmpNum = v[0], tmpLong = 1;
 9     for (auto i = 1; i < v.size(); i++)
10     {
11         if (v[i] >= tmpNum)
12         {
13             tmpNum = v[i];
14             tmpLong++;
15         }
16         else
17         {
18             tmpNum = v[i];
19             maxLong = Max(maxLong, tmpLong);
20             tmpLong = 1;
21         }
22     }
23     maxLong = Max(maxLong, tmpLong);
24     return maxLong;
25 }
26 void getAlienNum(vector<vector<int>> v) {
27     for (auto i = 0; i < v.size(); i++)
28     {
29         cout << LongNotDown(v[i]);
30     }
31     cout << endl;
32 }
33 int main() {
34     string s;
35     while (cin >> s)
36     {    
37         map<int, char> dic;
38         for (auto i = 0; i < s.size(); i++)
39         {
40             dic[s[i]] = i;
41         }
42         string line,word;
43         cin.ignore();
44         getline(cin, line);
45         istringstream words(line);
46         vector<vector<int>> words2Num;
47         vector<int> wordNum;
48         while (words >> word)
49         {
50             for (auto i = 0; i < word.size(); i++)
51             {
52                 wordNum.push_back(dic[word[i]]);
53             }
54             words2Num.push_back(wordNum);
55             wordNum.clear();
56         }
57         getAlienNum(words2Num);
58     }
59 }
原文地址:https://www.cnblogs.com/linhaowei0389/p/6657372.html