2014-7-19

來源:互聯網
上載者:User

標籤:os   2014   io   for   cti   re   

昨天做了兩道題,感覺挺水的,就沒寫題解。但是今天感覺還是紀念一下我的努力吧。

第一道是POJ 3630 Phone List

一道基礎的Trie樹,但是因為採用了動態建樹,剛開始就TLE了。最後用了靜態儲存的方法就過了。

#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn = 100010;struct N{    int next[11];    bool sign;}node[maxn];string s;int sum;bool add(){    int len = s.length();    int root = 0;    for(int i = 0; i < len; i++)    {        int tmp = s[i] - ‘0‘;        if(node[root].next[tmp])        {            root = node[root].next[tmp];            if(i == len - 1)            return 1;            if(node[root].sign)            return 1;        }        else        {            node[root].next[tmp] = sum;            root = sum++;        }        if(i == len - 1)        node[root].sign = 1;    }    return 0;}int main(){    int t;    cin >> t;    while(t--)    {        sum = 1;        bool flag = 1;        memset(node, 0, sizeof(node));        int n;        cin >> n;        for(int i = 0; i < n; i++)        {            cin >> s;            if(flag)            if(add())            flag = 0;        }        if(flag)        cout << "YES" << endl;        else        cout << "NO" << endl;    }}



第二道是POJ 3067 Japan

是一道樹狀數組的題。也很簡單,但是我也是因為犯了很低級的錯誤導致WA和TLE了幾發。
最後調試的時候發現 樹狀數組的更新,如果傳進去的參數為負數就會死迴圈,一下恍然大悟,負數加起來最後會是0,之後就死了。

#include <iostream>#include <cstdio>#include <algorithm>#include <vector>#include <cstring>using namespace std;const int maxn = 1010;struct N{    int f, l;} node[maxn * maxn];int sum[maxn];int n, m, k;vector<int> p;bool cmp(N a, N b){    return a.f > b.f;}void insert(int t){    while(t <= m)    {        sum[t] += 1;        t += t & (-t);    }}int findsum(int t){    t--;    int ret = 0;    while(t > 0)    {        ret += sum[t];        t -= t & (-t);    }    return ret;}void print(){    for(int i = 1; i <= m; i++)    cout << i << " " << sum[i] << endl;}int main(){    int t;    cin >> t;    for(int T = 0; T < t; T++)    {        memset(sum, 0, sizeof(sum));        cin >> n >> m >> k;        for(int i = 0; i < k; i++)            scanf("%d%d", &node[i].f, &node[i].l);        sort(node, node + k, cmp);        int tt = 0;        while(node[tt].f == node[0].f)        {            insert(node[tt].l);            tt++;        }        long long ans = 0;        for(int i = tt; i < k; i++)        {            if(node[i].f == node[i - 1].f)            {                ans += findsum(node[i].l);                p.push_back(node[i].l);            }            else            {                for(int j = 0; j < p.size(); j++)                    insert(p[j]);                p.clear();                ans += findsum(node[i].l);                p.push_back(node[i].l);            }        }        p.clear();        printf("Test case %d: %lld\n", T + 1, ans);    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.