x

  • Tạo bởi Tạo bởi LQDuy
  • Start date Start date
public class Solution
{
public Dictionary<char, List<char>> lookups = new Dictionary<char, List<char>>();
public int module = (int)Math.Pow(10, 9) + 7;
public int CountVowelPermutation(int n)
{
lookups.Add(' ', new List<char> { 'a', 'e', 'i', 'o', 'u' });
lookups.Add('a', new List<char> { 'e' });
lookups.Add('e', new List<char> { 'a', 'i' });
lookups.Add('i', new List<char> { 'a', 'e', 'o', 'u' });
lookups.Add('o', new List<char> { 'i', 'u' });
lookups.Add('u', new List<char> { 'a' });
var memo = new Dictionary<(char, int), int>();
return Dp(' ', 0, n, memo);
}

public int Dp(char root, int index, int n, Dictionary<(char, int), int> memo)
{
if (index == n)
return 1;

int ans = 0;
if(memo.ContainsKey((root, index)))
return memo[(root, index)];
foreach (var item in lookups[root])
{
var result = Dp(item, index + 1, n, memo)%module;
ans = (ans + result)%module;
}

memo[(root, index)] = (int)ans;
return memo[(root, index)];
}
}
 
public class Solution
{
public Dictionary<char, List<char>> lookups = new Dictionary<char, List<char>>();
public int module = (int)Math.Pow(10, 9) + 7;
public int CountVowelPermutation(int n)
{
lookups.Add(' ', new List<char> { 'a', 'e', 'i', 'o', 'u' });
lookups.Add('a', new List<char> { 'e' });
lookups.Add('e', new List<char> { 'a', 'i' });
lookups.Add('i', new List<char> { 'a', 'e', 'o', 'u' });
lookups.Add('o', new List<char> { 'i', 'u' });
lookups.Add('u', new List<char> { 'a' });
var memo = new Dictionary<(char, int), int>();
return Dp(' ', 0, n, memo);
}

public int Dp(char root, int index, int n, Dictionary<(char, int), int> memo)
{
if (index == n)
return 1;

int ans = 0;
if(memo.ContainsKey((root, index)))
return memo[(root, index)];
foreach (var item in lookups[root])
{
var result = Dp(item, index + 1, n, memo)%module;
ans = (ans + result)%module;
}

memo[(root, index)] = (int)ans;
return memo[(root, index)];
}
}
Ko ngờ ngài @Thích Vét Máng cũng biết làm Quy hoạch động :vozvn (20):
 
Sửa lần cuối:
Thuật toán nó có AI hỗ trợ hết rồi mà anh. Giờ a thử học lại và làm cùng anh em cho vui.

@Anhhaitietkiem @SK Telecom @lucho
Quan trọng phải hiểu, nhớ với vọc vạch được mà vẫn chạy được ấy. Chứ ai chả biết AI nó gen phát ra code luôn, nhưng giải thích với ráp vào case cụ thể thì ko có đâu, giải mấy bài toàn cơ bản or mấy cái chương trình GUI của bọn sinh viên thì được
 
Quan trọng phải hiểu, nhớ với vọc vạch được mà vẫn chạy được ấy. Chứ ai chả biết AI nó gen phát ra code luôn, nhưng giải thích với ráp vào case cụ thể thì ko có đâu, giải mấy bài toàn cơ bản or mấy cái chương trình GUI của bọn sinh viên thì được
Có cao X này đây. 🤓👇

@Xoanquay SA cho xin bí kiếp thành thần code và SA -DataAchitech với ạ.
 
C++:
#include <string>
#include <vector>
using namespace std;

class Solution {
public:
    vector<string> splitMessage(string message, int limit) {
        int messageLength = message.size();
        int sumOfDigits = 0;
        vector<string> splitMessages;
    
        for (int partCount = 1; partCount <= messageLength; ++partCount) {
            int lengthOfDigits = to_string(partCount).size();
            sumOfDigits += lengthOfDigits;
            int totalDigitsLength = lengthOfDigits * partCount;
            int totalSeparatorsLength = 3 * partCount;
        
            if (partCount * limit - (sumOfDigits + totalDigitsLength + totalSeparatorsLength) >= messageLength) {
                int currentIndex = 0;
                for (int partIndex = 1; partIndex <= partCount; ++partIndex) {
                    string tail = "<" + to_string(partIndex) + "/" + to_string(partCount) + ">”;
                    string part = message.substr(currentIndex, limit - tail.size()) + tail;
                    splitMessages.emplace_back(part);
                
                    currentIndex += limit - tail.size();
                }
                break;
            }
        }
        return splitMessages;
    }
};

T passed r nhé!
 
Sửa lần cuối:
Java:
class Solution {
    public String[] splitMessage(String message, int limit) {
        int size = message.length();
        int lenOfIndice = 1, total = 1;
        while (size + (3 + len(total)) * total + lenOfIndice > limit * total) {
            if (3 + len(total) * 2 >= limit) return new String[0];
            total += 1;
            lenOfIndice += len(total);
        }

        String[] result = new String[total];
        int index = 0;
        int seq = 1;
        int totalDigits = len(total);
        while (index < message.length()) {
            StringBuilder builder = new StringBuilder();
            int canRead = limit - 3 - len(seq) - totalDigits;
            while (index < message.length() && canRead-- > 0)
                builder.append(message.charAt(index++));
            
            builder.append('<').append(seq).append('/').append(total).append('>');
            result[seq-1] = builder.toString();
            seq++;
        }
        return result;
    }

    private static int len(int n) {
        int len = 0;
        while (n > 0) {
            n /= 10;
            len++;
            }
        return len;
    }
}
binary search lắm edge case quá nên t linear search mẹ cho rồi
 
Top