LeetCode Weekly 184

first question

To determine whether a character string is a substring, the efficient way should be a dictionary tree. After sorting in lexicographic order, the tree is built and the answer can be obtained during the process of rebuilding.
But this is the first question in the competition, so I use contains directly

c#

public class Solution {
    public IList<string> StringMatching(string[] words) {
        
        IList<string> ans = new List<string>();
        
        for(int i=0;i<words.Length;i++)
        {
            for(int j=0;j<words.Length;j++)
            {
                if(i==j)
                    continue;
                if(words[j].Contains(words[i]))
                {
                    ans.Add(words[i]);
                    break;
                }
            }
        }
        
        return ans;
        
    }
}

The second question The
length of the array is only 1000. Every time you change the position, you can violently update the position of other numbers.


    
class Solution {
public:
    int p[1005];
    int s[1005];
    vector<int> processQueries(vector<int>& queries, int m) {
        
        for(int i=1;i<=m;i++)
        {
            p[i-1]=i;
            s[i]=i-1;
        }
        
        vector<int> ans;
        for(int i=0;i<queries.size();i++)
        {
            int x = queries[i];
            ans.push_back(s[x]);
            
            int y = s[x];
            for(int j=0;j<y;j++)
            {
                s[p[j]]++;
            }
            for(int j=y;j>0;j--)
            {
                p[j]=p[j-1];
            }
            s[x]=0;
            p[0]=x; 
        }
        return ans;
           
    }
};

The third question
is a very simple string replacement problem

class Solution {
public:
    string entityParser(string text) {
        
        string ans="";
        
        int tag=0;
        string s="";
        for(int i=0;i<text.length();i++)
        {
            if(!tag&&text[i]=='&')
            {
                tag=1;
                continue;
            }
            if(tag&&text[i]==';')
            {
                if(s=="quot")
                {
                    //ans+=92;
                    ans+='"';
                }
                else if(s=="apos")
                {
                    ans+="'";
                }
                else if(s=="amp")
                {
                    ans+="&";
                }
                else if (s=="gt")
                {
                    ans+=">";
                }
                else if(s=="lt")
                {
                    ans+="<";
                }
                else if(s=="frasl")
                {
                    ans+="/";
                }
                else
                {
                    ans+="&";
                    ans+=s;
                    ans+=";";
                }
                tag=0;
                s="";
                continue;
            }
            
            if(!tag)
                ans+=text[i];
            else
            {
                s+=text[i];
            }
        }
        
        return ans;
        
    }
};

Question 4
A very important example is given in the test sample, that is, when n = 1, there are 12 cases in total. For all n is the combination of the 12 cases. We find the situation where the upper and lower parts cannot be combined. You can get the answer easily by using the recursive relationship.

const int mod=1e9+7;
class Solution {
public:
    int b[12][3];
    vector<int> d[12];
    int c[3];
    int pos=0;
    int a[5005][12];
    int numOfWays(int n) {
        
        dfs(0);
        for(int i=0;i<12;i++)
        {
            for(int j=0;j<12;j++)
            {
                if(i==j) continue;
                int tag=0;
                for(int k=0;k<3;k++)
                {
                    if(b[i][k]==b[j][k])
                    {
                        tag=1;
                        break;
                    }
                }
                if(!tag)
                    d[i].push_back(j);
            }
        }
        
        for(int i=0;i<12;i++)
        {
            a[1][i]=1;
        }
        for(int i=2;i<=n;i++)
        {
            for(int j=0;j<12;j++)
            {
                int x = a[i-1][j];
                for(int k=0;k<d[j].size();k++)
                {
                    a[i][d[j][k]] +=x;
                    a[i][d[j][k]] %= mod;
                }
            }
        }
        
        int ans=0;
        for(int i=0;i<12;i++)
        {
            ans+=a[n][i];
            ans%=mod;
        }
        
        return ans;
        
        
    }
    
    void dfs(int i)
    {
        if(i==3)
        {
            for(int i=0;i<3;i++)
            {
                b[pos][i]=c[i];
            }
            pos++;
            return;
        }
        
        for(int j=0;j<3;j++)
        {
            if(i!=0&&j==c[i-1])
                continue;
            c[i]=j;
            dfs(i+1);
        }
    }
};

Guess you like

Origin www.cnblogs.com/dacc123/p/12684715.html