• Codeforces 159C String Manipulation 1.0


    http://codeforces.com/problemset/problem/159/C

    题意就是给你k个相同的字符串S,有n个操作,每次操作时把第p个字母c去掉,问最后得到的串。

    AC Code
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 using namespace std;
     5 #define lson l,m,rt<<1
     6 #define rson m+1,r,rt<<1|1
     7 #define maxn 2000
     8 struct node{
     9     int cnt[26];
    10 }setree[maxn<<2];
    11 char ss[maxn][105],s[105];
    12 int cnt[26],n;
    13 void pushup(int rt)
    14 {
    15     for(int i=0;i<26;i++)
    16     setree[rt].cnt[i]=setree[rt<<1].cnt[i]+setree[rt<<1|1].cnt[i];
    17 }
    18 void build(int l,int r,int rt)
    19 {
    20     if(l==r){
    21         memset(setree[rt].cnt,0,sizeof(setree[rt].cnt));
    22         strcpy(ss[l],s);
    23         for(int i=0;i<26;i++)
    24         setree[rt].cnt[i]=cnt[i];
    25         return;
    26     }
    27     int m=(l+r)>>1;
    28     build(lson);
    29     build(rson);
    30     pushup(rt);
    31 }
    32 void update(int l,int r,int rt,int num,char c)
    33 {
    34     if(l==r){
    35         setree[rt].cnt[c-'a']--;
    36         int i=-1;
    37         while(num--){
    38             i++;
    39             while(ss[l][i]!=c)
    40             i++;
    41         }
    42         ss[l][i]='0';
    43         return;
    44     }
    45     int m=(l+r)>>1;
    46     if(setree[rt<<1].cnt[c-'a']>=num)
    47     update(lson,num,c);
    48     else
    49     update(rson,num-setree[rt<<1].cnt[c-'a'],c);
    50     pushup(rt);
    51 }
    52 void query(int l,int r,int rt)
    53 {
    54     if(l==r){
    55         for(int i=0;i<n;i++)
    56         if(ss[l][i]!='0')
    57         putchar(ss[l][i]);
    58         return;
    59     }
    60     int m=(l+r)>>1;
    61     query(lson);
    62     query(rson);
    63 }
    64 int main()
    65 {
    66     int k;
    67     scanf("%d%s",&k,s);
    68     memset(cnt,0,sizeof(cnt));
    69     n=strlen(s);
    70     for(int i=0;i<n;i++)
    71     cnt[s[i]-'a']++;
    72     build(1,k,1);
    73     int m;
    74     scanf("%d",&m);
    75     while(m--){
    76         int num;
    77         char op[5];
    78         scanf("%d%s",&num,op);
    79         update(1,k,1,num,op[0]);
    80     }
    81     query(1,k,1);
    82     return 0;
    83 }
  • 相关阅读:
    Python语言基础04-构造程序逻辑
    Python语言基础03-分支和循环结构
    Python语言基础02-变量和运算
    Python语言基础01-初识Python
    Hadoop+Hbase分布式集群架构“完全篇”
    kubernetes系列12—二个特色的存储卷configmap和secret
    安装AIDE文件完整性检测
    PAM和账户安全配置
    SSH安全配置
    CentOS7的审计配置
  • 原文地址:https://www.cnblogs.com/kim888168/p/3048253.html
Copyright © 2020-2023  润新知