题目1199:找位置
时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:2645
解决:1286
- 题目描述:
-
对给定的一个字符串,找出有重复的字符,并给出其位置,如:abcaaAB12ab12
输出:a,1;a,4;a,5;a,10,b,2;b,11,1,8;1,12, 2,9;2,13。
- 输入:
-
输入包括一个由字母和数字组成的字符串,其长度不超过100。
- 输出:
-
可能有多组测试数据,对于每组数据,
按照样例输出的格式将字符出现的位置标出。
- 样例输入:
-
abcaaAB12ab12
- 样例输出:
-
a:0,a:3,a:4,a:9 b:1,b:10 1:7,1:11 2:8,2:12
- 提示:
-
1、下标从0开始。
2、相同的字母在一行表示出其出现过的位置。
不想多说,考试时没时间来想,我用了三个数组,一个用来存字母的位置,一个用来存字母出现的次数,一个是用来在输出时判断是否在前面已经输出过该字母位置(做标记的)
#include<iostream> #include<cstdio> #include<cmath> #include<map> #include<cstring> using namespace std; int main() { char str[105]; while(~scanf("%s",str)) { int a[205][105],b[105],p=0,flag[105]; memset(b,0,sizeof(b)); memset(flag,0,sizeof(flag)); for(int i=0;i<strlen(str);i++) { a[str[i]-'0'][b[str[i]-'0']++] = i;//a数组记录字母每次出现的位置,b数组记录出现过的次数 } for(int i=0;i<strlen(str);i++) { if(b[str[i]-'0']>=2&&flag[str[i]-'0']==0) { flag[str[i]-'0']=1;//此时该字母的位置已经输出,标记数组记为1,下次不会再进入此循环 for(int j=0;j<b[str[i]-'0'];j++) { if(j!=b[str[i]-'0']-1) printf("%c:%d,",str[i],a[str[i]-'0'][j]); else printf("%c:%d ",str[i],a[str[i]-'0'][j]); } } } } return 0; }