首页 > 其他分享 >C - Many Replacement

C - Many Replacement

时间:2024-02-25 11:56:16浏览次数:22  
标签:字符 映射 int Many char abc342 Replacement

C - Many Replacement

https://atcoder.jp/contests/abc342/tasks/abc342_c

 

思路

根据q组字符转换动作,找出每个字符最终将变成的字符。

 

初始化字母转换表:

映射前 -> 映射后

a -> a

b -> b
......

z -> z

 

对于q组字符变换, 依次执行:

c -> d

 

如果 c 在 “映射后” 字符集合中存在, 则将“字母转换表中”的“映射后”的值替换为 d,

否则,不做替换。

Code

https://atcoder.jp/contests/abc342/submissions/50626633

int n;
string s;
int q;

int main()
{
    cin >> n;
    cin >> s;

    map<char, char> cc;
    
    for(char one='a'; one<='z'; one++){
        cc[one] = one;
    }

    cin >> q;
    for(int i=0; i<q; i++){
        char c, d;

        cin >> c >> d;

        if (c == d) {
            continue;
        }

        for(auto it: cc){
            char oldc = it.first;
            char newc = it.second;
            
//            cout << "oldc=" << oldc << " newc="<< newc << endl;
            
            if (newc == c){
                cc[oldc] = d;
//                cout << "oldc=" << oldc << " cc[oldc]="<< cc[oldc] << endl;
            }
        }

//        cc[c] = d;
    }

    for(int i=0; i<s.size(); i++){
        char one = s[i];
        if (cc[one]){
            s[i] = cc[one];
        }
    }

    cout << s << endl;

    return 0;
}

 

标签:字符,映射,int,Many,char,abc342,Replacement
From: https://www.cnblogs.com/lightsong/p/18032216

相关文章

  • 「题解」ARC139F Many Xor Optimization Problems
    考虑线性空间的标准基底(即每个主元都只有对应向量有值),答案为所有基底异或和。对于一个秩\(k\)计算它对答案的贡献。固定主元为\(a_1<a_2<\cdots<a_k\),各种情况应该是等概率,也就是对第\(i\)个基底来说,\(a_i\)位一定为\(1\),再往下的位除了在\(a\)出现过的以外的位0/1是......
  • itertools.combinations_with_replacement和itertools.combinations的区别
    itertools.combinations和itertools.combinations_with_replacement都是Python标准库中的工具,用于生成组合。它们的主要区别在于对元素的重复使用上。itertools.combinations(iterable,r):生成不含重复元素的组合。iterable是可迭代对象,例如列表或字符串。r是生成的......
  • CF1697F Too Many Constraints
    题意简述有一个长度为\(n\)的整数序列\(a\),值域为\([1,k]\),有\(m\)条限制:1ix,表示\(a_i\not=x\)2ijx,表示\(a_i+a_j\lex\)3ijx,表示\(a_i+a_j\gex\)试构造一个可能的\(a\),或报告无解。\(n,m\le2\times10^4,k\le10\)。分析看上去像是一个差分约束题,......
  • [ABC327G] Many Good Tuple Problems 题解
    Description对于一对长度均为\(M\)且元素值在\(\left[1,N\right]\)之间的序列\((S,T)\),定义其为好的当且仅当:存在一个长度为\(N\)的\(01\)序列\(X\),使得其满足如下条件:对于任意\(i\in\left[1,M\right]\),有\(X_{S_i}\neqX_{T_i}\)。给定\(N,M\),求在......
  • openGauss学习笔记-206 openGauss 数据库运维-常见故障定位案例-too many clients alr
    openGauss学习笔记-206openGauss数据库运维-常见故障定位案例-toomanyclientsalready206.1高并发报错“toomanyclientsalready”或无法创建线程206.1.1问题现象高并发执行SQL,报错“sorry,toomanyclientsalready”;或报无法创建线程、无法fork进程等错误。206.1.2......
  • 无涯教程-Java 正则 - String replaceAll(String replacement)函数
    java.util.regex.Matcher.replaceAll(Stringreplacement)方法使用给定的替换字符串替换与该模式匹配的每个子序列。StringreplaceAll-声明publicStringreplaceAll(Stringreplacement)replacement  - 替换字符串。StringreplaceAll-返回值通过用替换字符串替......
  • 无涯教程-Java 正则 - Matcher static String quoteReplacement(String s)函数
    java.time.Matcher.quoteReplacement(Strings)方法返回指定字符串的文字替换字符串。staticStringquoteReplacement-声明publicstaticStringquoteReplacement(Strings)s  - 要被字符串化的字符串。staticStringquoteReplacement-返回值文字字符串替换。......
  • 无涯教程-Java 正则 - Matcher appendReplacement(StringBuffer sb, String replacem
    java.time.Matcher.appendReplacement(StringBuffersb,Stringreplacement)方法实现了附加和替换操作。MatcherappendReplacement-声明publicMatcherappendReplacement(StringBuffersb,Stringreplacement)sb           - 目标字符串缓冲区......
  • Mysql报错:too many connections原因及解决方法
    原因是mysql连接数过多解决方案一:1、linux登录mysql:mysql-uroot-p;2、查看mysql允许的最大连接数showvariableslike'%max_connections%';3、查看这次mysql服务启动到现在,同一时刻最多mysql有多少连接showstatuslike'Max_used_connections';4、修改mysql最大连......
  • 启用微服务报错:Method has too many Body parameters
    启用微服务报错:MethodhastoomanyBodyparameterspackagecom.llq.springcloud.controller;@RestControllerpublicclassStorageController{@ResourceprivateStorageServicestorageService;//扣减库存你@PostMapping("/storage/reduce")......