时间限制:1秒
空间限制:65536K
题目描述
对给定的一个字符串,找出有重复的字符,并给出其位置,如:abcaaAB12ab12 输出:a,1;a,4;a,5;a,10,b,2;b,11,1,8;1,12, 2,9;2,13。
输入描述:
输入包括一个由字母和数字组成的字符串,其长度不超过100。
输出描述:
可能有多组测试数据,对于每组数据, 按照样例输出的格式将字符出现的位置标出。 1、下标从0开始。 2、相同的字母在一行表示出其出现过的位置。
示例1
输入
abcaaAB12ab12
输出
a:0,a:3,a:4,a:9
b:1,b:10
1:7,1:11
2:8,2:12
#include <stdio.h>
#include <string.h>
#define N 105
int cnt[63][N]; //记录下标
char ch[N];
int GetValue(int i){
int tmp;
if(ch[i]>='a' && ch[i]<='z'){
tmp = ch[i]-'a'+10;
}
else if(ch[i]>='A' && ch[i]<='Z'){
tmp = ch[i]-'A'+36;
}
else{
tmp = ch[i]-'0';
}
return tmp;
}
void Solve(){
memset(cnt,0,sizeof(cnt));
int tmp;
for(int i=0;ch[i];i++){
tmp = GetValue(i);
cnt[tmp][++cnt[tmp][0]] = i;
}
}
void Print(){
int tmp;
for(int i=0;ch[i];i++){
tmp = GetValue(i);
if(cnt[tmp][0]>=2){
for(int j=1;j<=cnt[tmp][0];j++){
printf("%c:%d",ch[i],cnt[tmp][j]);
if(j<cnt[tmp][0]) printf(",");
else printf("\n");
}
cnt[tmp][0] = 0; //标记为已经输出
}
}
}
int main(){
while(~scanf("%s",ch)){
Solve();
Print();
}
return 0;
}
题目链接:牛客网-计算机考研机试