hdu1800(哈希或map)

2015-01-24 05:42:45 · 作者: · 浏览: 3

题意大概就是求一些重复出现的字符串中最大的次数,用map可以水过,但是时间比较慢,rp不好就会TLE,看服务器洛,正好学了了哈希,用哈希搞是绝对不会超时的

写贴个map写法,998ms,760,670还TLE了一次,也是醉了

#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         #include 
         using namespace std; int main() { #ifdef xxz freopen("in.txt","r",stdin); #endif int n; while(cin>>n) { map
         
           mp; for(int i = 0; i < n; i++) { string a; cin>>a; for(string::iterator its = a.begin(); *its == '0'; a.erase(its) );//要把前导0去掉,不然得到的哈希值不同 mp[a]++; } map
          
           ::iterator it; int sum = 0; for(it = mp.begin(); it != mp.end(); it++) { sum = max(sum,it->second); } cout<
           
            


然后hash,202ms过掉

#include
             
              
#include
              
                #include
               
                 #include
                
                  #include
                 
                   #include
                  
                    using namespace std; int N; int Hash[3010]; char str[40]; int BKDRHash(char* s) { long long seed=131; long long hash=0; while(*s=='0')s++;//这点很重要,因为0000345645和34564是一个数,但是如果不处理的话会对应到不同的Hash值 while(*s) { hash=hash*seed+(*s++); } return (hash & 0x7FFFFFFF); } int main() { int i,ans; while(scanf("%d",&N)!=EOF) { i=0,ans=1; for(i=0;i
                   
                    ans)ans=temp; } else temp=1; } printf("%d\n",ans); } return 0; }