Description
给定若干个double类型变量的声明语句,要求从中分离出变量名,并按是否有重定义,分成两部分,按字典序输出
Input
若干行double类型变量的声明语句(每行的字符数不超过128)
Output
首先按字典序输出未重定义的变量名,每行一个
其次按字典序输出产生重定义的变量名,每行除了输出变量名外,还要输出变量名在整个声明语句中出现的次数
Sample Input
double a,b,c,aa,ab=0,ac_a2; double A[256]={1,2,5},B[1024][1024]={0},*C;
double aa , Aa, _a; double aa,aa; double b;
double *ac;
Sample Output
A
Aa
B
C
_a
a
ab
ac
ac_a2
c
aa 4
b 2
Hint
在所有测试数据中,没有Simple Input中不包含的情况,
例如,不需要考虑double a=1.0+2.0*3此类情况
即分隔符集为 ,={}[]* .;
KEY:这题很明显是函数strtok()的运用;用这个函数这题也不是难的……;
Source:

#include<iostream>
#include<string>
#include<map>
#include<algorithm>
using namespace std;

struct node

...{
char str[20];
int num;
};

node a[100];
int n;
map<string,int> mp;

int ok(char str[])

...{
if( (str[0]>='a'&&str[0]<='z')||(str[0]>='A'&&str[0]<='Z')||str[0]=='_')
return 1;
else return 0;
}

void fun(char str[])

...{
char* step=",={}[]* .; ";
char *t;
t=strtok(str,step);
while(t!=NULL)

...{
int i;
if(strcmp(t,"double")&&ok(t))

...{
if(mp[t]==0)

...{
strcpy(a[++n].str,t);
a[n].num=1;
mp[t]=1;
}
else

...{
for(i=1;i<=n;i++)
if(!strcmp(a[i].str,t)) break;
a[i].num++;
}
}
t=strtok(NULL,step);
}
}

int compare1(node x,node y)

...{
if(x.num>y.num) return 0;
else return 1;
}

int compare2(node x,node y)

...{
if(strcmp(x.str,y.str)>0) return 0;
else return 1;
}

int main()

...{
// freopen("fjnu_1762.txt","r",stdin);
char str[300];
while(gets(str))

...{
fun(str);
}
sort(a+1,a+n+1,compare1);
int i;
for(i=1;i<=n;i++)
if(a[i].num>1) break;
sort(a+1,a+i,compare2);
sort(a+i,a+n+1,compare2);
for(i=1;i<=n;i++)

...{
cout<<a[i].str;
if(a[i].num>1) cout<<" "<<a[i].num;
cout<<endl;
}
return 0;
}






