poj 2945

accept

#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;

char ss[20005][25];
int num[20005];
int n,m;

int cmp(const void * a,const void * b)
{
    int i,ans;
    char *s1,*s2;
    s1=(char *)a;
    s2=(char *)b;
    for(i=0;i<m;i++)
    {
        ans=s1[i]-s2[i];
        if(ans==0)
           continue;
        else
           return ans;
    }
    return ans;
}

int main()
{
    int i,j,sum;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0)
           break;
        for(i=0;i<n;i++)
           scanf("%s",ss[i]);
        qsort(ss,n,sizeof(ss[0][0])*25,cmp);
        memset(num,0,sizeof(num));
        for(i=1,sum=0;i<=n;i++)
        {
            if(strcmp(ss[i],ss[i-1])==0)
               sum++;
            else
            {
                num[sum]++;
                sum=0;
            }
        }
        for(i=0;i<n;i++)
           printf("%d\n",num[i]);
    }
    return 0;
}

#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;

char ss[20005][25];
int num[20005];
int n,m;

int cmp(const void * a,const void * b)
{
int i,ans;
char *s1,*s2;
s1=(char *)a;
s2=(char *)b;
for(i=0;i<m;i++)
{
ans=s1[i]-s2[i];
if(ans==0)
continue;
else
return ans;
}
return ans;
}

int main()
{
int i,j,sum;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0)
break;
for(i=0;i<n;i++)
scanf("%s",ss[i]);
qsort(ss,n,sizeof(ss[0][0])*25,cmp);
memset(num,0,sizeof(num));
for(i=1,sum=0;i<=n;i++)
{
if(strcmp(ss[i],ss[i-1])==0)
sum++;
else
{
num[sum]++;
sum=0;
}
}
for(i=0;i<n;i++)
printf("%d\n",num[i]);
}
return 0;
}

Time Limit Exceeded===pku 2945

=Time Limit Exceeded *
*********************

#include <iostream>
#include <map>
#include <cstring>
#include <string>
using namespace std;
map<string,int> my;
int a[100005];
int main(int argc, char *argv[])
{
int n,m,i,j; string s;
while(cin>>n>>m)
{
for(i=0;i<n;i++)
{
cin>>s;
if(my.count(s)==0) my[s]=1;
else my[s]++;
}
memset(a,0,sizeof(a));
map<string,int> ::iterator it;
for(it=my.begin();it!=my.end();it++)
{
int x=(*it).second;
a[x]++;
}
for(i=1;i<=n;i++)
{
cout<<a[i]<<endl;
}
my.clear();
}
return 0;
}

*********************************************************************************************************************

************************************************************************************************************************

#include <algorithm>
using namespace std;
#define MAX 20000
int A[MAX],ans,C[MAX];
map <string,int> mp;
int ID(string s){
if(mp[s]>0)return mp[s]-1;
mp[s]=ans++;
return ans-2;
}
int main(){
string s;
int n,m;
while(scanf("%d%d",&n,&m)==2 && n && m){
ans=1;
mp.clear();
memset(A,0,sizeof(A));
for(int i=0;i<n;i++){
cin>>s;
A[ID(s)]++;
}
memset(C,0,sizeof(C));
for(int i=0;i<n;i++)
C[A[i]]++;
for(int i=1;i<=n;i++)
printf("%d\n",C[i]);
}
return 0;
}

poj 2945

时间: 2024-08-08 14:07:14

poj 2945的相关文章

poj 2945 Find the Clones trie树的简单应用

题意: 给n个长m的字符串,统计他们的出现频率,输出出现1次的有几种,出现2次的有几种...出现n次的有几种.n<=20000,m<=20. 分析: 也可以用排序,map水的,但还是写个trie树也不麻烦,trie树我觉得就是针对字符串的hash表,效率如果数据大点是比暴力解法高很多的,另外写的时候不小心把index定义成char,n<256完全没问题..调了一个小时也是醉了. 代码: //poj 2945 //sep9 #include <iostream> using n

poj 2945 Find the Clones (map+string,hash思维)

Find the Clones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7498   Accepted: 2780 Description Doubleville, a small town in Texas, was attacked by the aliens. They have abducted some of the residents and taken them to the a spaceship

POJ 2945 Find the Clones (Trie树)

Find the Clones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7140   Accepted: 2655 Description Doubleville, a small town in Texas, was attacked by the aliens. They have abducted some of the residents and taken them to the a spaceship

poj 2945 trie树统计字符串出现次数

用记录附加信息的val数组记录次数即可. PS:trie树还有种动态写法,使用指针和动态分配内存代替了连续的ch数组,更加节省内存. Reference:http://blog.csdn.net/architect19/article/details/8966247 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 #define maxnode

POJ 2945 Find the Clones Hash

题目大意:给出一些字符串,问其中n个一样的有多少. 思路:看discuss里各种神奇的方法啊,什么map啊,什么Trie啊.这题不是一眼Hash么..难道是我想错了? 任意hash方法将所有字符串hash然后排序,之后统计一下相同的有多少就行了,500+MS水过.. PS:明天就是NOIP我这么水真的好( CODE: #include <cstdio> #include <cstring> #include <iostream> #include <algorit

POJ 2945 Find the Clones 水

Find the Clones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7524   Accepted: 2789 Description Doubleville, a small town in Texas, was attacked by the aliens. They have abducted some of the residents and taken them to the a spaceship

字典树trie的学习与练习题

博客详解: http://www.cnblogs.com/huangxincheng/archive/2012/11/25/2788268.html http://eriol.iteye.com/blog/1166118 http://www.360doc.com/content/12/1116/15/9615799_248213540.shtml http://www.cnblogs.com/tanky_woo/archive/2010/09/24/1833717.html http://bl

POJ 1815 Friendship

Friendship Time Limit: 2000MS   Memory Limit: 20000K Total Submissions: 10614   Accepted: 2945 Description In modern society, each person has his own friends. Since all the people are very busy, they communicate with each other only by phone. You can

POJ - 3186 Treats for the Cows (区间DP)

题目链接:http://poj.org/problem?id=3186 题意:给定一组序列,取n次,每次可以取序列最前面的数或最后面的数,第n次出来就乘n,然后求和的最大值. 题解:用dp[i][j]表示i~j区间和的最大值,然后根据这个状态可以从删前和删后转移过来,推出状态转移方程: dp[i][j]=max(dp[i+1][j]+value[i]*k,dp[i][j-1]+value[j]*k) 1 #include <iostream> 2 #include <algorithm&