头像

Kylin


访客:2483

离线:8天前



Kylin
8天前

https://www.luogu.com.cn/paste/yo55yexq

这个代码是高精度减法的代码,就是luogu测试点一个没过(大样例), 然后acwing的大数据也没过,求大佬解惑。数据范围没问题。

`#include[HTML_REMOVED]
using namespace std;
const int maxn = 10500;
int A[maxn], B[maxn], ans[maxn];
string a, b;
int num;

void pd()
{
if(a.size() < b.size() || (a.size() == b.size() && a < b))
{
cout << “-“;
swap(a, b);
}
}

int main()
{
cin >> a >> b;
pd();
for(int i = 0; i < a.size(); i )
A[a.size() - i - 1] = (a[i] - ‘0’);
for(int i = 0; i < b.size(); i
)
B[b.size() - i - 1] = (b[i] - ‘0’);
for(int i = 0; i < a.size(); i ++)
{
if(A[i] < B[i])
{
A[i + 1] –;
A[i] += 10;
}
ans[i] = A[i] - B[i];
}
num = a.size()-1;
while(!ans[num]) num–;
if(num == 0 && ans[num] == 0)
cout << “0”;
else {
for(int i = num; i >= 0; i –)
cout << ans[i];
}
return 0;

}`




Kylin
11天前

将yxc的代码中do-while改成while
只能过去小数据,大数据就是tle

https://www.luogu.com.cn/paste/2d3wnruk

求大佬赐教




Kylin
1个月前

题目链接 https://www.acwing.com/problem/content/787/
快排模板
`#include[HTML_REMOVED]
using namespace std;
const int maxn = 1e6+10;
int n, q[maxn];

void quick_sort(int q[], int l, int r)
{
if(l >= r) return;
int x = q[l], i = l - 1, j = r + 1;
while(i < j)
{
do i++; while(q[i] < x);
do j–; while(q[j] > x);
if(i < j) swap(q[i], q[j]);
}
quick_sort(q, l, j);
quick_sort(q, j+1, r);
}

int main()
{
scanf(“%d”, &n);
for(int i = 0; i < n; i)
scanf(“%d”, &q[i]);
quick_sort(q, 0, n-1);
for(int i = 0; i < n; i
)
printf(“%d “, q[i]);
return 0;
}
`
在这段代码中,如果 x = q[l+r >> 1] 改成 x = q[l] 就会超时。
想问各位大佬x = q[l]这样不就是直接赋值么, 为什么会超时????




Kylin
3个月前
//这里填你的代码^^
class Solution {
public:
    int maxVowels(string s, int k) {
        unordered_set<char> S({'a','e','i','o','u'});
        int res=0;
        for(int i=0,cnt=0;i<s.size();i++){
            cnt+=S.count(s[i]);
            if(i>=k) cnt-=S.count(s[i-k]);
            if(i>=k-1) res=max(res,cnt);
        }
        return res;
    }
};
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~



Kylin
3个月前
//这里填你的代码^^
class Solution {
public:
    int isPrefixOfWord(string sentence, string searchWord) {
        stringstream ssm(sentence);
        string word;
        for(int i=1;ssm>>word;i++){
            if(word.substr(0,searchWord.size())==searchWord)
                return i;
        }
        return -1;
    }
};
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~



Kylin
4个月前
//这里填你的代码^^
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> ans;
    vector<vector<int>> findPath(TreeNode* root, int sum) {
        vector<int> path;
        dfs(root,sum,path);
        return ans;
    }
    void dfs(TreeNode* root, int sum,vector<int>path){
        if(!root) return;
        path.push_back(root->val);
        sum-=root->val;
        if(!root->left&&!root->right&&!sum) ans.push_back(path);
        dfs(root->left,sum,path);
        dfs(root->right,sum,path);
        path.pop_back();
        sum+=root->val;
    }
};
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~



Kylin
4个月前
//这里填你的代码^^
class Solution {
public:
    vector<int> seq;
    bool verifySequenceOfBST(vector<int> sequence) {
        seq=sequence;
        if(seq.empty()) return true;
        return dfs(0,seq.size()-1);
    }
    bool dfs(int l,int r){
        int k,root;
        root=seq[r];
        while(k<r&&seq[k]<root) k++;
        for(int i=k;i<r;i++) if(seq[i]<root) return false;
        if(l>=r) return true;
        return dfs(l,k-1)&&dfs(k,r-1);
    }
};
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~



Kylin
4个月前
//这里填你的代码^^
class Solution {
public:
    ListNode *entryNodeOfLoop(ListNode *head) {
        if(!head||!head->next) return 0;
        ListNode *p=head,*q=head;
        while(p&&q)
        {
            p=p->next;
            q=q->next;
            if(q) q=q->next;
            else return 0;
            if(p==q)
            {
                p=head;
                while(p!=q){
                    p=p->next;
                    q=q->next;
                }
                return p;
            }
        }
        return 0;
    }
};
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~



Kylin
4个月前
//这里填你的代码^^
class MinStack {
public:
    /** initialize your data structure here. */
    stack<int> stackValue;
    stack<int> stackMin;
    MinStack() {

    }

    void push(int x) {
        stackValue.push(x);
        if (stackMin.empty() || stackMin.top() >= x)
            stackMin.push(x);
    }

    void pop() {
        if (stackMin.top() == stackValue.top()) stackMin.pop();
        stackValue.pop();
    }

    int top() {
        return stackValue.top();
    }

    int getMin() {
        return stackMin.top();
    }
};

//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~



Kylin
4个月前
//这里填你的代码^^
class Solution {
public:
    vector<int> printMatrix(vector<vector<int>>& matrix) {
        vector<int> res;
        if (matrix.empty()) return res;
        int n = matrix.size(), m = matrix[0].size();
        vector<vector<bool>> st(n, vector<bool>(m, false));
        int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
        int x = 0, y = 0, d = 1;
        for (int k = 0; k < n * m; k ++ )
        {
            res.push_back(matrix[x][y]);
            st[x][y] = true;

            int a = x + dx[d], b = y + dy[d];
            if (a < 0 || a >= n || b < 0 || b >= m || st[a][b])
            {
                d = (d + 1) % 4;
                a = x + dx[d], b = y + dy[d];
            }
            x = a, y = b;
        }
        return res;
    }
};

//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~