zoukankan      html  css  js  c++  java
  • 几道题-找规律-记录并查找

    http://abc048.contest.atcoder.jp/tasks/arc064_b

    这真是好题,有时间好好思考一下!为什么就没有英文题解呢!

    好好学习一下直线,判断点在直线的哪一侧,考虑叉乘的正负,考虑顺时针和逆时针顺序,研究一下!

    2. https://leetcode.com/contest/leetcode-weekly-contest-11/problems/convex-polygon/

    https://www.hackerrank.com/contests/w25/challenges/good-point

    这两道题差不多,都涉及多边形的性质。

    3. https://leetcode.com/contest/leetcode-weekly-contest-11/problems/unique-substrings-in-wraparound-string/

    这道题刚开始以为是数个数,然后就是找区间,然后l*(l + 1) / 2,后来发现还要去重,那不简单么,直接用set去重,后来发现tle, 因为子串太多了,然后不知道怎么解了,突然,我也不知道怎么想出,每个子串,可以压缩为首字符以及长度, 而且最终我们只需要记录每个字符开头的最大长度就可以了,最后的答案就是所有的长度加起来。ac以后,发现原来不过如此,还是需要多分析,多思考,怎么解决,有什么套路。

    4.https://leetcode.com/contest/leetcode-weekly-contest-11/problems/count-the-repetitions/

      418 Sentence Screen Fitting 这两道题目很相似, 我刚开始以为是二分,写了下,发现tle,因为原串就是100*1e6=1e8,扫一遍就超时了,根本不能多遍扫描,后来我想着:肯定是找规律,找到重叠的部分,然后计算,但是具体还是不知道怎么做,放弃了。

    然后看答案http://bookshadow.com/weblog/2016/12/04/leetcode-count-the-repetitions/讲的非常好,就是这样,还有上次那题,可惜加锁了,没法再进行提交,可惜,上次我就没有做出来!哎,真菜!彩笔!

    照着上面的题解写出的答案:

     1 typedef pair<int, int> pii;
     2 class Solution {
     3 public:
     4 int getMaxRepetitions(string s1, int n1, string s2, int n2) {
     5     int m1 = s1.size(), m2 = s2.size();
     6     int i, j, x, y; i = j = 0;
     7     map<pii, pii> ma;
     8     while(i < m1 * n1) {
     9         while(i < m1 * n1 && s1[i % m1] != s2[j % m2]) i++;
    10         if (i >= m1 * n1) break;
    11         x = i % m1; y = j % m2;
    12         if(ma.count({x, y})) {
    13             pii t = ma[{x, y}];
    14             int x1 = t.first, y1 = t.second;
    15             int cnt = (m1 * n1 - x1) / (i - x1);
    16             i = x1 + cnt * (i - x1);
    17             j = y1 + cnt * (j - y1);
    18         } else {
    19             ma[{x, y}] = {i, j};
    20         }
    21         if(i < m1 * n1) {
    22             i++;
    23             j++;
    24         }
    25     }
    26     return j / (m2 * n2);
    27 }
    28 };

     sentence screen fitting

     1 int work(int rows, int cols, vector<string> w) {
     2     int slen = 0;
     3     for (string s : w) {
     4         if(s.size() > cols) return 0;
     5         slen += s.size();
     6     }
     7     int n = w.size();
     8     slen += n;
     9     int pr, pc, pos, res;
    10     pr = pc = pos = res = 0;
    11     map<pii, pii> ma;
    12     while(pr < rows) {
    13         if(ma.count({pos, pc})) {
    14             pii t = ma[{pos, pc} ];
    15             int cnt = (rows - t.first) / (pr - t.first);
    16             res = t.second + cnt * (res - t.second);
    17             pr = t.first + cnt * (pr - t.first);
    18         } else {
    19             ma[{pos, pc} ] = {pr, res};
    20         }
    21         int ct = (cols - pc) / slen;
    22         res += ct;
    23         pc = pc + ct * slen + w[pos].size();
    24         if(pc <= cols) {
    25             pos++;
    26             pc++;
    27             if(pos == n) {
    28                 pos = 0;
    29                 res++;
    30             }
    31         }
    32         if(pc >= cols) {
    33             pr++;
    34             pc = 0;
    35         }
    36     }
    37     return res;
    38 }

    看似理解了,实际却不能很好的写出来,需要自己多写,而不仅仅是看懂!

  • 相关阅读:
    Tree MapByFold
    Tree DepthByFold
    Tree SizeByFold
    Tree MaximumByFold
    Tree Fold
    Tree Map
    Tree Depth
    Tree Maximum
    Tree Size
    Tree
  • 原文地址:https://www.cnblogs.com/y119777/p/6135907.html
Copyright © 2011-2022 走看看