Codeforces 600A Extract Numbers 【模拟】
2015-11-29 22:29
483 查看
A. Extract Numbers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given string s. Let's call word any
largest sequence of consecutive symbols without symbols ',' (comma) and ';'
(semicolon). For example, there are four words in string "aba,123;1a;0":
"aba", "123", "1a",
"0". A word can be empty: for example, the strings=";;"
contains three empty words separated by ';'.
You should find all words in the given string that are nonnegative INTEGER numbers without leading zeroes and build by them new string a.
String a should contain all words that
are numbers separating them by ',' (the order of numbers should remain the same as in the string s).
By all other words you should build string b in
the same way (the order of numbers should remain the same as in the strings).
Here strings "101", "0" are INTEGER numbers, but "01"
and "1.0" are not.
For example, for the string aba,123;1a;0 the string a would
be equal to "123,0" and string b would
be equal to "aba,1a".
Input
The only line of input contains the string s (1 ≤ |s| ≤ 105).
The string contains only symbols '.' (ASCII 46), ',' (ASCII
44), ';' (ASCII 59), digits, lowercase and uppercase latin letters.
Output
Print the string a to the first line and string b to
the second line. Each string should be surrounded by quotes (ASCII 34).
If there are no words that are numbers print dash (ASCII 45) on the first line. If all words are
numbers print dash on the second line.
Sample test(s)
input
output
input
output
input
output
input
output
Note
In the second example the string s contains five words: "1",
"", "01", "a0", "".
好弱。。。WA三次才过。
题意:给定一个字符串,字符';' 和 ','将串分开,每部分是一个单词,若出现中间为空的情况(连续两个分隔符),则认为中间的单词是空格。若单词是整数(不能有前导0),则放在串a里面,反之放在串b里面。在a,b串中,单词用','分开。若无单词是整数,a串为"-",若无单词是非整数,b串为"-"。最后输出a串和b串,如a串(b串)不是"-",输出时用""括起。
先填上空格,直接模拟就可以了。
AC代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define INF 0x3f3f3f
#define eps 1e-8
#define MAXN (100000+10)
#define MAXM (100000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1#define rr o<<1|1using namespace std;
char s[MAXN], str[MAXN*2];
char a[MAXN*2], b[MAXN*2];
bool judge(char op){
return op != ',' && op != ';';
}
bool check(char op){
return (op >= 'a' && op <= 'z') || (op >= 'A' && op <= 'Z') || (op == ' ') || (op == '.');
}
int main()
{
while(scanf("%s", s) != EOF)
{
int len = strlen(s), l;
if(len == 1 && (s[0] == ';' || s[0] == ','))
{
printf("-\n");
printf("%c,%c\n", 34, 34);
continue;
}
int top = 0;
for(int i = 0; i < len; i++)
{
if(i == 0)
{
if(s[i] == ',' || s[i] == ';')
{
str[top++] = ' ';
str[top++] = s[i];
if(s[i+1] == ',' || s[i+1] == ';')
str[top++] = ' ';
}
else
str[top++] = s[i];
}
else if(i == len-1)
{
if(s[i] == ',' || s[i] == ';')
{
str[top++] = s[i];
str[top++] = ' ';
}
else
str[top++] = s[i];
}
else
{
if((s[i] == ',' || s[i] == ';') && (s[i+1] == ',' || s[i+1] == ';'))
{
str[top++] = s[i];
str[top++] = ' ';
}
else
str[top++] = s[i];
}
}
str[top] = '\0';
a[0] = b[0]= '"'; a[1] = b[1] = '\0';
int timea = 0, timeb = 0;
for(int i = 0; i < top; )
{
if(judge(str[i]))
{
int j = i; int k = 0; bool flag = true;
while(j < top && judge(str[j]))
{
if(check(str[j]))
flag = false;
s[k++] = str[j++];
}
s[k] = '\0';
if(s[0] == '0' && k > 1)
flag = false;
if(flag)
{
if(timea)
{
l = strlen(a);
a[l] = ',';
a[l+1] = '\0';
}
if(s[0] != ' ')
strcat(a, s);
timea++;
}
else
{
if(timeb)
{
l = strlen(b);
b[l] = ',';
b[l+1] = '\0';
}
if(s[0] != ' ')
strcat(b, s);
timeb++;
}
i = j+1;
}
}
if(timea == 0)
printf("-\n");
else
{
printf("%s", a);
printf("%c\n", 34);
}
if(timeb == 0)
printf("-\n");
else
{
printf("%s", b);
printf("%c\n", 34);
}
}
return 0;
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given string s. Let's call word any
largest sequence of consecutive symbols without symbols ',' (comma) and ';'
(semicolon). For example, there are four words in string "aba,123;1a;0":
"aba", "123", "1a",
"0". A word can be empty: for example, the strings=";;"
contains three empty words separated by ';'.
You should find all words in the given string that are nonnegative INTEGER numbers without leading zeroes and build by them new string a.
String a should contain all words that
are numbers separating them by ',' (the order of numbers should remain the same as in the string s).
By all other words you should build string b in
the same way (the order of numbers should remain the same as in the strings).
Here strings "101", "0" are INTEGER numbers, but "01"
and "1.0" are not.
For example, for the string aba,123;1a;0 the string a would
be equal to "123,0" and string b would
be equal to "aba,1a".
Input
The only line of input contains the string s (1 ≤ |s| ≤ 105).
The string contains only symbols '.' (ASCII 46), ',' (ASCII
44), ';' (ASCII 59), digits, lowercase and uppercase latin letters.
Output
Print the string a to the first line and string b to
the second line. Each string should be surrounded by quotes (ASCII 34).
If there are no words that are numbers print dash (ASCII 45) on the first line. If all words are
numbers print dash on the second line.
Sample test(s)
input
aba,123;1a;0
output
"123,0" "aba,1a"
input
1;;01,a0,
output
"1" ",01,a0,"
input
1
output
"1" -
input
a
output
- "a"
Note
In the second example the string s contains five words: "1",
"", "01", "a0", "".
好弱。。。WA三次才过。
题意:给定一个字符串,字符';' 和 ','将串分开,每部分是一个单词,若出现中间为空的情况(连续两个分隔符),则认为中间的单词是空格。若单词是整数(不能有前导0),则放在串a里面,反之放在串b里面。在a,b串中,单词用','分开。若无单词是整数,a串为"-",若无单词是非整数,b串为"-"。最后输出a串和b串,如a串(b串)不是"-",输出时用""括起。
先填上空格,直接模拟就可以了。
AC代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define INF 0x3f3f3f
#define eps 1e-8
#define MAXN (100000+10)
#define MAXM (100000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1#define rr o<<1|1using namespace std;
char s[MAXN], str[MAXN*2];
char a[MAXN*2], b[MAXN*2];
bool judge(char op){
return op != ',' && op != ';';
}
bool check(char op){
return (op >= 'a' && op <= 'z') || (op >= 'A' && op <= 'Z') || (op == ' ') || (op == '.');
}
int main()
{
while(scanf("%s", s) != EOF)
{
int len = strlen(s), l;
if(len == 1 && (s[0] == ';' || s[0] == ','))
{
printf("-\n");
printf("%c,%c\n", 34, 34);
continue;
}
int top = 0;
for(int i = 0; i < len; i++)
{
if(i == 0)
{
if(s[i] == ',' || s[i] == ';')
{
str[top++] = ' ';
str[top++] = s[i];
if(s[i+1] == ',' || s[i+1] == ';')
str[top++] = ' ';
}
else
str[top++] = s[i];
}
else if(i == len-1)
{
if(s[i] == ',' || s[i] == ';')
{
str[top++] = s[i];
str[top++] = ' ';
}
else
str[top++] = s[i];
}
else
{
if((s[i] == ',' || s[i] == ';') && (s[i+1] == ',' || s[i+1] == ';'))
{
str[top++] = s[i];
str[top++] = ' ';
}
else
str[top++] = s[i];
}
}
str[top] = '\0';
a[0] = b[0]= '"'; a[1] = b[1] = '\0';
int timea = 0, timeb = 0;
for(int i = 0; i < top; )
{
if(judge(str[i]))
{
int j = i; int k = 0; bool flag = true;
while(j < top && judge(str[j]))
{
if(check(str[j]))
flag = false;
s[k++] = str[j++];
}
s[k] = '\0';
if(s[0] == '0' && k > 1)
flag = false;
if(flag)
{
if(timea)
{
l = strlen(a);
a[l] = ',';
a[l+1] = '\0';
}
if(s[0] != ' ')
strcat(a, s);
timea++;
}
else
{
if(timeb)
{
l = strlen(b);
b[l] = ',';
b[l+1] = '\0';
}
if(s[0] != ' ')
strcat(b, s);
timeb++;
}
i = j+1;
}
}
if(timea == 0)
printf("-\n");
else
{
printf("%s", a);
printf("%c\n", 34);
}
if(timeb == 0)
printf("-\n");
else
{
printf("%s", b);
printf("%c\n", 34);
}
}
return 0;
}
相关文章推荐
- 【排序相关】选择排序、插入排序、冒泡排序
- 批处理中的&、&&、|、||、>、>>符号
- 感受、情感、体验是私有的
- iframe对文档加载的影响
- Oracle VM VirtualBox网络说明
- 多个Jdk版本(转)
- 第二百四十一天 how can I 坚持
- 对称加密算法 之 RC4流密码
- 链表
- test page
- 关联容器
- Oracle11gR2 单机环境 GI PSU补丁安装
- LeetCode -- Maximum Subarray
- Heap与stack的差别
- 打开图片出现com surrogate 已停止工作
- HDU-新生赛-油菜花王国【并查集】
- 应届生,你为什么那么想做产品经理
- Bootstrap表单介绍
- hdfs详解
- Perl相关,未完成,自己看