1. 程式人生 > >indeed一道線上筆試題

indeed一道線上筆試題

String Arrangement
Time limit時間制限 : 2sec / Stack limitスタック制限 : 256MB / Memory limitメモリ制限 : 256MB
Problem
  Mr. Takahashi is poor at English. He often mistakes spelling, especially when he spells the words in which same alphabetic character appear successively. For example, he is bad at words such as accept and hellorather than wrong or bye.
  Your task in this problem is to support Mr. Takahashi. You are given a string S that consists only of lower alphabets. Construct a string S’ that satisfies the following conditions.
  For each alphabet, its appearance frequencies in S and S’ are the same.
  Any two adjacent characters in S’ are different.
  If there are more than one string that satisfies these conditions, construct the lexicographically smallest one. If there is no string that satisfies them, report it.
  This problem adopts a special scoring method. If you find it too difficult to construct the lexicographically smallest string, you can earn partial scores by outputting any string that satisfies the two conditions above. Refer to “Note” for more details.
Input
Input is given in the following format;
S
In the first line, string S(1≦|S|≦200000) is given.
Output
Output should be written to the standard output.
Output S’ that satisfies the conditions explained in the description. If such S’ does not exist, output -1. Be sure to print a newline at the end of the output.
Note
There are three sets of inputs in this problem. You can get partial points by solving some of these datasets.
[Dataset 1] All the inputs in this dataset satisfy 1≦|S|≦1000. Moreover, for each input, outputting any string that satisfies the two conditions above is considered correct (regardless of lexicographical order). You will earn 20 points if you solved all the tests in this dataset.
[Dataset 2] All the inputs in this dataset satisfy 1≦|S|≦1000. Only outputting the lexicographically smallest string is considered correct in this dataset. You will earn 20 points if you solved all the tests in this dataset.
[Dataset 3] All the inputs in this dataset satisfy 1≦|S|≦200000. Only outputting the lexicographically smallest string is considered correct in this dataset. You will earn 60 points if you solved all the tests in this dataset.
Input Example1
hello
Output Example1
ehlol
Under the scoring criteria of dataset 1, lheol and olehl are also correct answers.
However, for the scoring criteria of datset 2 and 3, only ehlol is the correct answer.
Input Example2
aaax
Output Example2
-1
We cannot avoid a appearing successively.

題意很簡單:
給一個字串S(長度不超過20w),求S’,
1:S’滿足,對於每個字母在S’和S中出現次數相同
2:S’中,相鄰的兩個字母不能相同

如果有多個S,輸出其中字典序最小的

比如:hello,輸出:ehlol

如果沒有答案輸出:-1

我的思路是貪心策略,首先判斷一下是否滿足,這個只要一個單詞出現的次數大於總數 + 1的一半,則肯定不行
然後求字典序最小:
1:預處理,統計每個字母的個數
2:掃一遍,只要沒有某個剩餘的個數大於總數的一半,就拿和上一次不同的字典序最小的,否則拿超過個數一半的那個

複雜度O(26*N)
因為沒有OJ測評,自己測了幾個資料,也不知道最後這程式碼能不能AC

#include <iostream>
#include <string>
#include <algorithm>
int main()
{
#ifdef xxz
    freopen("in.txt","r",stdin);
#endif // xxz
    std::string str;

    while (std::cin >> str) {

        int len = str.length();
        int hash[27] = {0};

        int flag = 0;
        int first = 100
; for (int i = 0; i < str.length(); i++) { hash[str[i] - 'a'] += 1; if (str[i] - 'a' < first) { first = str[i] - 'a'; } if (hash[str[i] - 'a'] > (len + 1)/2 ) { flag = 1; } } if (flag){ std::cout<<"-1"<<std::endl; } else{ std::string ans = ""; int temp = first; int it = first; hash[first]--; ans.push_back(first + 'a'); int i,j; for (i = len - 1; i >= 1; i--){ flag = 0; it = 27; for (j = 0; j < 27; j++) { if (j == temp) continue; if (hash[j] != 0 && j < it) { it = j; } if (hash[j] > i/2) { flag = 1; break; } } if (flag) { temp = j; ans.push_back(temp + 'a'); hash[j]--; } else { temp = it; ans.push_back(temp + 'a'); hash[it]--; } } std::cout<<ans<<std::endl; } } return 0; }