n = int(input()) sp = [] c = 0 for i in range(n): b = input() sp.append(b) sp.sort() for i in sp: if sp.count(i) >= 2 and c == 0: print(i) c = 1 if sp.count(i) < 2: c = 0 Pascal var a,b: array[1..100] of string; i,j,k,n: integer; s: string; f: boolean; begin readln(n); for i:=1 to n do begin readln(s); f:=false; for j:=1 to n do if a[j] = s then f:=true; if f = false then a[i]:=s else begin f:=false; for j:=1 to n div 2 do if b[j] = s then f:=true; if f = false then b[i]:=s; end; end; for i:=1 to n div 2 do if b[i] <> '' then writeln(b[i]);
end. Hi, here's a very short and clear solution I came up with in a few seconds. I guess somebody'll like the trick used in it. #include <bits/stdc++.h> using namespace std; int n,i; string s; map<string,int>M; int main(){ //freopen("input.txt","r",stdin); cin>>n; for(i=1;i<=n;++i){ cin>>s; ++M[s]; if(M[s]>1)cout<<s<<endl,M[s]=-1e9; } return 0; } #include <bits/stdc++.h> using namespace std; string s; map<string,int>M; int main(){ while (getline(cin, s)){ ++M[s]; if(M[s]>1)cout<<s<<endl,M[s]=-1e9; } return 0; } This is shorter .... Get Ac, but wrong . Weak tests. Edited by author 12.09.2016 09:59 using System; using System.Collections.Generic; namespace CA_TimusRu { class Program1496 { static void Main(string[] args) { Dictionary<string, int> dict = new Dictionary<string, int>(); int n = int.Parse(Console.ReadLine()); for (int i = 0; i < n; i++) { string s = Console.ReadLine(); if (dict.ContainsKey(s)) dict[s]++; else dict.Add(s, 1); } foreach (var item in dict) { if (item.Value >= 2) Console.WriteLine(item.Key); } } } } Just read it Runtime error. Программа аварийно завершила работу. Возможные причины: ненулевой код возврата (ошибка «non-zero exit code»); деление на ноль (ошибка «division by zero»); бесконечная рекурсия (ошибка «stack overflow»); недостаточный размер массивов или обращение по недоступному адресу в памяти (ошибка «access violation»). Чтобы избежать переполнения стека, используйте специальные директивы, которые приведены в разделах, посвященных конкретным языкам программирования. I've read and understood where my mistake is. Edited by author 24.03.2016 00:57 #include <iostream> #include <string> #include <ctime> #include <cstdlib> using namespace std; int main(){ int n, active, s = 0; bool kek = true; cin >> n; string *str = new string[n]; for (int i = 0; i < n; i++) { cin >> str[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (str[i] == str[j] && i != j){ str[j] = "1"; s = 1; }
} if (s == 1 && str[i]!="1"){ cout << str[i] << endl; }
str[i] = "1"; s = 0;
} } If you can't solve this solution, it may help you. import java.util.Arrays; import java.util.Scanner; public class _1496 { public static void main(String[] args) { Scanner s = new Scanner(System.in); int amount = s.nextInt(); if (amount == 0) System.out.println("0"); else { String[] source = new String[amount]; String[] uniT = new String[amount]; int j = 0; int count = 0; for (int i = 0; i < amount; i++) { source[i] = s.nextLine(); } Arrays.sort(source); uniT[0] = source[0]; for (int i = 0; i < amount; i++) { if (source[i].equals(uniT[j]) != true) { j++; uniT[j] = source[i]; } } for (int i = 0; i <= j; i++) { count = 0; for (int k = 0; k < amount; k++) { if (source[k].equals(uniT[i])) { count++; } } if (count > 1) { System.out.println(uniT[i]); } } } } } You have to change Output in case of empty solution from "0" to Empty string. It will solve your problem var a:array[1..101] of string; b:array[1..101] of integer; k,n,i,j,t,max,p:integer; x:string; begin k:=1; readln(n); readln(x); a[1]:=x; for i:=2 to n do begin readln(x); for j:=1 to k do if x=a[j] then b[j]:=b[j]+1; for j:=1 to k do if x<>a[j] then a[k+1]:=x; k:=k+1; end; for t:=1 to n do if b[t]>=max then max:=b[t]; for p:=1 to n do if b[p]=max then writeln(a[p]); end. https://uzdisk.uz/public.php?service=files&token=dacc643443b8fa3475306cd6b57a9277a6d354a5&file=/acm.timus.ru/1496.%20%D0%A1%D0%BF%D0%B0%D0%BC%D0%B5%D1%80.cpp #include <iostream> #include <string> #include <algorithm> using namespace std; int n; string array[10001]; struct TSpamer { string name; short index; }Spamer[10001]; void my_sort(int j) { int ind = 0; int Min_element = Spamer[ind].index; for(int i=0; i<j; i++) { int l = 0; while(l < j) { if(Spamer[l].index <= Min_element) { Min_element = Spamer[l].index; ind = l; } l++; } cout << Spamer[ind].name << endl; Min_element = 101; Spamer[ind].index = 102; } return; } int main(void) { int counter = 1; cin >> n; for(int i=0; i<n; i++) cin >> array[i]; sort(array, array+n); int k = 0, j = 0; while(k < n-1) { for(int i=k+1; i<n; i++) if(array[k] == array[i]) counter++; if(counter > 1) { Spamer[j].index = counter; Spamer[j].name = array[k]; j++; } k += counter; counter = 1; } my_sort(j); return 0; } Edited by author 16.10.2013 20:53 check my sol :P #include <iostream> #include <set> #include <string> using namespace std; int main () { string a[103]; set<string> gh; int n; cin >> n; for(int i = 0; i < n; i++)cin >> a[i]; for(int i = 0; i < n - 1; i++){ for(int j = i + 1; j < n; j++) if(a[i] == a[j]){gh.insert(a[i]);} } while (!gh.empty()){ cout << *gh.begin() << "\n"; gh.erase(gh.begin());} } Edited by author 20.02.2015 15:18 First: When N is 0 exit the program and print nothing. Second: get attention in "range" of Array or vector (or what you use to save the team name) test 3: N = 0; test 5: N = any, but output is nothing. Hope it help you! STL map rulz for this probl Edited by author 14.02.2008 20:29 Edited by author 14.02.2008 20:29 I think this problem can be solved easy without STL ;) Yes but STL map<string,int> is really great for string counting problems like this, and leads to short, clear solutions. Code: #include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) {int spammers; char peeps[33][101]; bool k=true; cin>>spammers; for(int i=0; i<=spammers; i++) { peeps[31][i]=0; peeps[32][i]=0; for(int g=0; peeps[g-1][i]!='\n'; g++) { cin.get(peeps[g][i]); peeps[31][i]+=peeps[g][i]; peeps[32][i]++; } } for(int i=0; i<=spammers-1; i++) { for(int g=i+1; g<=spammers; g++) { if(peeps[31][i]==peeps[31][g] && peeps[32][i]==peeps[32][g] && peeps[31][g]!=-1) { if(k==true) { peeps[31][g]=-1; for(int y=0; peeps[y-1][g]!='\n'; y++) { cout<<peeps[y][g]; } k=false; } else { peeps[31][g]=-1; } } } k=true; } system("PAUSE"); return EXIT_SUCCESS; } Any ideas why it crashes? Sorry, your code is kind of too obfuscated for lazy me, but I would believe that you've really got access violation in your program. If you're curious, replace all arrays with vectors and use range checking access via "at". 0 Edited by author 12.01.2012 22:05 var n,i,j:byte; s:array[1..100]of string[31]; begin readln(n); for i:=1 to n do begin readln(s[i]); for j:=1 to i-1 do if s[i]=s[j] then writeln(s[i]); end; end. ______________________________ HELP PLZ try test from example )))) your program writes something like this abikbaev abikbaev abikbaev x it is just fun) Edited by author 23.11.2007 00:51 Please, can you give accepted code? I did not understand this program. #include <iostream> #include <string> using namespace std; int main() { int N, counter = 0, p, g = 0; cin >> N; const int max = 128; char ar[max][32] = {0}; char temp[max][32]; for ( int j = 0; j < N; ++j ) cin >> ar[j]; cout << endl; for ( int j = 0; j < N - 1; ++j ) for ( int i = j + 1; i < N; ++i ) if ( strcmp ( ar[j], ar[i] ) == 0 ) { if ( counter > 0 ) { for ( p = 1; p <= counter; ++p ) { if ( strcmp ( ar[j], temp[p] ) == 0 ) ++g; } if ( g == 0 ) { strcpy ( temp[++counter], ar[j] ); g = 0; break; } } else { strcpy ( temp[++counter], ar[j] ); break; } break; } else continue; for ( int j = 1; j <= counter; ++j ) cout << temp[j] << endl; return 0; } Wrong answer. Test №7 Why ? Edited by author 20.08.2011 02:09 Same. Give test, please. My code [C#]: using System; using System.Text; using System.Collections.Generic; namespace _1820 { class Program { static void Main(string[] args) { List<string> spam = new List<string>(); List<string> good = new List<string>(); int n = Int32.Parse(Console.ReadLine()); if (n <= 100) { int i, j, k; bool find = false; for (i = 0; i < n; i++) { spam.Add(Console.ReadLine()); } for (i = 0; i < spam.Count; i++) { if (spam[i].Length <= 30) { for (j = i + 1; j < spam.Count; j++) { find = true; if (spam[i] == spam[j]) { if (good.Count > 0) { for (k = 0; k < good.Count; k++) { if (good[k] == spam[j]) break; find = false; } if (!find) good.Add(spam[j]); } else { good.Add(spam[i]); break; } break; } } } } for (i = 0; i < good.Count; i++) { Console.WriteLine(good[i]); }
} } } } The solution is pretty straightforward. There're two primary reasons why you get WA with Java code: 1) you tokenize read strings instead of reading them "as is", they might contain numbers; 2) you forget that the input might be just 0 - program must output nothing then. Thanks buddy :) you saved my day with your tip #2 :) Thanks a lot... program qwerty; var a,b,c:array [1..200] of string; n,i,j,m,k,h,x:integer; begin m:=0; readln (n); for i:=1 to n do readln (a[i]); for i:=1 to n-1 do for j:=I+1 to n do if a[i]=a[j] then begin m:=m+1; b[m]:=a[i]; end; c[1]:=b[1]; k:=1; for i:=1 to m do if b[i]<>c[k] then begin h:=0; for x:=1 to k do if b[i]=c[x] then h:=1; If h<>1 then begin k:=k+1; c[k]:=b[i]; end; end; for i:=1 to k do writeln (c[i]); end. This is my code. I don't know why crash. Help me please. Hello! This is listing of my class import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class q1496 { /** * @param args * @throws IOException * @throws NumberFormatException */ public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int amount = Integer.parseInt(in.readLine()); String[] source= new String[amount]; String[] uniT= new String[amount]; int j=0; int count=0; for (int i=0;i<amount;i++) { source[i]=in.readLine(); } Arrays.sort(source); uniT[0]=source[0];
for (int i=0;i<amount;i++) { if (source[i].equals(uniT[j])!=true) { j++; uniT[j]=source[i]; } } for (int i=0;i<=j;i++) { count=0; for (int k=0;k<amount;k++) { if (source[k].equals(uniT[i])) { count++; } } if (count>1){System.out.println(uniT[i]);} } } } |
|