13692 - Aftermath's Ideology

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.9 kB
1
Indexable
Never
#include <stdio.h>
#include <string.h>

#define MAX_char 1000000

int n, tmp;
char alph[100000][26];
char s[MAX_char + 1];

void init();
void cpyalph();
void swap(int a, int b);

int main(void)
{
    freopen("test.txt", "r", stdin);
    freopen("out.txt", "w", stdout);

    scanf("%s", s);
    scanf("%d", &n);

    int op[n], first[n], second[n];
    for (int i = 0; i < n; ++i)
    {
        scanf("%d", &op[i]);
        if (op[i] == 1)
        {
            char a, b;
            scanf(" %c %c", &a, &b);
            first[i] = a;
            second[i] = b;
        }
        else if (op[i] == 2)
        {
            int a;
            char b;
            scanf(" %d %c", &a, &b);
            first[i] = a;
            second[i] = b;
        }
        else if (op[i] == 3)
        {
            int a, b;
            scanf(" %d %d", &a, &b);
            first[i] = a;
            second[i] = b;
        }
    }
    init();

    tmp = n;
    for (int i = n - 1; i >= 0; --i)
    {
        tmp--;
        if (op[i] == 1)
        {
            alph[tmp][first[i] - 'a'] = (char)second[i];
        }
        cpyalph();
    }
    for (int i = 0, l = strlen(s); i < l; ++i)
    {
        s[i] = alph[0][s[i] - 'a'];
    }

    for (int i = 0; i < n; ++i)
    {
        if (op[i] == 2)
        {
            s[first[i]] = alph[i][second[i] - 'a'];
        }
        else if (op[i] == 3)
        {
            swap(first[i], second[i]);
        }
    }

    puts(s);

    return 0;
}

void init()
{
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < 26; ++j)
        {
            alph[i][j] = j + 'a';
        }
    }
}

void cpyalph()
{
    if (tmp != 0)
    {
        for (int i = 0; i < 26; ++i)
        {
            alph[tmp - 1][i] = alph[tmp][i];
        }
    }
}

void swap(int a, int b)
{
    char tmp;
    tmp = s[a];
    s[a] = s[b];
    s[b] = tmp;
}