锘??xml version="1.0" encoding="utf-8" standalone="yes"?> 鏈鍒濈殑鎯蟲硶“鍏堥氳繃姝e父鐨勪簩鍒嗘煡鎵捐繑鍥炰竴涓綅緗畃os錛岀劧鍚庡湪浜屽垎鏌ユ壘0-pos錛屽鏋滄湁鍚堥傜殑newpos緇х畫0-newpos錛屼笉榪囨槸log2N + log2(N/2)+銆傘傘?#8221; 浼樺寲鍚?/p> 1 姝e父鐨勪簩鍒嗘煡鎵捐繑鍥炰竴涓綅緗畃os 錛屾鏃剁殑pos 鏄敱 left right 鐢熸垚 錛岃漿鍒扮浜屾 2 緇х畫璋冪敤 浜屽垎鎼滅儲 鍦?left 鍜?pos –1 涓煡鎵緆ey 錛?濡傛灉鎵懼埌 緇х畫絎簩姝ワ紝濡傛灉娌℃湁絎笁姝?/p> 3 姝ゆ椂鍙互璇存槑 left – pos –1 涓?涓嶅瓨鍦?key 錛岄偅涔堝彲浠ヨ pos 灝辨槸絎竴嬈″嚭鐜扮殑涓嬫爣 鎴戠嫭绔嬪崥瀹㈢殑銆鍘熸枃銆銆銆嬈㈣繋澶у璁塊棶錛屾壒璇勬寚姝o紱# include<stdio.h>
#define DEBUG
int bsearch(int array[],int &start ,int &end ,int key)
{
int i,j;
int mid;
i = start;
j = end ;
while(i <= j)
{
mid = (i+j)/2;
#ifdef DEBUG
// printf("now i j and mid is %d %d %d\n",i,j,mid);
#endif
if(array[mid] == key)
return mid;
else if(array[mid] < key)
i = mid + 1;
else
j = mid - 1;
}
start = i,end = mid;
return -1;
}
int FirstBsearch(int array[],int start ,int end ,int key)
{
int i,j;
int mid,pos;
i = start;
j = end ;
mid = bsearch(array,start ,end ,key);
if(array[mid] == key)//濡傛灉鎵懼埌浜?/span>
{
pos = mid;
while( pos != -1 )
{
j = pos -1;
pos = bsearch(array ,i ,j ,key);
}
return j+1;
}
return -1;
}
int main()
{
int data[11] = {1,3,4,7,7,7,7,56,134,134,132487990};
int key,i;
int start = 0,end =10;
for(i = 0 ; i < 4; i ++)
{
scanf("%d",&key);
printf("%d",FirstBsearch(data,start,end,key));
}
return 0;
}
]]>#ifndef THING_H_
#define THING_H_
#include <iostream>
#include <string>
using namespace std;
class Thing {
public:
Thing(int n) : m_Num(n) {
}
~Thing() {
cout << "destructor called: "
<< m_Num << endl;
}
private:
string m_String;
int m_Num;
};
#endif
榪愯緇撴灉
destructor called: 107
destructor called: 106
destructor called: 101
destructor called: 104
destructor called: 103
destructor called: 102
destructor called: 101
]]>
]]>C++ 榪愮畻絎︿紭鍏堢駭鍒楄〃
http://www.cppreference.com/operator_precedence.html
Precedence Operator Description Example Associativity
1
()
[]
->
.
::
++
--Grouping operator
Array access
Member access from a pointer
Member access from an object
Scoping operator
Post-increment
Post-decrement(a + b) / 4;
array[4] = 2;
ptr->age = 34;
obj.age = 34;
Class::age = 2;
for( i = 0; i < 10; i++ ) ...
for( i = 10; i > 0; i-- ) ...left to right
2
!
~
++
--
-
+
*
&
(type)
sizeofLogical negation
Bitwise complement
Pre-increment
Pre-decrement
Unary minus
Unary plus
Dereference
Address of
Cast to a given type
Return size in bytesif( !done ) ...
flags = ~flags;
for( i = 0; i < 10; ++i ) ...
for( i = 10; i > 0; --i ) ...
int i = -1;
int i = +1;
data = *ptr;
address = &obj;
int i = (int) floatNum;
int size = sizeof(floatNum);right to left
3
->*
.*Member pointer selector
Member pointer selectorptr->*var = 24;
obj.*var = 24;left to right
4
*
/
%Multiplication
Division
Modulusint i = 2 * 4;
float f = 10 / 3;
int rem = 4 % 3;left to right
5
+
-Addition
Subtractionint i = 2 + 3;
int i = 5 - 1;left to right
6
<<
>>Bitwise shift left
Bitwise shift rightint flags = 33 << 1;
int flags = 33 >> 1;left to right
7
<
<=
>
>=Comparison less-than
Comparison less-than-or-equal-to
Comparison greater-than
Comparison geater-than-or-equal-toif( i < 42 ) ...
if( i <= 42 ) ...
if( i > 42 ) ...
if( i >= 42 ) ...left to right
8
==
!=Comparison equal-to
Comparison not-equal-toif( i == 42 ) ...
if( i != 42 ) ...left to right
9
&
Bitwise AND
flags = flags & 42;
left to right
10
^
Bitwise exclusive OR
flags = flags ^ 42;
left to right
11
|
Bitwise inclusive (normal) OR
flags = flags | 42;
left to right
12
&&
Logical AND
if( conditionA && conditionB ) ...
left to right
13
||
Logical OR
if( conditionA || conditionB ) ...
left to right
14
? :
Ternary conditional (if-then-else)
int i = (a > b) ? a : b;
right to left
15
=
+=
-=
*=
/=
%=
&=
^=
|=
<<=
>>=Assignment operator
Increment and assign
Decrement and assign
Multiply and assign
Divide and assign
Modulo and assign
Bitwise AND and assign
Bitwise exclusive OR and assign
Bitwise inclusive (normal) OR and assign
Bitwise shift left and assign
Bitwise shift right and assignint a = b;
a += 3;
b -= 4;
a *= 5;
a /= 2;
a %= 3;
flags &= new_flags;
flags ^= new_flags;
flags |= new_flags;
flags <<= 2;
flags >>= 2;right to left
16
,
Sequential evaluation operator
for( i = 0, j = 0; i < 10; i++, j++ ) ...
left to right
]]>
#include<iostream>
#include<map>
#include<string>
using namespace std;
int main()
{
string ss,ss1;
int k,n,m,t=1;
cin>>n;
while (n--)
{
map< string,map<string,int> >mm;// 鍙互宓屽map
if (t>1) cout<<endl;
cin>>k;
while (k--)
{
cin>>ss1>>ss>>m;
mm[ss][ss1]+=m;
}
// map鍐呴儴鏄粯璁ゆ帓搴忕殑
map< string,map<string,int> >::iterator itr;
map< string,int > ::iterator itr1;
for (itr=mm.begin();itr!=mm.end();itr++)
{
cout<<(*itr).first<<endl;
for (itr1=(*itr).second.begin();itr1!=(*itr).second.end();itr1++)
cout<<" |----"<<(*itr1).first<<'('<<(*itr1).second<<')'<<endl;
}
t++;
}
return 0;
}
]]>

鐜板湪緇欎綘N涓崟璇嶅拰涓浜涙煡璇紝璇瘋緭鍑烘彁紺虹粨鏋滐紝涓轟簡綆鍖栬繖涓棶棰橈紝鍙渶瑕佽緭鍑轟互鏌ヨ璇嶄負鍓嶇紑鐨勫茍涓旀寜瀛楀吀搴忔帓鍒楃殑鏈鍓嶉潰鐨?涓崟璇嶏紝濡傛灉絎﹀悎瑕佹眰鐨勫崟璇嶄竴涓篃娌℃湁璇峰彧杈撳嚭褰撳墠鏌ヨ璇嶃?/dd>
鍏朵腑錛歂<=10000,Q<=1000010
a
ab
hello
that
those
dict
youdao
world
your
dictionary
6
bob
d
dict
dicti
yo
zbob
dict dictionary
dict dictionary
dictionary
youdao your
z
鐢ㄧ殑鏄痶rie 鏍?br>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
/*
*
*/
struct node{
int next[26];// 瀵逛簬鏌愪竴灞傝岃█ next銆恑銆?nbsp;涓璱灝辮〃紺鴻灞傛湁鐨勫瓧絎︿簡 next銆恑銆戠殑鍊兼寚鍚戜粬鎵鎸囧悜鐨勭粨鏋?/span>
int flag;// 鐢ㄦ潵鏍囪鑺傜偣鏈夋病鏈夎浣跨敤
}trie[210000];
char str[100];
char ans[100];
int totle=1;
void insert(){
int p=0;
int k=0;
while(str[k]){
int v=str[k]-'a';
if(trie[p].next[v]==-1)trie[p].next[v]=totle++;
p=trie[p].next[v];
k++;
}
trie[p].flag=1;
}
int cur;
void dfs(int k,int p)//姝ゆ爲鍦ㄧ粍緇囩殑鏃跺?nbsp;灝辨槸鎸夊瓧鍏告潵鎺掔殑
{
if(cur>=8)return;
if(trie[p].flag!=-1){
ans[k]=0;
if(cur==0)printf("%s",ans);
else printf(" %s",ans);
cur++;
}
for(int i=0;i<26;i++)
if(trie[p].next[i]!=-1){
ans[k]=i+'a';
dfs(k+1,trie[p].next[i]);
}
return;
}
void find(){
cur=0;
int p=0,k=0;
while(str[k]&&p!=-1)//姣斿 abc 鎸夋牴寮濮?nbsp;鎵懼埌鍖歸厤 c 絎笁灞傜殑 P
{
p=trie[p].next[str[k]-'a'];
ans[k]=str[k];
k++;
}
if(p==-1)// 娌℃湁鍖歸厤鐨?nbsp;閭d箞鐩存帴鎵撳嵃 鎸夐鎰忔潵
{
printf("%s\n",str);
return;
}
dfs(k,p);//緇х畫鎼?nbsp;str[k]涓瓧絎?/span>
printf("\n");
}
int main()
{
freopen("in.txt","r",stdin);
vector<string> my;
int n;
memset(trie,-1,sizeof(trie));
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%s",str);
insert();
}
int q;
scanf("%d",&q);
while(q--){
scanf("%s",str);
find();
}
return 0;
}
鍚屾椂榪樼湅鍒頒竴浣嶇墰浜?鐢╯tl 鍐欑殑 閭d釜鐗涘弶 涔熻創涓婁簡 浠ヤ緵鑷繁鍙傝?
string ts;
bool issub(const string c)
{
if (ts.length() > c.length()) return false;
return ts == c.substr(0, ts.length());//c瀛椾覆涓瀛樺湪ts 榪斿洖true
}
typedef vector<string> DIC;
int main()
{
DIC dict;
string str;
size_t dsize, ssize;
cin >> dsize;
dict.reserve(dsize);//紜繚dict 鐨勫閲忚嚦灝戜負dsize
for (size_t i = 0; i < dsize; ++i)
{
cin >> str;
dict.push_back(str);
}
std::sort(dict.begin(), dict.end());//鎺掑簭
dict.erase(std::unique(dict.begin(), dict.end()), dict.end());//鍘婚櫎閲嶅鐨?/span>
cin >> ssize;
for (size_t i = 0; i < ssize; ++i)
{
DIC::iterator iter;
cin >> ts;
bool found = false;
iter = lower_bound(dict.begin(), dict.end(),ts);
//姝ゅ嚱鏁板湪msdn 鐨勮В閲婁負 鍦╠ict 涓彃鍏s 鏈灝忕殑浣嶇疆騫剁淮鎸佸簭鍒楁湁搴?/span>
for(size_t j=0;j<8 && iter!=dict.end();++j,++iter)
{
if(issub(*iter)){
cout<<*iter<<" ";
found=true;
}
}
if (!found)
cout << ts;
cout << endl;
}
return 0;
}
]]>
#include<iostream>
#include<algorithm>
#include <functional>
using namespace std;
int data[1010];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i = 0; i < n; i ++)
data[i] = i+1;
sort(data,data+n);
m --;
while(m--)
next_permutation(data,data+n);
for( i = 0;i<n;i++)
printf(i ==0 ?"%d":" %d",data[i]);
printf("\n");
}
return 0;
}
]]>
#include<iostream>
#include<algorithm>
#include <vector>
#include <functional>
using namespace std;
const int maxn = 3000+10;
int data[maxn];
struct node
{
int x,y;
char name[20];
} re[maxn];
// bool cmp(const struct node a,const struct node b)
// {
// return (a).x - (b).x;
// }
class cmp{
public:
bool operator()(const node &a,const node &b)
{
//return a.x > b.x;
if(strcmp(a.name,b.name) > 0)
return true;
return false;
}
};
int main()
{
int N,M,i;
// vector<int > re(maxn*maxn/2);
while(scanf("%d%d",&N,&M)!=EOF)
{
for(i = 0; i < N; i ++)// 杈撳叆n涓暟瀛?/span>
scanf("%s",&re[i].name);
partial_sort(re,re+M,re+M,cmp());//鎺掑嚭鍓峂澶т釜鏁板瓧
for( i = 0; i < M; i ++)
if(i ==0) cout<<re[i].name;
else cout<<" "<<re[i].name;
// for( i = 0; i < M; i ++)
// if(i ==0) cout<<re[i].x;
// else cout<<" "<<re[i].x;
printf("\n");
}
return 0;
]]>
榪欓噷鍒欓渶瑕?6MS 鐩告瘮鍏堝墠鐨?00澶氱 鏄劇劧鏄揩浜嗗緢澶?銆傘傘?br>
#include<iostream>
#include<algorithm>
#include <vector>
#include <functional>
using namespace std;
const int maxn = 3000+10;
int data[maxn];
//int re[maxn*maxn/2];
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
int N,M,ans,i;
vector<int > re(maxn*maxn/2);
while(scanf("%d%d",&N,&M)!=EOF)
{
for(i = 0; i < N; i ++)
scanf("%d",&data[i]);
//sort(data,data+N,cmp);
ans = 0;
for( i = 0;i < N;i++)
for(int j = i+1;j<N;j++)
{
re[ans] = data[i]+data[j];
//re.p (data[i]+data[j]);
ans ++;
}
//for( vector<int>::iterator it1 = re.begin(); it1 != re.end() ; it1 ++)
// cout<<*it1<<" ";
//printf("\n");
//partial_sort(re.begin(),re.begin()+M,re.end());//鍥犱負鐢寵浜哸xn*maxn/2 絀洪棿 鎵浠ユ湁寰堝鏄?鍊?nbsp;
/* 榪欐槸瀵艱嚧鎺掑簭鍚?nbsp;鍑虹幇0 鍊肩殑鍘熷洜*/
partial_sort(re.begin(),re.begin()+M,re.begin()+ans,cmp);
for( vector<int>::iterator it = re.begin(); it != re.begin()+M&& it != re.end() ; it ++)
if(it ==re.begin()) cout<<*it;
else cout<<" "<<*it;
cout<<endl;// printf("\n");
}
return 0;
}
]]>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 3000+10;
int data[maxn];
int re[maxn*maxn/2];
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
int N,M,ans;
while(scanf("%d%d",&N,&M)!=EOF)
{
for(int i = 0; i < N; i ++)
scanf("%d",&data[i]);
//sort(data,data+N,cmp);
ans = 0;
for( i = 0;i < N;i++)
for(int j = i+1;j<N;j++)
{
re[ans++] = data[i]+data[j];
}
sort(re,re+N*(N-1)/2,cmp);
for( i = 0; i < M; i ++)
printf(i ==0 ?"%d":" %d",re[i]);
printf("\n");
}
return 0;
}
鍋欰CM棰樼殑鏃跺欙紝鎺掑簭鏄竴縐嶇粡甯歌鐢ㄥ埌鐨勬搷浣溿傚鏋滄瘡嬈¢兘鑷繁鍐欎釜鍐掓場涔嬬被鐨凮(n^2)鎺掑簭錛屼笉浣嗙▼搴忓鏄撹秴鏃訛紝鑰屼笖嫻垂瀹濊吹鐨勬瘮璧涙椂闂達紝榪樺緢鏈夊彲鑳藉啓閿欍係TL閲岄潰鏈変釜sort鍑芥暟錛屽彲浠ョ洿鎺ュ鏁扮粍鎺掑簭錛屽鏉傚害涓簄*log2(n)銆備嬌鐢ㄨ繖涓嚱鏁幫紝闇瑕佸寘鍚ご鏂囦歡銆?br> 榪欎釜鍑芥暟鍙互浼犱袱涓弬鏁版垨涓変釜鍙傛暟銆傜涓涓弬鏁版槸瑕佹帓搴忕殑鍖洪棿棣栧湴鍧錛岀浜屼釜鍙傛暟鏄尯闂村熬鍦板潃鐨勪笅涓鍦板潃銆備篃灝辨槸璇達紝鎺掑簭鐨勫尯闂存槸[a,b)銆傜畝鍗曟潵璇達紝鏈変竴涓暟緇刬nt a[100]錛岃瀵逛粠a[0]鍒癮[99]鐨勫厓绱犺繘琛屾帓搴忥紝鍙鍐檚ort(a,a+100)灝辮浜嗭紝榛樿鐨勬帓搴忔柟寮忔槸鍗囧簭銆?br> 鎷挎垜鍑虹殑“AC鐨勭瓥鐣?#8221;榪欓鏉ヨ錛岄渶瑕佸鏁扮粍t鐨勭0鍒發en-1鐨勫厓绱犳帓搴忥紝灝卞啓sort(t,t+len);
瀵瑰悜閲弙鎺掑簭涔熷樊涓嶅錛宻ort(v.begin(),v.end());
鎺掑簭鐨勬暟鎹被鍨嬩笉灞闄愪簬鏁存暟錛屽彧瑕佹槸瀹氫箟浜嗗皬浜庤繍綆楃殑綾誨瀷閮藉彲浠ワ紝姣斿瀛楃涓茬被string銆?br> 濡傛灉鏄病鏈夊畾涔夊皬浜庤繍綆楃殑鏁版嵁綾誨瀷錛屾垨鑰呮兂鏀瑰彉鎺掑簭鐨勯『搴忥紝灝辮鐢ㄥ埌絎笁鍙傛暟鈥斺旀瘮杈冨嚱鏁般傛瘮杈冨嚱鏁版槸涓涓嚜宸卞畾涔夌殑鍑芥暟錛岃繑鍥炲兼槸bool鍨嬶紝瀹冭瀹氫簡浠涔堟牱鐨勫叧緋繪墠鏄?#8220;灝忎簬”銆傛兂鎶婂垰鎵嶇殑鏁存暟鏁扮粍鎸夐檷搴忔帓鍒楋紝鍙互鍏堝畾涔変竴涓瘮杈冨嚱鏁癱mp
bool cmp(int a,int b)
{
return a>b;
}
鎺掑簭鐨勬椂鍊欏氨鍐檚ort(a,a+100,cmp);
鍋囪鑷繁瀹氫箟浜嗕竴涓粨鏋勪綋node
struct node{
int a;
int b;
double c;
}
鏈変竴涓猲ode綾誨瀷鐨勬暟緇刵ode arr[100]錛屾兂瀵瑰畠榪涜鎺掑簭錛氬厛鎸塧鍊煎崌搴忔帓鍒楋紝濡傛灉a鍊肩浉鍚岋紝鍐嶆寜b鍊奸檷搴忔帓鍒楋紝濡傛灉b榪樼浉鍚岋紝灝辨寜c闄嶅簭鎺掑垪銆傚氨鍙互鍐欒繖鏍蜂竴涓瘮杈冨嚱鏁幫細
浠ヤ笅鏄唬鐮佺墖孌碉細
bool cmp(node x,node y)
{
if(x.a!=y.a) return x.a
if(x.b!=y.b) return x.b>y.b;
return return x.c>y.c;
}
]]>