↓ 跳过正文
  1. Posts/

codeforces #327 B Rebranding

·411 字·1 分钟

http://codeforces.com/contest/591/problem/B

题意:给定一个字符串。给出m组替换。对于某一组替换,给出x,y。将字符串中所有的字符x换成y,所有的字符y换成x. 字符串仅包含英文小写字母。

思路: 可以用一个char 到 char 的map 初始映射本身。。 然后进行m次修改。。需要注意的是,每一次修改要修改全部。。因为当进行完i次修改而要进行i+1次修改的时候。。value值为x的可能不止一个。。所以要从a到z都扫一遍。。

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2015年12月04日 星期五 19时13分12秒
 4File Name :code/cf/#327/B.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25
26
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=2E5+7;
34char st[N];
35
36int n,m;
37char x,y;
38map<char,char>mp;
39
40
41int main()
42{
43    freopen("code/in.txt","r",stdin);
44    mp.clear();
45    for ( int i = 0 ; i < 26 ; i++)
46    {
47	char tmp = char(i+97);
48	mp[tmp] = tmp;
49    }
50
51    scanf("%d %d",&n,&m);
52    scanf("%s",st);
53    getchar();
54    while (m--)
55    {
56	char x,y;
57	scanf("%c %c",&x,&y);
58	getchar();
59	for ( int i = 0 ; i < 26 ; i ++)
60	{
61	    char tmp = char(i+97);
62	    if (mp[tmp]==x) mp[tmp]=y;
63	    else if (mp[tmp]==y) mp[tmp] = x;
64	}
65    }
66    for ( int i = 0 ; i < n ;i++)
67    {
68	printf("%c",mp[st[i]]);
69    }
70    return 0;
71}

相关文章

codeforces edu1 B Queries on a String

·531 字·2 分钟
题意:给一个字符串(1E4),然后给m次操作(m<=300),每次操作是给定一个区间l,r,然后进行k次(k<=1E6)cyclic shift (rotation) 变换。 One operation of a cyclic shift (rotation) is equivalent to moving the last character to the position of the first character and shifting all other characters one position to the right.

(BC 一周年)hdu 5311 Hidden String

·767 字·2 分钟
http://acm.hdu.edu.cn/showproblem.php?pid=5311 题意:问能否从一个给定的字符串中拿出三个不相交的字串(原串可以有剩余),组成字符串“anniversary” 思路:暴力。 比赛的时候没做出来,sad 我发现我有一个问题,就是不敢跑暴力 有不少题其实正解就是暴力 或者有的题,暴力不是标解,但是绝对可A,可我就不敢写… 就觉得不会是这样.. 说到底还是不自信吧…

codeforces #327 A. Wizards' Duel

·293 字·1 分钟
题意:一个长度为l的走廊。两个人站在两端点。互相向对方发射某种魔法。A的魔法速度为p米/秒,B的魔法速度为q米/s,魔法相遇以后会反射。反射会发射人那里会再次发射。问两种魔法第二次相遇的时候距离A的距离。 思路:由于每种魔法的速度保持肯定不变。。所以不管第几次相遇。相遇点都是同一个。。。ans=p*(p+q)/l;

codeforces edu1 D. Igor In the Museum

·640 字·2 分钟
http://codeforces.com/contest/598/problem/D 题意:给第一个地图。 ‘.’是能走的,‘’是不能走的。**每个‘.’和’‘之间有一幅画,**给出k个起点,问对于每组起点,最多能观察到多少副画。