日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

String Statistics(2008年珠海市ACM程序设计竞赛)

發布時間:2023/12/19 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 String Statistics(2008年珠海市ACM程序设计竞赛) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

String Statistics

時間限制(普通/Java):1000MS/3000MS????????? 運行內存限制:65536KByte

描述

?

You have an n × n matrix of lower-case letters. You start from somewhere in the matrix, walk in one of the eight directions (left, right, up, down, up-left, up-right, down-left, down-right) and concatenate every letter you meet, in the order you see them, to form a string. You can stop anywhere (possibly not moving at all!), but you cannot go out of the matrix, or change direction in the middle of the journey.How many different non-empty strings can you get?

?

輸入

?

The ?rst line contains t (1 ≤ t ≤ 10), the number of test cases followed. Each test case begins with one integer n(1 ≤ n ≤ 30), followed by n lines, containing the matrix. The matrix will contain lower-case letters only.

?

輸出

?

For each test case, print the number of different strings you can get.

?

樣例輸入

2 2 aa bb 3 aba bcc daa

?

樣例輸出

6 31 題目大意:給出一個n*n的矩陣,有上,下,左,右,左上,左下,右上,右下八個方向,從任意位置開始,按同一方向走動,可以在任意位置停止,輸出總共有多少種不為空且各不相同的字符串。 題解:固定方向,用set容器來插入字符串,這樣可以去重 #include<iostream> #include<string> #include<set> using namespace std;int dx[8]={0,0,1,-1,1,1,-1,-1}; //八個方向
int dy[8]={1,-1,0,0,-1,1,-1,1}; int n; char op[40][40]; string str; set<string>p;void dfs(int i,int j,int k) //按同方向搜索
{
if(i>0&&j>0&&i<=n&&j<=n){str+=op[i][j];p.insert(str);dfs(i+dx[k],j+dy[k],k);} } int main() {int i,j,k,t;cin>>t;while(t--){cin>>n;getchar();for(i=1;i<=n;i++)for(j=1;j<=n;j++)cin>>op[i][j];p.clear(); //每尋找一次需要清除容器里的東西
    
for(i=1;i<=n;i++)for(j=1;j<=n;j++)for(k=0;k<8;k++)
{
  str
= "";str+=op[i][j];p.insert(str);dfs(i+dx[k],j+dy[k],k);}cout<<p.size()<<endl;}return 0; }

?

轉載于:https://www.cnblogs.com/lavender913/p/3319857.html

總結

以上是生活随笔為你收集整理的String Statistics(2008年珠海市ACM程序设计竞赛)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。