481 Magical String

 
像之前那个base ,首先对magic string做出分析,才能够分析这道题的规律
读题:
//magic string仅包含1 2 并遵循
//串联字符串中 ‘1’ 和 ‘2’ 的连续出现次数可以生成该字符串。concatenating 字符串中的 1 2 contiguous occurences
//像是compress string的升级版
//magic string
思路:
//算前n个字符种1的数目:将前n项全部相加Sum ,会表示前sum项,反过来,如果n为某个数x及其之前的字符相加,则x是通过计数得到的
//如果已知s 那么res 就等于遍历 s , prev ++ 计数变量prev碰到不同的数字就判断prev == 1 ? res++;res;并且prev = 0 ;—>逆推出这道题难点是还原s
//已知s[0] = 1 , s[1] = 2所以可以推后面的吗? 主要是这个问题,
//频率 组数 当前位 双指针
class Solution {
public:
    int magicalString(int n) {
        if(n <= 3) return 1;
        int occur = 0 , sum = 3 , res = 1;
        vector<int> s = {1 , 2 , 2};
        for(int i = 2; i < n ; i ++){
            //填充s
            occur = s[i];//确定第i+1组的字符数 第i+1组有s[i]个字符
            sum += occur;//记录可以被推测的位数 , 确定可以推断的位置 sum-1 sum-2
            if(occur == 2){
                //判断是1还是2
                s.push_back(s[sum - 3] == 2 ? 1 : 2);//每组的开头肯定和前一组不一致
                s.push_back(s[sum - 3] == 2 ? 1 : 2);
            }else{
                s.push_back(s[sum - 2] == 2 ? 1 : 2);
            }
            //计数1
            if(s[i] == 1){
                res++;
            }
        }
        return res;
    }
};
优化【积累学习】
class Solution {
public:
    int magicalString(int n) {
        if (n <= 0) return 0;
        if (n <= 3) return 1; // s = "122" for n <= 3
        vector<int> s = {1, 2, 2}; // 初始化前3个字符
        int res = 1; // 已经有一个 '1'
        int head = 2; // 当前字符的位置
        int tail = 3; // 新字符的位置
        while (tail < n) {
            int count = s[head]; // 取出当前字符需要生成的次数
            int nextNum = s[tail - 1] == 1 ? 2 : 1; // 下一个字符类型
            for (int i = 0; i < count; ++i) {
                if (tail >= n) break;
                s.push_back(nextNum);
                if (nextNum == 1) res++; // 如果是 '1',则计数
                tail++;
            }
            head++;
        }
        return res;
    }
};

c++代码学习 : 数组动态创建错误
class Solution {
public:
    int magicalString(int n) {
        if(n <= 3) return 1;
        int occur = 0 , sum = 1 , res = 1;
        vector<int> s(n,0);//除非n为已知数,否则不能这么干
        s[0] = 1;
        s[1] = 2;
        s[2] = 2;
//修正
s = {1,2,2};
之后只能s.push_back();
522 Longest Uncommon Subsequence II

class Solution {
public:
    bool isSubsequence(string str1 , string str2){
        int n1 = str1.size() , n2 = str2.size();
        int s1 = 0 , s2 = 0;//指针
        if(str1 == str2)return true;
        //str2是否为str1的子字符串
        while(s1 < n1 && s2 < n2){
            if(str1[s1] == str2[s2]){
                s2++;
            }
            s1++;
        } 
        return s2 == n2;
    }
    int findLUSlength(vector<string>& strs) {
        //最长 特殊 序列
        //strs 返回最长特殊序列的长度,不存在则-1
        //最长特殊序列:着重点在 特殊,独一无二的 不为别人的一部分/子序列的序列
        //一般先找最长的长度,然后逐一下降?--->一一对比 contains中的数值都不大
        int n = strs.size();
        int max = -1;
        for(int i = 0 ; i < n ; i++){
            bool isUnique = true;
            for(int j = 0 ; j < n ; j++){
                if(i != j && isSubsequence(strs[j] , strs[i])){
                    isUnique = false;
                    break;
                    
                }
            }
            if(isUnique){
                max = std::max(max, static_cast<int>(strs[i].size()));
            }
        }
        return max; 
    }
};
// std::max()



















