1. 程式人生 > >HDU-6208 The Dominator of Strings(查詢子串string)

HDU-6208 The Dominator of Strings(查詢子串string)

                                    The Dominator of Strings

                      Time Limit: 3000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
                                             Total Submission(s): 5188    Accepted Submission(s): 1718


Problem Description

Here you have a set of strings. A dominator is a string of the set dominating all strings else. The string S is dominated by T if S is a substring of T.

 Input

The input contains several test cases and the first line provides the total number of cases.
For each test case, the first line contains an integer N indicating the size of the set.
Each of the following N lines describes a string of the set in lowercase.
The total length of strings in each case has the limit of 100000.
The limit is 30MB for the input file.

 Output

For each test case, output a dominator if exist, or No if not.

 Sample Input

3
10
you
better
worse
richer
poorer
sickness
health
death
faithfulness
youbemyweddedwifebetterworsericherpoorersicknesshealthtilldeathdouspartandpledgeyoumyfaithfulness
5
abc
cde
abcde
abcde
bcde
3
aaaaa
aaaab
aaaac

Sample Output

youbemyweddedwifebetterworsericherpoorersicknesshealthtilldeathdouspartandpledgeyoumyfaithfulness
abcde
No

http://acm.hdu.edu.cn/showproblem.php?pid=6208

題意:

給你n個字串,問裡面是否有一個字串,它包含了全部的字串。也就是所有的字串都是這個字串的字串。有的話輸出該字串,沒有輸出No.

思路:

先找出最長的字串,那麼肯定是主串。可以用string中的find來查詢其他字串是否在該最長的字串中。有一種特殊情況,如果字串的長度相等,那麼只要有其中一個不相等,輸出No。

string.find的用法:

查詢字串a是否包含子串b,
不是用strA.find(strB) > 0而是strA.find(strB) != string:npos
string::size_type pos = strA.find(strB);

 ////find函式返回型別 size_type
string s("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i");
string flag;
string::size_type position;

//find 函式 返回jk 在s 中的下標位置 
position = s.find("jk");
 if (position != s.npos)  //如果沒找到,返回一個特別的標誌c++中用npos表示,我這裡npos取值是4294967295,
 {
  cout << "position is : " << position << endl;
 }
 else
 {
  cout << "Not found the flag" + flag;
 } 

//find 函式 返回flag 中任意字元 在s 中第一次出現的下標位置
 flag = "c";
 position = s.find_first_of(flag);
 cout << "s.find_first_of(flag) is : " << position << endl;

 //從字串s 下標5開始,查詢字串b ,返回b 在s 中的下標
 position=s.find("b",5);
 cout<<"s.find(b,5) is : "<<position<<endl;

//查詢s 中flag 出現的所有位置。
 flag="a";
 position=0;
 int i=1;
 while((position=s.find_first_of(flag,position))!=string::npos)
 {
  //position=s.find_first_of(flag,position);
  cout<<"position  "<<i<<" : "<<position<<endl;
  position++;
  i++;
 }

 //查詢flag 中與s 第一個不匹配的位置
 flag="acb12389efgxyz789";
 position=flag.find_first_not_of (s);
 cout<<"flag.find_first_not_of (s) :"<<position<<endl;


 //反向查詢,flag 在s 中最後出現的位置
 flag="3";
 position=s.rfind (flag);
 cout<<"s.rfind (flag) :"<<position<<endl;
}

 

說明:

1.  如果string sub = ”abc“;

              string s = ”cdeabcigld“;

     s.find(sub) , s.rfind(sub) 這兩個函式,如果完全匹配,才返回匹配的索引,即:當s中含有abc三個連續的字母時,才返回當前索引。

     s.find_first_of(sub),   s.find_first_not_of(sub),   s.find_last_of(sub),  s.find_last_not_of(sub)  這四個函式,查詢s中含有sub中任意字母的索引。

2.  如果沒有查詢到,則返回string::npos,這是一個很大的數,其值不需要知道。

 

程式碼:

#include<bits/stdc++.h>
using namespace std;
const int Maxn = 100010;
string s[Maxn];
int main()
{
	cin.sync_with_stdio(false);//沒有它會超時。使用它,可以讓cin,cout的速度與scanf,printf差不多
	int t;
	cin >> t;
	while(t--){
		int n; 
		cin >> n;
		int len;
		int maxid = -1;
		int Max = -1;
		int flag = true;
		for(int i = 0; i < n; i++) {
			cin >> s[i];
			len = s[i].length();
			if(len > Max || Max == -1) {
				Max = len;
				maxid = i; 
				flag = true;
			} 
			else if(len == Max && s[maxid] != s[i])
				flag = false;
		}
		if(flag) {
			for(int i = 0; i < n ; i++) {
				if(s[maxid].find(s[i]) == s[i].npos) {
					flag = false;
					break;
				}
			}
		}	
		if(flag)
			cout << s[maxid] << endl;
			
		else 	
			cout << "No" << endl; 
	}
	
	return 0;
}