TÓPICO
PROBLEM 1255 - URI Fórum 1.0
Este tópico foi resolvido e não pode receber novas respostas.
-
maragao respondido 8 years ago
Já testei muitas entradas e as respostas sempre batem, porém, tô recebendo sempre WA 100%
#include <iostream> #include <cctype> #include <string> #include <algorithm> #include <vector> #include <cstdio> using namespace std; typedef struct{ char letra; int num; }LETRA; bool ordena(LETRA a, LETRA b){ if(a.num > b.num) return true; else if(a.num == b.num) return true; return false; } bool alfa(LETRA a, LETRA b){ if(a.letra < b.letra) return true; return false; } int main(){ int qtde, tam, base; LETRA a; string in; vector<LETRA> l; scanf("%d", &qtde); getchar(); for(int i = 0; i < qtde; i++){ getline(cin, in); for(int j = 0; j < in.size(); j++){ if(isalpha(in[j]) == true){ in[j] = tolower(in[j]); }else if(isalpha(in[j]) == false){ in.erase(in.begin()+j); j--; } } sort(in.begin(), in.end()); base = 0; l.resize(1); l[0].letra = in[0]; l[0].num = 1; tam = in.size(); for(int j = 1; j < tam; j++){ if(l[base].letra == in[j]){ l[base].num += 1; }else{ a.letra = in[j]; a.num = 1; l.push_back(a); base++; } } sort(l.begin(), l.end(), ordena); for(int j = 1; j < l.size(); j++){ if(l[j-1].num != l[j].num) l.erase(l.begin()+j, l.end()); } sort(l.begin(), l.end(), alfa); //saida cout << l[0].letra; for(int j = 1; j < l.size(); j++){ if(l[j-1].num == l[j].num) cout << l[j].letra; else break; } cout << endl; } return 0; }
-
mleao1 respondido 9 years ago
Marcos, não vi erro no seu código. Testei e recebi accepted com essa pequena mudança (não sei se é ela a causa do erro), li uma vez antes do for (para pegar o lixo que está depois de 4 e que não foi lido).
-
gchiquini respondido 9 years ago
Algum de vocês pode apontar meu erro, por favor? todos os casos que já tentei funcionam e continuo com 40% W.A.
Resolvido!
-
erdoliveira4 respondido 7 years ago
For some reason my code don't count the first letter of the cases sometimes, but i no understand why / Por alguma rasão meu código não conta a primeira letra dos casos as vezes, mas eu nao entendo o motivo
Exemplo:
Code:
Resolvido, era um erro de variavel só
Input:
4 Input Computers account for only 5% of the country's commercial electricity consumption. Input frequency letters
expected output / Saida esperada:
inptu co inptu e
My output / Minha saida:
inptu o nptu e
-
Rychard respondido 8 years ago
Não consigo sacar o erro que tá acontecendo. Coloco em ordem alfabética, só os maiores, ignoro não letras... mas continua dando 20% de erro. Já coloquei vários casos de teste (inclusive os postados aqui) e não consegui sacar qual que é o erro. Alguém ajuda?
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String line; char letter; HashMap<Character, Integer> map = new HashMap<Character, Integer>(); int n = Integer.parseInt(scanner.nextLine()), max; for(int i = 0; i < n; i++){ map.clear(); max = 1; line = scanner.nextLine(); // Fill the map (key = character, value = frequency) for(int j = 0; j < line.length(); j++){ letter = line.charAt(j); if (Character.isLetter(letter)){ // Ignore non letters letter = Character.toLowerCase(letter); // Pass to lower case if(!map.containsKey(letter)){ map.put(letter, 1); } else { map.put(letter, map.get(letter) + 1); if (map.get(letter) > max) // Check the highest frequency max = map.get(letter); } } } // Get keys with values equals to max ArrayList<Character> letter_list = new ArrayList<Character>(); Iterator it = map.keySet().iterator(); while(it.hasNext()){ letter = (char) it.next(); if(map.get(letter) == max) letter_list.add(letter); } // Print the result for(Character c: letter_list){ System.out.print(c); } System.out.println(); } } }
-
mleao1 respondido 8 years ago
A função fflush não dá certo aqui. Coloca um espaço depois do %d que vai ser ignorada a quebra de linha (faz o que o fflush deveria fazer).
-
mleao1 respondido 8 years ago
Não sei se seu problema é esse, mas lembra que fgets armazena o \n no fim da frase, ou seja, tu precisa de mais dois espaços extras na string (um para o '\0' e um para o '\n'), acho que você não está tratando isso.
-
gdazevedo respondido 8 years ago
Não sei o que está errado, tentei vários casos testes (os do fórum e outros que eu fiz), mas mesmo assim está dando WA(100%)
#include <stdio.h> #include<string.h> #include<ctype.h> int main () { int N,repeticao[26], maior,k,g,l ; char frase[201]; scanf("%d", &N); fflush(stdin); for(int i=0; i<N; i++) { maior=0; for(k=0; k<27; k++) repeticao[k]=0; for(k=0; k<201; k++) frase[k]= '1'; scanf("%c", &frase[0]); for(g=0; l != -87; g++) { if (g>0) scanf("%c", &frase[g] ); if(frase[g]<91 && frase[g]>64) frase[g] = tolower(frase[g]); l = frase[g]-'a'; if(frase[g] < 123 && frase[g] > 96) repeticao[l]++; } l=42; for(k=0; k<26; k++) if(maior < repeticao[k]) maior = repeticao[k]; for(k=0; k<26; k++) if(maior == repeticao[k] && maior != 0) printf("%c", k+'a'); } return 0; }
-
mleao1 respondido 9 years ago
Quando você lê o inteiro, fica um "\n" no fim da linha (lixo), e o seu próximo getline, ao invés de pegar a linha, pega esse lixo. Coloca um getline depois do cin >> inteiro que aí você resolve o problema do lixo.
-
lmontesuma respondido 9 years ago
Alguém poderia me dar uma dica? Meu programa passa nos testes do site e alguns outros que tentei, ainda assim ao submeter recebo WA 100%. Não faço ideia do que tem de errado.
#include <iostream> #include <string> using namespace std; int main() { int cases; cin >> cases; for (int casos = 0; casos < cases; casos++) { string a; getline(cin, a); int vect[26] = { 0 }; for (int i = 0; i < a.size(); i++) { if (isalpha(a[i])) { if (isupper(a[i])) { vect[a[i] - 65]++; } else { vect[a[i] - 97]++; } } } int higher = 0; for (int i = 0; i < 26; i++) { if (vect[i]>higher) higher = vect[i]; } for (int i = 0; i < 26; i++) { if (vect[i] == higher) cout << (char)(i + 97); } cout << endl; } }
-
mleao1 respondido 9 years ago
Sua saída para A!abb é a, e deveria ser ab. Veja que o carácter ! não é válido, mas equivale a (int)A - 32.
-
mleao1 respondido 9 years ago
if (Character.isAlphabetic(c))
Não tenho certeza se funciona, mas você já tentou substituir por Character.isLetter()?
-
mleao1 respondido 9 years ago
Alguns casos: 8 bbbaaaccc BBBaaa A abc.. 55a. abc def abcdefghijklmnopqrstuvxwyz a.
-
aluiz1 respondido 9 years ago
Poderiam me fornecer mais casos de teste? Estou recebendo 30% de erro,mas ão sei aonde =/
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; class PorLetra implements Comparator<Pair>{ @Override public int compare(Pair o1, Pair o2) { // TODO Auto-generated method stub return o1.letter-o2.letter; } } class Pair implements Comparable<Pair> { public static boolean hasEmpate=false; public char letter; public int count; public Pair(char letter, int count) { this.letter = letter; this.count= count; } @Override public boolean equals(Object obj) { // TODO Auto-generated method stub Pair p = (Pair) obj; return p.letter==letter; } @Override public int compareTo(Pair o) { // TODO Auto-generated method stub return count-o.count; } } public class Main { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out)); int n; n = Integer.valueOf(in.readLine()); for (int i=0;i<n;i++){ ArrayList<Pair> letrasRepetidas = new ArrayList<Pair>(); char[] frase = in.readLine().toLowerCase().toCharArray(); for (char c:frase){ if (Character.isAlphabetic(c)){ Pair p2 = new Pair(c,0); if (letrasRepetidas.contains(p2)) letrasRepetidas.get(letrasRepetidas.indexOf(p2)).count++; else letrasRepetidas.add(new Pair(c,0)); } } Collections.sort(letrasRepetidas); Collections.sort(letrasRepetidas,Collections.reverseOrder()); boolean isEmpate = letrasRepetidas.get(0).count == letrasRepetidas.get(letrasRepetidas.size()-1).count; if (isEmpate) Collections.sort(letrasRepetidas,new PorLetra()); int maior = letrasRepetidas.get(0).count; for (Pair p:letrasRepetidas){ if (p.count != maior) break; writer.write(p.letter); } writer.newLine(); } writer.flush(); } }
-
mdourado respondido 9 years ago
Já testei vários casos de teste e ainda não sei o que está errado no meu código.
#include <iostream> #include <string> #include <queue> #include <cctype> #include <algorithm> #include <vector> using namespace std; class Letra { public: char letra; int posicaoNoAlfabeto; int qtdRepetida; Letra(char letra, int posicaoNoAlfabeto){ this->letra = letra; this->posicaoNoAlfabeto = posicaoNoAlfabeto; this->qtdRepetida=0; } bool operator<(const Letra& letra) const { if(qtdRepetida == letra.qtdRepetida) return posicaoNoAlfabeto < letra.posicaoNoAlfabeto; return qtdRepetida > letra.qtdRepetida; } private: }; int main() { int n; cin >> n; for(int cont=0; cont<=n; cont++) { vector<Letra> letras; string line; getline(cin, line); for (unsigned int i = 0; i < 26; i++) { char caracter = 'a'+i; letras.push_back(Letra(caracter, i+1)); } for (unsigned int i = 0; i < line.size(); i++) { if((line[i]>='a' && line[i]<='z') || (line[i]>='A' && line[i]<='Z')) { line[i] = tolower(line[i]); } else { line.erase(line.begin()+i); i--; } } for(unsigned int i = 0; i < line.size(); i++) { if(line[i]>='a' && line[i]<='z') { int pos = (int) line[i]-'a'; letras[pos].qtdRepetida++; } } sort(letras.begin(), letras.end()); int maior = letras[0].qtdRepetida; for(unsigned int i = 0; i < line.size() && line.size()>0; i++) { if(line[i]>='a' && line[i]<='z' && letras[i].qtdRepetida == maior) cout << letras[i].letra ; } if(line.size()) cout << endl; } return 0; }
-
jhonatangcavalcanti respondido on fev. 16 2014
caramba, muita falta de atenção, modifiquei o programa e até melhorei o tempo dele, porém continuava o erro, já estava desesperado por essa bobeira kkk, muito obrigado, deu accepted agora!