输入包括一个由字母和数字组成的字符串,其长度不超过100。
可能有多组测试数据,对于每组数据, 按照样例输出的格式将字符出现的位置标出。 1、下标从0开始。 2、相同的字母在一行表示出其出现过的位置。
abcaaAB12ab12
a:0,a:3,a:4,a:9 b:1,b:10 1:7,1:11 2:8,2:12
HashMap<String, String> map = new HashMap<String, String>(); LinkedHashMap<String,String> linkedmap = new LinkedHashMap<String,String>(); for(int i= 0;i<str.length();i++){ String s = str.charAt(i)+""; if(map.containsKey(s)){ String v = map.get(s); map.remove(s); map.put(s, v+ s+":"+(i)+","); }else{ map.put(s, s+":"+(i)+","); linkedmap.put(s,""); } } Iterator iter = linkedmap.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); Object key = entry.getKey(); String v = map.get(key); String[] t = v.split(","); if(t.length==1){ continue; } System.out.println(v.substring(0,v.length()-1)); }
//java import java.util.*; public class Main { public static void main(String[] args){ Scanner input = new Scanner(System.in); String str = input.nextLine(); Map<Character, List<Integer>> map = new LinkedHashMap<>(); for (int i = 0; i < str.length(); i++) { List<Integer> list; if (!map.containsKey(str.charAt(i))) { list = new ArrayList<>(); list.add(i); map.put(str.charAt(i), list); } else { list = map.get(str.charAt(i)); list.add(i); } } for (Map.Entry<Character, List<Integer>> entry: map.entrySet()) { List<Integer> list = entry.getValue(); if (list != null && list.size() > 1) { for (int i = 0; i < list.size(); i++) { System.out.printf("%c:%d", entry.getKey(), list.get(i)); if (i != list.size() - 1) { System.out.print(","); } } System.out.println(); } } } }
// c++ #include<bits/stdc++.h> using namespace std; int main() { vector<pair<char, vector<int>>> m; string str; cin >> str; vector<pair<char, vector<int>>>::iterator it; for(int i = 0; i < str.length(); i++) { for(int j = 0; j < m.size(); j++) { // find pair<char, vector<int>> p= m[j]; if(p.first == str[i]) { vector<int> v = p.second; v.push_back(i); p.second = v; m[j] = p; break; } } // not find pair<char, vector<int>> p; p.first = str[i]; vector<int> v; v.push_back(i); p.second = v; m.push_back(p); } for(int i = 0; i < m.size(); i++) { pair<char, vector<int>> p= m[i]; vector<int> v = p.second; if(v.size() > 1) { for(int j = 0; j < v.size(); j++) { printf("%c:%d", p.first, v[j]); if(j != v.size() - 1) { printf(","); } } cout << endl; } } return 0; }
#include "bits/stdc++.h"
using namespace std;
#pragma warning (disable:4996)
typedef long long int LL;
int main() {
string str;
multimap<char, int> mp;
while (cin >> str) {
mp.clear();
int n = str.length();
for (int i = 0; i < n; i++) {
mp.insert(pair<char, int>(str[i], i));
}
for (int i = 0; i < n; i++) {
char ch = str[i];
if (mp.count(ch) > 1) {
auto mit = mp.equal_range(ch);
for (auto it = mit.first; it != mit.second; it++) {
if (it != mit.first) cout << ',';
cout << ch << ':' << it->second;
}
mp.erase(ch);
cout << endl;
}
}
}
return 0;
}
#include <iostream> #include <cstring> using namespace std; int main() { char str[100] = {}; char restr[123] = {}; while (cin >> str) { int len=strlen(str); for (int i=0; i<len; i++) restr[int(str[i])] = restr[int(str[i])] + 1; for (int i=0; i<len; i++) { if (int(restr[int(str[i])]) > 1) { for (int j=i; j<len; j++) { if (str[i] == str[j]) { cout << str[i] << ":" << j; restr[int(str[i])] = restr[int(str[i])] - 1; if (int(restr[int(str[i])]) > 0) cout << ","; } } cout << endl; } } for (int i=0; i<123; i++) restr[i] = 0; } return 0; }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan=new Scanner(System.in); StringBuffer sb = new StringBuffer(); int flag; while(scan.hasNext()){ String str=scan.nextLine(); int[] visited = new int[str.length()]; //已经重复过的就不用输出了 for(int i=0;i<str.length();i++){ flag = 0; for(int j=i+1;j<str.length();j++){ if( visited[j]==0){ if(flag==0 && str.charAt(i) ==str.charAt(j) ){ sb.append(str.charAt(j)+":"+i+","+str.charAt(j)+":"+j); flag=1; visited[j]=1; }else if(str.charAt(i) == str.charAt(j) && flag==1){ sb.append(","+str.charAt(j)+":"+j); visited[j]=1; } } } if(flag==1){ System.out.println(sb.toString()); sb.setLength(0);//将sb清空 } } } } }
#include <bits/stdc++.h> using namespace std; int main() { string s; while(cin>>s) { int visited[101]={0}; int len=s.length(); for(int i=0;i<len;++i) { bool first=true; visited[i]=1; for(int j=i+1;j<len;++j) { if(s[j]==s[i]&&visited[j]==0) { if(first) { cout<<s[i]<<":"<<i; first=false; } cout<<","<<s[j]<<":"<<j; visited[j]=1; } } if(!first)cout<<endl; } } return 0; } 不用vector
#include <iostream> using namespace std; int main(){ string str; cin >> str; int length=str.size(); bool visited[length]; for(int i=0; i<length; ++i){ visited[i]=false; } bool flag=false; for(int i=0; i<length; ++i){ if(!visited[i]){ flag=false; for(int j=i+1; j<length; ++j){ if(str[i]==str[j]){ flag=true; break; } } if(flag){ cout << str[i] << ":" << i << ","; for(int j=i+1; j<length; ++j){ if(str[i]==str[j]){ cout << str[j] << ":" << j << ","; visited[j]=true; } } cout << "\b"<<" "; cout << endl; } } } return 0; }
题目不难,但如何按照要求格式输出确实还是要绕一点弯子的
#include #include #include // 因为要是用字符有关函数strlen() using namespace std; const int maxN = 100; char str[maxN]; int flag[maxN]; //使用标记(打印标记) int main(){ bool isBlank; //用于判断是否输出空格 while(scanf("%s", str) != EOF){ for(int i = 0; i < strlen(str); i++){ isBlank = false; //每个新字母比较的开始, 都置为false if(flag[i]) continue;//1、用于判断若该字母在字符串中会重复出现,那么若之前是否已经被查出过之前是否已被 //2、注意区分break 与 continue, break直接跳出循环,而continue是结束本次循环,开次下次循环 else{ for(int j = i+1; j < strlen(str); j++){ if(str[i] == str[j]){ isBlank = true; //当有重复出现的字母时,需要输出空格 if(!flag[i]) printf("%c:%d", str[i], i); //是否已经打印过首元,同时控制了何时打印逗号 flag[i] = 1; printf(",%c:%d", str[j], j); flag[j] = 1; //打印标记(使用标记) } } if(isBlank) printf("\n"); //打印换行 } } } return 0; }
//祝我成功!!! #include<cstring> (803)#include<vector> #include<iostream> (720)#include<unordered_map> using namespace std; int main() { char c; int count=0; string keys; unordered_map<char, vector<int>> um; while(cin>>c) { if(um.find(c)==um.end()) keys+=c; um[c].emplace_back(count++); } for(char c:keys){ int size=um[c].size(); if(size>1) { for(int i=0; i<size; ++i) { cout<<c<<":"<<um[c][i]; if(i!=size-1) cout<<','; else cout<<'\n'; } } } }
#include<string.h> (845)#include<stdio.h> #include<stdlib.h> int main(){ char s[100]; int a[128];//对ASCII打表 memset(a,0,sizeof(a)); scanf("%s",s); //打表 for(int i=0;s[i]!='\0';i++){ a[s[i]]++; } //对字符串每一个字符检测,重复了就进行输出该字符在字符串中的所有位置 for(int i=0;s[i]!='\0';i++){ //判断是否重复 if(a[s[i]]>1){ char c=s[i]; //将重复的字符依次输出其位置,注意格式控制 for(int j=0;s[j]!='\0';j++){ if(s[j]==c){ if(a[s[j]]>1){ printf("%c:%d,",s[j],j); a[s[j]]--; continue; } if(a[s[j]]==1){ printf("%c:%d\n",s[j],j); a[s[j]]--; } } } } } }
#include <bits/stdc++.h> using namespace std; int main(){ string str; map<char,int>visited; while(cin>>str){ for(int i=0;i<str.size();i++){ if(!visited[str[i]]&&count(str.begin(),str.end(),str[i])>1){ visited[str[i]]=1; cout<<str[i]<<":"<<i; int j=i; while(str.find(str[i],j+1)!=string::npos){ j=str.find(str[i],j+1); cout<<","<<str[i]<<":"<<j; } cout<<endl; } } } return 0; }
#include<iostream> (720)#include<map> #include<vector> (721)#include<string> using namespace std; int main(){ string s; while(getline(cin,s)){ map<char,int> mp; map<char,vector<int> > mmap; map<char,int> is_cout; int len=s.size(); for(int i=0;i<len;i++){ //先记录所有字符的次数 mp[s[i]]++; } for(int i=0;i<len;i++){ //遍历字符串,对于所有次数大于1次的字符,令它对应的vector容器加入出现位置,即下标i if(mp[s[i]]>1){ mmap[s[i]].push_back(i); } } for(int i=0;i<len;i++){ if(mp[s[i]]>1 && is_cout[s[i]]==0){ //如果次数大于1次并且没有被输出过,那么就输出这个字符对应的vector的所有值,并且令这个字符已输出 is_cout[s[i]]=1; for(int j=0;j<mmap[s[i]].size();j++){ cout << s[i] << ":" << mmap[s[i]][j]; if(j<mmap[s[i]].size()-1) cout << ","; else cout << endl; } } } } return 0; }
import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){ char[] array = scanner.nextLine().toCharArray(); LinkedHashMap<Character, ArrayList<Integer>> map = new LinkedHashMap<>(); for (int i = 0; i < array.length; i++) { if (map.get(array[i])==null){ ArrayList<Integer> list = new ArrayList<>(); list.add(i); map.put(array[i],list); }else { ArrayList<Integer> list = map.get(array[i]); list.add(i); } } for (Map.Entry<Character, ArrayList<Integer>> entry : map.entrySet()) { ArrayList<Integer> value = entry.getValue(); if (value.size()>1){ Character key = entry.getKey(); for (int i = 0; i < value.size() - 1; i++) System.out.print(key+":"+value.get(i)+","); System.out.println(key+":"+value.get(value.size()-1)); } } } } }
#include <stdio.h> #include <stdlib.h> int k = 0; void pri(int flag[], char s[],int i, int j) { if(flag[i] != 1) { if(k != 0) printf("\n"); printf("%c:%d", s[i], i); k = 1; } printf(",%c:%d", s[i], j); } int main() { char s[100]; int flag[100] = {0}; scanf("%s", s); int len = strlen(s); for(int i = 0; i < len; i++) { if(flag[i]) continue; for(int j = i+1; j < len; j++) { if(s[i] == s[j]) { pri(flag, s, i, j); flag[i] = 1; flag[j] = 1; } } } return 0; }
//注意最后一个重复的字符输出后面没有',';没有重复的字符不要输出 #include <iostream> #include <vector> #include <string> using namespace std; int main(){ string str; while(cin>>str){ for(string::size_type i=0;i<str.size();++i){ char c=str[i]; if(i==str.find(c)){ //如果没有输出过,就查找并输出 string::size_type pos=str.find(c); if(str.find(c,pos+1)!=string::npos){ while((pos=str.find(c,pos))!=string::npos){ if(str.find(c,pos+1)==string::npos) //要到最后一个时改变输出格式 cout<<c<<':'<<pos<<endl; else cout<<c<<':'<<pos<<','; ++pos; } } } } } return 0; }
//使用哈希表和集合实现 #include <iostream> #include <map> #include <set> #include <string> using namespace std; int main() { string s; while (cin >> s) { map <char, set<int>> m; //采用{字符:{集合},}形式保存 set <char> has_printed; //保存已经输出的字符 for(int i = 0; i < s.size(); i++) { m[s[i]].insert(i); } for (int i = 0; i < s.size(); i++) { if(has_printed.find(s[i]) == has_printed.end() && m[s[i]].size() != 1) //若在已经输出的元素集合中没找到字符,则输出字符,并将字符写入到集合中 { for(set <int> :: iterator iter = m[s[i]].begin(); iter != m[s[i]].end(); iter++) { if(iter != m[s[i]].begin()) cout << ","; cout << s[i] << ":" << *iter; } has_printed.insert(s[i]); cout << endl; } } } }
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<cstring> #include<iostream> using namespace std; int main() { char a[101]; int n = 0; while (cin >> a) { n = strlen(a); //b数组按顺序存放字符串中重复出现过的元素 char b[101]; int b_size = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (a[i] == a[j]) { if (b_size == 0) { b[0] = a[i]; b_size++; } else { for (int k = 0; k < b_size; k++) { if (a[i] == b[k]) { break; } if (k == b_size - 1) { b_size++; b[b_size - 1] = a[i]; } } } } } } for (int k = 0; k <= b_size; k++) { int first = 0; int second = 0; for (int i = 0; i < n; i++) { //注意此处顺讯,十分关键 if (b[k] == a[i] && first != 0) { cout << ',' << b[k] << ':' << i; } if (b[k] == a[i] && first == 0) { cout << b[k] << ':' << i; first++; } } cout << endl; } getchar(); getchar(); getchar(); return 0; } }