【CodeForces 159C】String Manipulation 1.0(字符串处理)

来源:互联网 发布:vs编写windows程序 编辑:程序博客网 时间:2024/06/05 17:14

C. String Manipulation 1.0
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

One popular website developed an unusual username editing procedure. One can change the username only by deleting some characters from it: to change the current name s, a user can pick number p and character c and delete the p-th occurrence of character c from the name. After the user changed his name, he can't undo the change.

For example, one can change name "arca" by removing the second occurrence of character "a" to get "arc".

Polycarpus learned that some user initially registered under nickname t, where t is a concatenation of k copies of string s. Also, Polycarpus knows the sequence of this user's name changes. Help Polycarpus figure out the user's final name.

Input

The first line contains an integer k (1 ≤ k ≤ 2000). The second line contains a non-empty string s, consisting of lowercase Latin letters, at most100 characters long. The third line contains an integer n (0 ≤ n ≤ 20000) — the number of username changes. Each of the next n lines contains the actual changes, one per line. The changes are written as "pi ci" (without the quotes), where pi (1 ≤ pi ≤ 200000) is the number of occurrences of letter ci, ci is a lowercase Latin letter. It is guaranteed that the operations are correct, that is, the letter to be deleted always exists, and after all operations not all letters are deleted from the name. The letters' occurrences are numbered starting from 1.

Output

Print a single string — the user's final name after all changes are applied to it.

Examples
input
2bac32 a1 b2 c
output
acb
input
1abacaba41 a1 a1 c2 b
output
baa
Note

Let's consider the first sample. Initially we have name "bacbac"; the first operation transforms it into "bacbc", the second one — to "acbc", and finally, the third one transforms it into "acb".

题目大意:字符串s是给定c重复k次的字符串,依次删除第p个ch字母,输出最后得到的子串

思路:用二维数组循环模拟,先统计每个子串中每个字母出现的个数,再循环遍历字符串

#include <bits/stdc++.h>using namespace std;int a[2005][27];bool f[2005][102];int main(){    int k,n,m,num,cot;    char s[102],c;    memset(f,true,sizeof(f));    scanf("%d%s",&k,s);    for (int i=1; i<=k; i++)        for (int j=0; j<strlen(s); j++)            a[i][s[j]-97]++;    scanf("%d",&n);    while(n--){        scanf("%d %c",&m,&c);        cot=0;  //第i层前的总个数        for (int i=1; i<=k; i++){            if(cot + a[i][c-97] >= m){                a[i][c-97]--;                num=cot;                for (int j=0; j<strlen(s); j++){                    if(s[j]==c && num==m-1 && f[i][j]){                        f[i][j]=false;                        break;                    }                    else if(s[j]==c && f[i][j]) num++;                }                break;            }            else cot+=a[i][c-97];        }    }    for (int i=1; i<=k; i++)        for (int j=0; j<strlen(s); j++)            if(f[i][j]) printf("%c",s[j]);    printf("\n");    return 0;}

百度到有人用vector实现,代码简洁了许多,第一次使用vector,感受到了它的友好,用vector来进行删除操作太方便了,不过vector比数组慢很多。开始生成s时,用strcat实现,结果第4个就T了,改成string的+居然就能过了,原来strcat比string慢的不是一个数量级啊,涨姿势了。

#include <bits/stdc++.h>#define manx 200005using namespace std;vector<int>v[27];bool f[manx];int main(){    int k,n,m,x;    string c,s;    char ch;    cin>>k>>c;    while(k--) s+=c;       for (int i=0; i<s.length(); i++)        v[s[i]-97].push_back(i+1);    scanf("%d",&m);    while(m--){        scanf("%d %c",&x,&ch);        f[ v[ch-97][x-1] ]=1;        v[ch-97].erase(v[ch-97].begin() + x-1);    }    for (int i=0; i<s.length(); i++)        if(!f[i+1]) printf("%c",s[i]);    printf("\n");    return 0;}


0 0
原创粉丝点击