总时间限制: 1000ms 内存限制: 65536kB
- 描述
-
给定n个字符串(从1开始编号),每个字符串中的字符位置从0开始编号,长度为1-500,现有如下若干操作:
- copy N X L:取出第N个字符串第X个字符开始的长度为L的字符串。
- add S1 S2:判断S1,S2是否为0-99999之间的整数,若是则将其转化为整数做加法,若不是,则作字符串加法,返回的值为一字符串。
- find S N:在第N个字符串中从左开始找寻S字符串,返回其第一次出现的位置,若没有找到,返回字符串的长度。
- rfind S N:在第N个字符串中从右开始找寻S字符串,返回其第一次出现的位置,若没有找到,返回字符串的长度。
- insert S N X:在第N个字符串的第X个字符位置中插入S字符串。
- reset S N:将第N个字符串变为S。
- print N:打印输出第N个字符串。
- printall:打印输出所有字符串。
- over:结束操作。
其中N,X,L可由find与rfind操作表达式构成,S,S1,S2可由copy与add操作表达式构成。
输入 -
第一行为一个整数n(n在1-20之间)
接下来n行为n个字符串,字符串不包含空格及操作命令等。
接下来若干行为一系列操作,直到over结束。
输出 -
根据操作提示输出对应字符串。
样例输入 -
3 329strjvc Opadfk48 Ifjoqwoqejr insert copy 1 find 2 1 2 2 2 print 2 reset add copy 1 find 3 1 3 copy 2 find 2 2 2 3 print 3 insert a 3 2 printall over
样例输出 -
Op29adfk48 358 329strjvc Op29adfk48 35a8
提示 -
推荐使用string类中的相关操作函数。
#include <iostream> #include <string> #include <vector> #include <queue> #include <cstdlib> #include <cstring> #include <cstdio> using namespace std; vector<string> vec; queue<string> que; char * strr = new char[101]; string mycopy(int n, int x, int l) { string str = vec.at(n-1); return str.substr(x, l); } string myadd(string s1, string s2) { for (int i = 0; i < s1.size(); i++) if (s1.at(i) < '0' || s1.at(i) > '9') return s1 + s2; for (int i = 0; i < s2.size(); i++) if (s2.at(i) < '0' || s2.at(i) > '9') return s1 + s2; int a = atoi(s1.c_str()); int b = atoi(s2.c_str()); if (a >= 0 && a <= 99999 && b >= 0 && b <= 99999) { int c = a + b; sprintf(strr, "%d", c); return strr; } else return s1 + s2; } int myfind(int n, string s) { int pos = vec.at(n-1).find(s); if (pos == string::npos) return s.size(); else return pos; } int myrfind(int n, string s) { int pos = vec.at(n-1).rfind(s); if (pos == string::npos) return s.size(); else return pos; } void myinsert(int n, int x, string s) { vec.at(n-1).insert(x, s); } void myreset(int n, string s) { vec.at(n-1)=s; } void myprint(int n) { cout << vec.at(n-1) << endl; } void myprintall() { for ( int i = 0; i < vec.size(); i++) cout << vec.at(i) << endl; } void text( char* ch) { string str; for (int i = 0; ch[i] != '\0'; i++) { if (ch[i] != ' ') { str.push_back(ch[i]); } else { que.push(str); str.clear(); } } que.push(str); } string process() { string str = que.front(); que.pop(); if (str == "copy") { int n = atoi(process().c_str()); int x = atoi(process().c_str()); int l = atoi(process().c_str()); return mycopy(n, x, l); } else if (str == "add") { string s1 = process(); string s2 = process(); return myadd(s1, s2); } else if (str == "find") { string s = process(); int n = atoi(process().c_str()); sprintf(strr, "%d", myfind(n, s)); return strr; } else if (str == "rfind") { string s = process(); int n = atoi(process().c_str()); sprintf(strr, "%d", myrfind(n, s)); return strr; } else { return str; } } void process1() { string str = que.front(); que.pop(); if (str == "insert") { string s = process(); int n = atoi(process().c_str()); int x = atoi(process().c_str()); myinsert(n, x, s); } else if (str == "reset") { string s = process(); int n = atoi(process().c_str()); myreset(n, s); } else if (str == "print") { int n = atoi(process().c_str()); myprint(n); } else if (str == "printall") myprintall(); } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { string str; cin >> str; vec.push_back(str); } char ch[501]; while(1) { gets(ch); if(ch[0]=='o') break; text(ch); process1(); } return 0; }