【CodeForces 159C】String Manipulation 1.0(字符串处理)
来源:互联网 发布:vs编写windows程序 编辑:程序博客网 时间:2024/06/05 17:14
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.
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.
Print a single string — the user's final name after all changes are applied to it.
2bac32 a1 b2 c
acb
1abacaba41 a1 a1 c2 b
baa
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;}
- 【CodeForces 159C】String Manipulation 1.0(字符串处理)
- H CodeForces 159C String Manipulation 1.0
- B - String Manipulation 1.0-CodeForces 159C
- codeforces 之 C. String Manipulation 1.0
- CF 159C String Manipulation 1.0
- Lua 标准库 - 字符串处理(string manipulation)
- Lua 标准库 - 字符串处理(string manipulation)
- 字符串操作说明(String Manipulation (CRT))
- VK Cup 2012 Qualification Round 2 C. String Manipulation 1.0
- cf_159C. String Manipulation线段树+字符串
- 【CodeForces 797C】Minimal string(贪心+字符串)
- Codeforces Round #423 (Div. 2) C. String Reconstruction(字符串)
- String Manipulation
- CodeForces 4C Registration system(字符串处理 | map)
- C++ string函数 与 C字符串处理函数(整理)
- C++ string函数 与 C字符串处理函数(整理)
- C++ string函数 与 C字符串处理函数(整理)
- CodeForces 496C 字符串阵处理
- Java不可改变字符串
- 常用STL算法样例
- 51nod 1091 线段的重叠(贪心)
- redis的学习
- 【BaseJava】关于Integer与int
- 【CodeForces 159C】String Manipulation 1.0(字符串处理)
- VMware+NAT+Ubuntu网络无连接问题
- 基于TCAM 的高速路由查找
- Linux网络协议
- Redux 入门教程(三):React-Redux 的用法
- 信息摘要技术及算法介绍
- Mock & Mockito
- SMAT,PACbio
- main.c(16): warning: #223-D: function "led_init" declared implicitly