UVa 11988 (数组模拟链表) Broken Keyboard (a.k.a. Beiju Text)
2014-09-18 21:06
381 查看
题意:
模拟一个文本编辑器,可以输入字母数字下划线,如果遇到'['则认为是Home键,如果是']'则认作End键。
问最终屏幕上显示的结果是什么字符串。
分析:
如果在数组用大量的移动字符必然很耗时。所以next数组表示显示屏中s[i]右边的字符编号,变量cur模拟光标,即当前光标位于s[cur]的右边。
变量last记录显示屏最后一个字符的下标。
我理解的连接的情况应该是这样子的:
代码君
模拟一个文本编辑器,可以输入字母数字下划线,如果遇到'['则认为是Home键,如果是']'则认作End键。
问最终屏幕上显示的结果是什么字符串。
分析:
如果在数组用大量的移动字符必然很耗时。所以next数组表示显示屏中s[i]右边的字符编号,变量cur模拟光标,即当前光标位于s[cur]的右边。
变量last记录显示屏最后一个字符的下标。
我理解的连接的情况应该是这样子的:
//#define LOCAL #include <cstdio> #include <cstring> const int maxn = 100000 + 10; int last, cur, next[maxn]; char s[maxn]; int main(void) { #ifdef LOCAL freopen("11988in.txt", "r", stdin); #endif while(scanf("%s", s + 1) == 1) { int n = strlen(s + 1); last = cur = 0; next[0] = 0; for(int i = 1; i <= n; ++i) { char ch = s[i]; if(s[i] == '[') cur = 0; else if(s[i] == ']') cur = last; else { next[i] = next[cur]; next[cur] = i; if(cur == last) last = i; //更新最后一个字符编号 cur = i; //移动光标 } } for(int i = next[0]; i != 0; i = next[i]) printf("%c", s[i]); puts(""); } return 0; }
代码君
相关文章推荐
- UVa 11988 Broken Keyboard (a.k.a. Beiju Text) 数组模拟单向链表
- UVa 11988 - Broken Keyboard (a.k.a. Beiju Text) ( 模拟链表 )
- UVa 11988 Broken Keyboard(数组模拟链表)
- Broken Keyboard (a.k.a. Beiju Text) UVA, 11988(链表)
- UVa - 11988 Broken Keyboard(数组模拟链表)
- 链表 UVA 11988 Broken Keyboard (a.k.a. Beiju Text)
- UVA11988 Broken Keyboard (a.k.a. Beiju Text)【数组模拟链表】
- UVA - 11988 Broken Keyboard (a.k.a. Beiju Text)链表
- uva 11988 Broken Keyboard (a.k.a. Beiju Text)
- UVa 11988 - Broken Keyboard (a.k.a. Beiju Text)
- UVA - 11988 Broken Keyboard (a.k.a. Beiju Text)
- UVA 11988 - Broken Keyboard (a.k.a. Beiju Text)
- UVa 11988 Broken Keyboard(模拟链表)
- UVa 11988 Broken Keyboard (a.k.a. Beiju Text) (模拟链表)
- UVA11988:Broken Keyboard (a.k.a. Beiju Text)
- UVA 11988 Broken Keyboard (a.k.a. Beiju Text)
- Uva - 11988 - Broken Keyboard (a.k.a. Beiju Text)
- UVA 11988 Broken Keyboard (a.k.a. Beiju Text)
- UVA 11988 Broken Keyboard (a.k.a. Beiju Text) (链表,模拟)
- UVA 11988 - Broken Keyboard (a.k.a. Beiju Text)