zoukankan      html  css  js  c++  java
  • Codeforces909D Colorful Points(缩点)

    http://codeforces.com/problemset/problem/909/D

    直接模拟超时。要运用缩点的方法,把相同的一段缩成一点,记录有几个。

    对于非首尾的缩点每次-2,首尾的-1。

    注意strlen不要放循环里,因为这个超时找了好久。。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<cstdlib>
     6 #include<cmath>
     7 #include<map>
     8 #include<vector>
     9 #include<stack>
    10 #include<queue>
    11 #define lson l, m, rt<<1
    12 #define rson m+1, r, rt<<1|1
    13 #define IO ios::sync_with_stdio(false);cin.tie(0);
    14 #define INF 0x3f3f3f3f
    15 #define MAXN 500010
    16 const int MOD=1e9+7;
    17 typedef long long ll;
    18 using namespace std;
    19 typedef struct{
    20     char c;
    21     int num;
    22 }Node;
    23 Node node[1000010];
    24 char ss[1000010];
    25 int len;
    26 int update(int len)
    27 {
    28     int j=0, flag = 0;
    29     for(int i = 0; i < len; i++){
    30         if(node[i].num>0){//该点还有几个 
    31             if(!flag){//第一次找到,直接赋给node[0]
    32                 flag = 1;
    33                 node[j] = node[i];
    34             }
    35             else if(node[i].c == node[j].c){
    36                 node[j].num += node[i].num;
    37             }
    38             else{
    39                 node[++j] = node[i]; 
    40             }
    41         }
    42     }
    43     return j;
    44 }
    45 int solve()
    46 {
    47     int len = update(strlen(ss))+1, cnt=0;//第一次update用的是缩点前长度,得到缩点后长度 
    48     while(len>1){
    49         for(int i = 1;i < len-1; i++){
    50             node[i].num -= 2;
    51         }
    52         node[0].num--; node[len-1].num--;
    53         len = update(len)+1;
    54         cnt++;
    55     }
    56     return cnt;
    57 }
    58 int main()
    59 {
    60     IO;
    61     cin >> ss;
    62         int len = strlen(ss);
    63     for(int i = 0; i < len; i++){//注意,循环里不要放strlen!!会很耗很耗时
    64         node[i].c = ss[i];
    65         node[i].num = 1;
    66     }
    67     cout << solve() << endl;
    68     return 0;
    69 } 
  • 相关阅读:
    uni-app拒绝授权后再次授权
    vue触底操作
    vue滚动条滚到到底部触发的方法
    pagination插件使用例子
    修改后台返回数据的字段
    v-cloak指令的作用
    修改checkbox样式
    获取selected的值
    前端工程师必备的几个实用网站
    html发展史简介(摘抄)
  • 原文地址:https://www.cnblogs.com/Surprisezang/p/8736206.html
Copyright © 2011-2022 走看看